| |
| Posted by H. S. Teoh in reply to victoroak | PermalinkReply |
|
H. S. Teoh
Posted in reply to victoroak
| On Fri, Nov 05, 2021 at 09:22:12PM +0000, victoroak via Digitalmars-d wrote:
> 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.
@safe does not mean enforcing type constraints. It means *memory* safety. The above code exhibits no memory unsafety, even though constraints are violated and the output is, ostensibly, wrong because of broken constraints.
T
--
There are four kinds of lies: lies, damn lies, and statistics.
|