Null references
Simen Kjaeraas
simen.kjaras at gmail.com
Tue Aug 21 06:30:25 PDT 2012
On Tue, 21 Aug 2012 13:55:58 +0200, Regan Heath <regan at netmail.co.nz>
wrote:
> I don't buy the argument that having to explicitly type ".get" will stop
> anyone from automatically coding statements like:
>
> val g: Option[T] = f.doSomething()
> g.get.doSomethingElse()
So don't give them that access?
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.
> On the other hand, the suggested syntax of "bob?.department?.head?.name"
> from Kotlin is pure genius, and has a good chance of actually being
> used.
Absolutely. Probably hard to include in D though, as a ? .b is the
beginning of a conditional expression.
--
Simen
More information about the Digitalmars-d
mailing list