D 语言:如果带有约束的函数签名直接引用类型或参数名称,它们是否被视为相等?
考虑到以下函数签名(及其约束),它们会被认为是相同的吗?两者都通过了我的单元测试,所以我相信它们可能是,但我想知道它们是否真正相同或是否不同(但行为相同):
这里,签名约束指的是参数名称(我意识到运行时信息不可用,我的假设是编译器使用它们来引用 haystack 和 Needle 类型):
T[] find(T, E)(T[] haystack, E needle)
if(is(typeof(haystack[0] != needle) == bool))
{
// ...
}
现在,如果我更新以引用类型 T 和 E,它仍然有效。我更喜欢这种形式,因为它明确表明我的签名约束正在查找类型(而不是运行时信息)......并且它更简洁:
T[] find(T, E)(T[] haystack, E needle)
if(is(typeof(T != E) == bool))
{
// ...
}
我的假设正确还是我遗漏了某些内容?
Given the below function signatures (and their constraints), would they be considered the same? Both pass my unittests, so I am led to believe they may be, but I'd like to know if they are truly identical or if they are different (but behave the same):
Here, the signature constraint refers the parameter names (I realize the runtime information is not available, my assumption is that the compiler uses these to refer to the types of haystack and needle):
T[] find(T, E)(T[] haystack, E needle)
if(is(typeof(haystack[0] != needle) == bool))
{
// ...
}
Now, if I update to refer to the types T and E, it still works. I like this form better because it's explicit that my signature constraint is looking the types (and not runtime information)... and well it's more concise:
T[] find(T, E)(T[] haystack, E needle)
if(is(typeof(T != E) == bool))
{
// ...
}
Are my assumptions correct or am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
就我个人而言,我会使用 if(is(typeof(T.init != E.init) == bool)) 来确保它与类型的变量有关
(然后当您希望 T 成为一个范围(并且丢失数组符号,它是
if(isInputRange(T) && is(typeof(T.init.front != E.init) == bool))
)编辑:测试这样的事情的最佳方法是扩展测试用例:
如果我们采用不同的函数:
它会按照您的预期进行编译和工作(除非实现细节......)
,但
不会(调用
binarySearch([ 1,2,3],0);
上它不会编译)但是就像我原来的答案:
这确实像预期的那样工作
personally I'd use
if(is(typeof(T.init != E.init) == bool))
to ensure it's about the vars of the type(and then when you want T to be a range (and losing the array notation it'd be
if(isInputRange(T) && is(typeof(T.init.front != E.init) == bool))
)edit: best way to test things like this is by expanding the test case:
if we take a different function:
this compiles and works as you'd expect (barring implementation details...)
but
doesn't (calling
binarySearch([1,2,3],0);
on it doesn't compile)however like my original answer:
this does work like expected
我的第一个想法是函数签名与静态约束无关。我认为,使用时您的函数是生成的,它的签名也是如此。
约束只是为了消除歧义和/或生成编译时错误,我的假设是您不能谈论模板函数的签名,但也许作为签名模板:)
在您的示例中,我相信您想检查一个函数的可转换性键入另一个,例如将
E
转换为T
(这是double
数组中的int
),即用is(E : T)
或在 std.traits 中使用方便的模板isImplicitlyConvertible
。在第一个示例中,您检查了
haystack[0]
和needle
值的比较是可能的,但您不可能找到3.14
> int 数组,但是您可以将 int 与 float 进行比较,因此这个static if
似乎过于宽松。My first tought is that the function signature is not related to static constraints. When used your function is generated, and so is its signature, I think.
Constraints are just to disambiguate and/or generate compile time errors, my assumption is that you can't talk about signature of a template function, but maybe, as signature template :)
In your example, I believe you want to check convertibility from one type to another, say
E
toT
, (is thisint
in the array ofdouble
), which is written withis(E : T)
or using convenient templateisImplicitlyConvertible
in std.traits.In your first example, you check that comparison of values
haystack[0]
andneedle
is possible, but there is no chance that you can find3.14
in an array ofint
, however you can compare an int to a float, so thisstatic if
seems to be too permissive.