D - Unsafe and doomed

Timon Gehr timon.gehr at gmx.ch
Sat Jan 4 09:57:26 PST 2014


On 01/04/2014 03:46 PM, Adam D. Ruppe wrote:
> On Saturday, 4 January 2014 at 12:37:34 UTC, ilya-stromberg wrote:
>> Have you got any plans to impove this situation?
>
> I wrote a NotNull struct for phobos that could catch that situation. I
> don't think it got pulled though.
>
> http://arsdnet.net/dcode/notnull.d
>
> With @disable is becomes reasonably possible to restrict built in types
> with wrapper structs. It isn't perfect but it isn't awful either.
> ...

This mechanism would be more useful if moving was specified to occur 
whenever provably possible using live variable analysis.

Currently it is impossible to implement even a type analogous to rusts 
~T type, a unique reference (without a non-dereferenceable state.)

> The big thing people have asked for before is
>
> Object foo;
> if(auto obj = checkNull(foo)) {
>     obj == NotNull!Object
> } else {
>    // foo is null
> }
>
> and i haven't figured that out yet...

I think it is impossible to do, because the boolean value tested must be 
computable from the result of checkNull, which must be a variable of 
type NotNull!Object, which does not have a state for null.

The following is possible:

auto checkNull(alias notnull, alias isnull,T)(T arg) /+if(...)+/{
     return arg !is null ? notnull(assumeNotNull(arg)) : isnull();
}

Object foo;

foo.checkNull!(
     obj => ... /* is(typeof(obj)==NotNull!Object) */,
     ()  => ... /* foo is null */,
)


More information about the Digitalmars-d mailing list