www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22501] New: Improve lambda inference for delegate type

https://issues.dlang.org/show_bug.cgi?id=22501

          Issue ID: 22501
           Summary: Improve lambda inference for delegate type template
                    params
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kinke gmx.net

This currently works, templatizing the delegate type for attribute propagation
while still documenting the expected signature:

```
size_t count(T, P : bool delegate(T))(T[] array, P predicate) {
    size_t ret;
    foreach (e; array)
        if (predicate(e))
            ++ret;
    return ret;
}

size_t countOdd(int[] array)  nogc nothrow pure  safe {
    return array.count(delegate(int a) => a % 2 == 1);
}
```

But the usage is plain ugly and super-explicit. Inferring parameter types and
promoting the function literal to a delegate literal would be a very welcome
improvement and enable `array.count(a => a % 2 == 1)`.

--
Nov 10 2021