Thread overview
[Issue 17953] inout-like mechanism to infer function attributes from callback attributes
Oct 31, 2017
anonymous4
Oct 31, 2017
anonymous4
Dec 17, 2022
Iain Buclaw
October 31, 2017
https://issues.dlang.org/show_bug.cgi?id=17953

--- Comment #1 from anonymous4 <dfj1esp02@sneakemail.com> ---
Apparently even templates don't help here much:
---
import std.typecons:Nullable;
Nullable!V convert(T,V)(Nullable!T v, scope V delegate(T) c)
{
        if(v.isNull)return Nullable!V();
        return Nullable!V(c(v.get()));
}

void f() pure
{
        Nullable!long v;
        Nullable!int v1=v.convert!(long,int)((a){ return cast(int)a; });
}
---
Error: pure function 'f' cannot call impure function 'convert!(long,
int).convert'

--
October 31, 2017
https://issues.dlang.org/show_bug.cgi?id=17953

anonymous4 <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|Other                       |All
                 OS|Other                       |All

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=17953

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P4

--
April 05
https://issues.dlang.org/show_bug.cgi?id=17953

Bastiaan Veelo <Bastiaan@Veelo.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Bastiaan@Veelo.net

--- Comment #2 from Bastiaan Veelo <Bastiaan@Veelo.net> ---
This came up again at the DLF quarterly meeting April 5, 2024, with industry partners, and Walter asked to make sure there is a feature request. As discussed there:

For arguments and return types of various mutability we have the `inout` keyword that saves us from having to define multiple function overloads[1]. A similar solution for other attributes does not exist.

For example, if you have foreach loops over a struct S both in a @nogc context
and with an allocating loop body,
```d
void f()
{
    S s;
    foreach (a; s)
        allocating(a);
}

void g() @nogc
{
    S s;
    foreach (a; s)
        nonallocating(a);
}

void allocating(int) {}

void nonallocating(int) @nogc {}
```

then S needs both of these opApply overloads:

```d
    int opApply(scope int delegate(int) dg);
    int opApply(scope int delegate(int) @nogc dg) @nogc;
```

Similar for @safe, nothrow, pure and their permutations.

Templating opApply[2] can help in many of these cases, but problems arise when classes and inheritance get involved[3].

There is a DIP for argument dependent attributes[4] but work on it has stalled.

[1] https://dlang.org/spec/const3.html#inout
[2] https://dlang.org/spec/statement.html#template-op-apply
[3] https://youtu.be/9lOtOtiwXY4?si=KME_ZddnrecMdWOJ&t=359
[4] https://github.com/dlang/DIPs/pull/198

--