D 语言:如果带有约束的函数签名直接引用类型或参数名称,它们是否被视为相等?

发布于 2024-11-08 06:47:18 字数 513 浏览 1 评论 0原文

考虑到以下函数签名(及其约束),它们会被认为是相同的吗?两者都通过了我的单元测试,所以我相信它们可能是,但我想知道它们是否真正相同或是否不同(但行为相同):

这里,签名约束指的是参数名称(我意识到运行时信息不可用,我的假设是编译器使用它们来引用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2024-11-15 06:47:18

就我个人而言,我会使用 if(is(typeof(T.init != E.init) == bool)) 来确保它与类型的变量有关

(然后当您希望 T 成为一个范围(并且丢失数组符号,它是 if(isInputRange(T) && is(typeof(T.init.front != E.init) == bool))


编辑:测试这样的事情的最佳方法是扩展测试用例:

如果我们采用不同的函数:

int binarySearch(T,E)(T[] haystack, E needle)
    if(is(typeof(haystack[0] < needle) == bool)) {
//...
   return -1;
}

它会按照您的预期进行编译和工作(除非实现细节......)

,但

int binarySearch(T,E)(T[] haystack, E needle)
    if(is(typeof(T < E) == bool)) {
//...
   return -1;
}

不会(调用 binarySearch([ 1,2,3],0); 上它不会编译)

但是就像我原来的答案:

int binarySearch(T,E)(T[] haystack, E needle)
   if(is(typeof(T.init > E.init) == bool)) {
    //...
    return -1;
}

这确实像预期的那样工作

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:

int binarySearch(T,E)(T[] haystack, E needle)
    if(is(typeof(haystack[0] < needle) == bool)) {
//...
   return -1;
}

this compiles and works as you'd expect (barring implementation details...)

but

int binarySearch(T,E)(T[] haystack, E needle)
    if(is(typeof(T < E) == bool)) {
//...
   return -1;
}

doesn't (calling binarySearch([1,2,3],0); on it doesn't compile)

however like my original answer:

int binarySearch(T,E)(T[] haystack, E needle)
   if(is(typeof(T.init > E.init) == bool)) {
    //...
    return -1;
}

this does work like expected

雨落□心尘 2024-11-15 06:47:18

我的第一个想法是函数签名与静态约束无关。我认为,使用时您的函数是生成的,它的签名也是如此。
约束只是为了消除歧义和/或生成编译时错误,我的假设是您不能谈论模板函数的签名,但也许作为签名模板:)

在您的示例中,我相信您想检查一个函数的可转换性键入另一个,例如将 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 to T, (is this int in the array of double), which is written with is(E : T) or using convenient template isImplicitlyConvertible in std.traits.

In your first example, you check that comparison of values haystack[0] and needle is possible, but there is no chance that you can find 3.14 in an array of int, however you can compare an int to a float, so this static if seems to be too permissive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文