Smart pointers instead of GC?

Adam D. Ruppe destructionator at gmail.com
Tue Feb 4 19:27:20 PST 2014


On Wednesday, 5 February 2014 at 00:47:27 UTC, Walter Bright 
wrote:
> The difficulty comes when needing to transfer the nullability 
> of the argument to the nullability of the return type. Implicit 
> conversions don't help with that.
>
> See inout.

You could always do it with a template; T identity(T t) { return 
t;} will literally work. (I'm pretty sure it would also work with 
const and immutable; the reason inout exists isn't that it is 
impossible, it is just to counter template bloat)

Is this common enough to warrant an addition, or would templates 
be good enough when it is needed? inout has a very common real 
world use case: slicing.

inout(char)* strchr(inout(char)* haystack, in char* needle);

What's the real world equivalent to strchr with regard to 
nullability?


* typeof(this) inside a class is never nullable. You can't call a 
method with a null this. So a method that returns this can always 
just return the regular (not null) type.

* this[x..y] is never nullable. Slicing a null reference is 
illegal, so again, no need to offer the nullable overload.

* Returning a field of a nullable type is illegal, dereferencing 
null to get to the fields is illegal. Whereas a struct might 
return inout(member_variable), it never makes sense to do 
inout_nullability(member_variable).


I can't think of a single real world case where you'd need 
this... and if I'm wrong, we can always use a template and/or 
revisit the problem later.


More information about the Digitalmars-d mailing list