- html - 我的下拉菜单的内容关闭得太快
- c# - 使用 Html Agility Pack 从网页中的表中获取值而不使用 "SelectNode'
- html - 内容容器下的 CSS 下拉菜单
- html - 如何停止嵌套列表重叠父列表?
说,我有
member this.Test (x: 'a) = printfn "generic"
1
member this.Test (x: Object) = printfn "non generic"
2
var a = test.Test(3); // calls generic version
var b = test.Test((object)3); // calls non generic version
var c = test.Test<object>(3); // calls generic version
let d = test.Test(3); // calls non generic version
let e = test.Test<int>(3); // calls generic version
Object
而不是它的继承类?)
Object
,则有点危险。 type 而另一个是泛型的,并且都返回相同的类型。 (就像在这个例子中,或者在单元测试中
Assert.AreEqual
),因为那么我们很可能在没有注意到的情况下得到错误的重载(不会是任何编译器错误)。不会有问题吗?
Assert.AreEqual(3, 5)
如 Assert.AreEqual(Object a, Object b)
但不是 Assert.AreEqual<T>(T a, T b)
Array.BinarySearch([|2;3|], 2)
如 BinarySearch<T>(T[]array, T value)
但不是 BinarySearch(Array array, Object value)
最佳答案
F# Method overload resolution not as smart as C#?
3
已上传到
obj
类型。这两种方法都适用,但选择了更简单(非通用)的方法。
1) Prefer candidates whose use does not constrain the use of a user-introduced generic type annotation to be equal to another type.
2) Prefer candidates that do not use ParamArray conversion. If two candidates both use ParamArray conversion with types pty1 and pty2, and pty1 feasibly subsumes pty2, prefer the second; that is, use the candidate that has the more precise type.
3) Prefer candidates that do not have ImplicitlyReturnedFormalArgs.
4) Prefer candidates that do not have ImplicitlySuppliedFormalArgs.
5) If two candidates have unnamed actual argument types ty11...ty1n and ty21...ty2n, and each ty1i either
a. feasibly subsumes ty2i, or
b. ty2i is a System.Func type and ty1i is some other delegate type, then prefer the second candidate. That is, prefer any candidate that has the more specific actual argument types, and consider any System.Func type to be more specific than any other delegate type.
6) Prefer candidates that are not extension members over candidates that are.
7) To choose between two extension members, prefer the one that results from the most recent use of open.
8) Prefer candidates that are not generic over candidates that are generic—that is, prefer candidates that have empty ActualArgTypes.
type T() =
member this.Test (x: 'a) = printfn "generic"; 1
member this.Test (x: System.ValueType) = printfn "non-generic"; 2
let t = T()
let d = t.Test(3) // calls non-generic version
let e = t.Test(test) // call generic version
Array.BinarySearch([|2;3|], 2)
已解析为
BinarySearch<T>(T[] array, T value)
.这是关于函数参数的另一个示例,其中
T.Test((fun () -> 2), 2)
T.Test(f: unit -> 'a, v: 'a)
T.Test(f: unit -> obj, v: obj)
关于F# 方法重载解析不如 C# 智能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14616462/
假设我有一个类,我在其中重载了运算符 == : Class A { ... public: bool operator== (const A &rhs) const; ... };
我知道你不应该使用 std::find(some_map.begin(), some_map.end()) 或 std::lower_bound,因为它会采用线性时间而不是 some_map.lowe
我正在尝试在 Haskell 中定义 Vector3 数据类型,并允许在其上使用 (+) 运算符。我尝试了以下方法: data Vector3 = Vector3 Double Double Doub
我已经为我的类图将运算符重载为“-”。它的用途并不完全直观(糟糕的编码 - 我知道)但是如果我做 graph3 = graph2-graph1 那么图 3 是应该只接收图 2 和图 1 中的那些顶点。
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Operator overloading 我想重载 以按字母顺序排列字符串,但我不确定该怎么做。 如何再次
下面的代码给我一个编译错误。谁能告诉我为什么? class mytype { public: int value; mytype(int a) { value = a;
这有什么问题吗? class Vec2 attr_accessor :x, :y # ... def += (v) @x += v.x @y += v.y retu
是否可以重载 [] 运算符两次?允许这样的事情:function[3][3](就像在二维数组中一样)。 如果可能的话,我想看看一些示例代码。 最佳答案 您可以重载 operator[] 以返回一个对象
我的团队目前正在与 Lua 合作,创建一个 android 游戏。我们遇到的一件事是表面上无法创建重载构造函数。 我习惯于使用默认值设置一个对象,然后在需要时使其过载。 前任: apples() {
我有一个网页,在某个时候显示一个导航栏,它只不过是一个 a 元素的列表 (ul)。所述 a 元素的大多数样式规则都是通用的。唯一应该改变的部分是要显示的图像,可以从列表中每个 li 元素的 id 标签
我对使用/重载“范围步长”运算符(.. ..)很感兴趣,但我终其一生都无法了解如何使用它。 在文档中它说 // Usage: start .. step .. finish 但是在 F# shell
Java 11(可能无关紧要): public static String toString(Object obj) { return ReflectionToStringBuilder.to
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我无法理解以下代码(针对行号进行注释) class Base { void m1(Object o) { } void m2(String o) { } } publi
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
class myclass{ //definitions here }; myclass e; int myarray[10]; /* Do something... */ e = myarray;
为什么不能将下标运算符(operator [])作为 friend 函数重载? 最佳答案 正如Bjarne Stroustrup在D&E book中所说: However, even in the o
我有以下代码片段: #include using namespace std; struct Integer{ int x; Integer(const int val) : x(v
因此,我有一个问题是我最近尝试重载 namespace Eng { /** * A structure to represent pixels */ typedef
如何重载onResume()以正确的方式工作?我想从 activity 返回到 MainActivity ,我希望在其中具有与应用程序启动后相同的状态。我想使用 recreate() 但它循环了或者类
我是一名优秀的程序员,十分优秀!