June 22, 2017
On Thursday, 22 June 2017 at 13:56:29 UTC, ag0aep6g wrote:
> For example, the type system guarantees that immutable data never changes. But the compiler allows you to cast from immutable to mutable and change the data. It's an invalid operation, but the compiler is not expected to catch that for you.
Casts are part of the type system. Yes, D type system allows invalid operations. It's not the compiler's fault, it's type system's fault.

unittest
{
    immutable int a = 4;
    int* b = cast(int*) &a;
    *b = 5;
    assert(*(&a) == 5);
    assert(a == 4);
}
June 22, 2017
On Thursday, 22 June 2017 at 18:38:59 UTC, Boris-Barboris wrote:
> On Thursday, 22 June 2017 at 13:56:29 UTC, ag0aep6g wrote:
>> For example, the type system guarantees that immutable data never changes. But the compiler allows you to cast from immutable to mutable and change the data. It's an invalid operation, but the compiler is not expected to catch that for you.
> Casts are part of the type system. Yes, D type system allows invalid operations. It's not the compiler's fault, it's type system's fault.
>
> unittest
> {
>     immutable int a = 4;
>     int* b = cast(int*) &a;
>     *b = 5;
>     assert(*(&a) == 5);
>     assert(a == 4);
> }

Here it's the programmer's fault really. You should never use casts in normal code, cast is the ultimate switch to say "Look, I know what I'm doing, so disable all safety, don't try to make sense of it, and let me do my thing. If I'm telling you it's a cat, then it is dammit.". You can't blame the type system not to do something coherent here, you explicitely went out of your way to lie to that very same type system in the most unsafe way possible.
June 22, 2017
On Thursday, 22 June 2017 at 19:11:19 UTC, Cym13 wrote:
> Here it's the programmer's fault really. You should never use casts in normal code, cast is the ultimate switch to say "Look, I know what I'm doing, so disable all safety, don't try to make sense of it, and let me do my thing. If I'm telling you it's a cat, then it is dammit.". You can't blame the type system not to do something coherent here, you explicitely went out of your way to lie to that very same type system in the most unsafe way possible.
We're on the same page, I just think that ability to lie is part of the type system, that's all.
June 22, 2017
On 06/22/2017 08:38 PM, Boris-Barboris wrote:
> Casts are part of the type system. Yes, D type system allows invalid operations. It's not the compiler's fault, it's type system's fault.
> 
> unittest
> {
>      immutable int a = 4;
>      int* b = cast(int*) &a;
>      *b = 5;
>      assert(*(&a) == 5);
>      assert(a == 4);
> }

This is just arguing semantics, of course, but I wouldn't say that the type system allows this specific invalid operation. Rather, with casting you can step out of the type system, and break its guarantees.

Point is, you need a way to say that the operation is invalid. It's invalid because it breaks what `immutable` promises. `immutable` is part of the type, so I'd say the guarantee is part of the type system.
June 23, 2017
On Thu, 2017-06-22 at 18:38 +0000, Boris-Barboris via Digitalmars-d- learn wrote:
> […]
> 
> Casts are part of the type system. Yes, D type system allows
> invalid operations. It's not the compiler's fault, it's type
> system's fault.
> […]

Well maybe casts should be allowed as they effectively break the type system.

Sadly D2 has casts, so the type system is weak, so problems with GC algorithms allowed.

Maybe it is time for D3, which is D2 and no casts.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

1 2
Next ›   Last »