Optional and NotNull version 0.5.0 - swift optional like and scala option like

aliak something at something.com
Mon Aug 20 12:00:57 UTC 2018


On Monday, 20 August 2018 at 09:16:18 UTC, ikod wrote:
> On Thursday, 16 August 2018 at 16:20:09 UTC, aliak wrote:
>> On Thursday, 16 August 2018 at 12:25:14 UTC, aliak wrote:
>>> It's also @nogc and @safe
>>
>> No it's not. Not dispatching at least. Dunno why though. Seems 
>> safey is because taking an address. Nogc will have to look in 
>> to.
>
> Hello!
>
> please, help, how can I use match in @nogc code:
>
> import optional;
>
> void main() @nogc {
>     Optional!int i = 1;
>     bool b = true;
>     i.match!(
>         (v) => b,
>         () => false
>     );
> }
>
> fails to compile:
> source/app.d(3,6): Error: function `D main` is @nogc yet 
> allocates closures with the GC
> source/app.d(7,9):        app.main.__lambda1 closes over 
> variable b at source/app.d(5,10)
>
> Thanks!

Hi, it's because the lambda "(v) => b" allocates. Same would 
happen with any other function that takes an alias to a lambda 
and that lambda causes a closure allocation.

Changing this to "(int v) => b" seems to make the compiler infer 
@nogc though and it compiles.

But it's still a delegate, so I'm not sure why that works and not 
sure in which situations D infers a nogc delegate.

The same thing does not work for algorithm.map for e.g.

Cheers
- Ali




More information about the Digitalmars-d-announce mailing list