Optional parameters?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Apr 1 22:44:45 UTC 2018


On Sunday, April 01, 2018 22:37:17 Boris-Barboris via Digitalmars-d-learn 
wrote:
> On Sunday, 1 April 2018 at 22:25:45 UTC, Jonathan M Davis wrote:
> > How would a pointer help? Instead of doing
> >
> > foo(nullable(42))
> >
> > he'd have to do
> >
> > foo(new int(42))
> >
> > which is just one character shorter and ends up allocating on
> > the heap, unlike with Nullable.
> >
> > - Jonathan M Davis
>
> foo(&x);

which doesn't work in @safe code and doesn't work when you have an rvalue as
you would when passing 42. Ultimately, using pointers ultimately either
requires explicitly allocating stuff on the heap to be able to pass rvalues,
or it has the same restrictions that ref does in terms of passing rvalues.
You can certainly take that approach if you'd like, but overall, I think
that it's safe to say that using Nullable generally causes fewer problems.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list