March 15, 2021 Re: What is the shortest way to define an immutable token of unique type? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 14.03.21 17:37, Andrei Alexandrescu wrote:
> Is there a simpler way that this?
>
> struct Hex {}
> immutable Hex hex;
>
> I don't want to introduce two names in the scope, just `hex`. Another attempt:
>
> immutable hex = function {};
>
> That's a bit... arcane. Is there another simple way? Thanks!
>
Not sure about simple or non-arcane, but this works:
immutable typeof(new class{}) hex;
|
March 15, 2021 Re: What is the shortest way to define an immutable token of unique type? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Monday, 15 March 2021 at 20:07:53 UTC, Timon Gehr wrote:
> On 14.03.21 17:37, Andrei Alexandrescu wrote:
>> Is there a simpler way that this?
>>
>> struct Hex {}
>> immutable Hex hex;
>>
>> I don't want to introduce two names in the scope, just `hex`. Another attempt:
>>
>> immutable hex = function {};
>>
>> That's a bit... arcane. Is there another simple way? Thanks!
>>
>
> Not sure about simple or non-arcane, but this works:
>
> immutable typeof(new class{}) hex;
This is nice because it's actually kinda readable
|
March 15, 2021 Re: What is the shortest way to define an immutable token of unique type? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Monday, 15 March 2021 at 20:07:53 UTC, Timon Gehr wrote:
> Not sure about simple or non-arcane, but this works:
>
> immutable typeof(new class{}) hex;
Nice. Would be interesting to list use cases for this in the docs.
Moreover
typeof(new class{})
is
__anonclass1
so, for conformity,
typeof(struct {})
should be allowed and be
__anonstruct1
|
Copyright © 1999-2021 by the D Language Foundation