January 09
On Monday, 8 January 2024 at 18:03:00 UTC, Jonathan M Davis wrote:
> On Monday, January 8, 2024 3:57:52 AM MST FeepingCreature via Digitalmars-d wrote:
>> Force default values to be typed immutable?
>
> That would require that there be a general way to copy an immutable value to a mutable one, because the object must be mutable after it's been default-initialized.
>
> Bastiaan's suggestion to make such cases illegal is probably the only sane way to prevent these kind of issues, though I think that it would have to be an error to have mutable indirections with an allocation rather than illegal to point to any allocations; otherwise, stuff like SysTime's member which is Rebindable(immutable TimeZone) becomes illegal.
>
> - Jonathan M Davis

Yes, the point of making it forced `immutable` is exactly to make the problematic cases illegal. Don't even think about deepcopy vs array vs object etc., just let the const system handle it.
January 09
On Monday, January 8, 2024 11:16:42 PM MST FeepingCreature via Digitalmars-d wrote:
> On Monday, 8 January 2024 at 18:03:00 UTC, Jonathan M Davis wrote:
> > On Monday, January 8, 2024 3:57:52 AM MST FeepingCreature via
> >
> > Digitalmars-d wrote:
> >> Force default values to be typed immutable?
> >
> > That would require that there be a general way to copy an immutable value to a mutable one, because the object must be mutable after it's been default-initialized.
> >
> > Bastiaan's suggestion to make such cases illegal is probably the only sane way to prevent these kind of issues, though I think that it would have to be an error to have mutable indirections with an allocation rather than illegal to point to any allocations; otherwise, stuff like SysTime's member which is Rebindable(immutable TimeZone) becomes illegal.
>
> Yes, the point of making it forced `immutable` is exactly to make the problematic cases illegal. Don't even think about deepcopy vs array vs object etc., just let the const system handle it.

Maybe? My gut reaction is that there's likely to be an issue with that, but at a glance, it seems like it might work. So, it could be a really good idea, but there may also be some subtlety that I'm not thinking of at the moment.

- Jonathan M Davis



January 12
On Tuesday, 9 January 2024 at 06:16:42 UTC, FeepingCreature wrote:
>
> Yes, the point of making it forced `immutable` is exactly to make the problematic cases illegal. Don't even think about deepcopy vs array vs object etc., just let the const system handle it.

I might be misinterpreting, but it seems like you're suggesting
```
S s;
s.arr[0] = "imm"; // compile-time error?
s.arr = ["new"];
s.arr[0] = "mut"; // ok
```
I'm not seeing how that would fly.

I can imagine it being easier done at runtime - with a bit of extra overhead - using mprotect or tagged pointers.
January 12
On Friday, 12 January 2024 at 09:57:25 UTC, Iain Buclaw wrote:
> On Tuesday, 9 January 2024 at 06:16:42 UTC, FeepingCreature wrote:
>>
>> Yes, the point of making it forced `immutable` is exactly to make the problematic cases illegal. Don't even think about deepcopy vs array vs object etc., just let the const system handle it.
>
> I might be misinterpreting, but it seems like you're suggesting
> ```
> S s;
> s.arr[0] = "imm"; // compile-time error?
> s.arr = ["new"];
> s.arr[0] = "mut"; // ok
> ```
> I'm not seeing how that would fly.
>
> I can imagine it being easier done at runtime - with a bit of extra overhead - using mprotect or tagged pointers.


I see no problem in having runtime overhead for the sake of having default initialized arrays in that case :)

I also would not complain if we had a way to choose between the extra overhead vs an error message by using an UDA.
January 13

On Friday, 12 January 2024 at 09:57:25 UTC, Iain Buclaw wrote:

>

On Tuesday, 9 January 2024 at 06:16:42 UTC, FeepingCreature wrote:

>

Yes, the point of making it forced immutable is exactly to make the problematic cases illegal. Don't even think about deepcopy vs array vs object etc., just let the const system handle it.

I might be misinterpreting, but it seems like you're suggesting

S s;
s.arr[0] = "imm"; // compile-time error?
s.arr = ["new"];
s.arr[0] = "mut"; // ok

I'm not seeing how that would fly.

I can imagine it being easier done at runtime - with a bit of extra overhead - using mprotect or tagged pointers.

Not quite: you couldn't even define S as

struct S
{
    int[] arr = [1, 2, 3]; // Error: immutable(int)[] cannot be converted to int[].
}

The type of an array-literal, evaluated at compile-time, would always be immutable. That way, you'd either have to define a constructor or you would be protected from accidental aliasing anyways. You'd be missing out on intentional aliasing, but that's such a corner case I'm fine sacrificing it.

1 2
Next ›   Last »