Worst ideas/features in programming languages?
victoroak
jackpboy at gmail.com
Fri Nov 5 21:22:12 UTC 2021
On Friday, 5 November 2021 at 17:02:05 UTC, Atila Neves wrote:
>
>> - @safe `void` initialization
>
> For what types? It doesn't compile for pointers, for instance,
> and I don't see why void initialising an int would be unsafe.
>
>> - .init
>
> Because?
>
Well, I can't answer for him but `void` initialization and
`.init` makes it impossible to have any meaningful constraint on
a type. And some types may depend on these constraints to
maintain safety.
```d
import std.stdio;
struct LimitedInt(int min, int max)
{
@disable this();
this(int number)
{
assert(number >= min);
assert(number <= max);
_number = number;
}
private int _number;
}
void main() @safe
{
LimitedInt!(1, 1000) x = void;
auto y = LimitedInt!(1, 1000).init;
writeln(x);
writeln(y);
}
```
It's `@safe` in this example but there's no way to enforce these
constraints when you have those.
More information about the Digitalmars-d
mailing list