What are (were) the most difficult parts of D?

Paul Backus snarwin at gmail.com
Wed May 11 15:34:12 UTC 2022


On Wednesday, 11 May 2022 at 15:10:28 UTC, Tejas wrote:
> That'll be true the day when `@safe` becomes the default... 
> Until then, I'll atleast do `@safe:` on top of every module :)

`@safe:` is actually a bad idea if you're writing templated code, 
because it turns inference of `@system` into a compile-time error:

```d
@safe:

auto map(alias fun, T)(T[] arr)
{
     auto result = arr.dup;
     foreach (ref e; result) e = fun(e);
     return result;
}

int safeDouble(int n) @safe { return 2*n; }
int systemDouble(int n) @system { return 2*n; }

void main() @system
{
     int[] a = [1, 2, 3];
     // ok
     auto b = a.map!safeDouble;
     // Error: `@safe` function `map!systemDouble` cannot call 
`@system` function `systemDouble`
     auto c = a.map!systemDouble;
}
```

Without `@safe:` at the top, `map!systemDouble` would have been 
inferred as `@system`, and calling it from a `@system` `main` 
function would have been allowed.


More information about the Digitalmars-d-learn mailing list