Null references

Philippe Sigaud philippe.sigaud at gmail.com
Wed Aug 22 08:42:12 PDT 2012


On Tue, Aug 21, 2012 at 10:17 PM, Nick Treleaven <nospam at example.net> wrote:
> On 21/08/2012 14:30, Simen Kjaeraas wrote:


>> I have an Option struct on my home computer that only allows access to
>> the held value if the error case is simultaneously handled:
>>
>> Option!int a = foo();
>>
>> int n = a.match!(
>>      (int x) => x,
>>      (None n) => 0 // or throw, or what have you.
>>      );
>>
>> The same solution is perfectly adaptable to MaybeNull, MaybeNan, and
>> probably
>> a host of others of the same family.
>>
>> With some clever use of opDispatch, one could write an Option struct that
>> lets you call methods and acces members of the wrapped type, and return
>> Option!ReturnType.
>
>
> Apart from the opDispatch bit, I've been slowly working on something similar
> (Boost license):
>
> https://github.com/ntrel/d-maybe/blob/master/maybe.d
>
> I'm still working on it, but the basic ideas are there. I think the best bit
> is apply(), which supports passing multiple Maybe values, calling the
> delegate only if all Maybes are valid. In fact Maybe.map() is unnecessary
> due to apply().

Then, both Simen and you could code a generic algebraic datatype
generator, with the associated matching functions (and probably
mapping / reducing) Here come the whole Haskell / ML menagerie of
types :)

mixin(ADT("
Tree(T):
    Leaf(T)
  | Branch(Tree, Tree)
"));

or something like that... And then encode JSON like this.

Or update std.typecons.Algebraic to make it deal with recursive definitions...


More information about the Digitalmars-d mailing list