digitalmars.D - SafeD and Nullable concept
- Denis Koroskin (5/10) Jan 14 2009 I think I have found one more reason for Nullable concept to appear in D...
- Jarrett Billingsley (6/19) Jan 15 2009 How do you distinguish between the key being missing and there being a
- Steven Schveighoffer (23/36) Jan 15 2009 Because you can change the value, not what the value poitns to. For
I think I have found one more reason for Nullable concept to appear in D: Since SafeD disallows pointers, it needs something safe to replace them. For example, it is impossible to use AA in SafeD, because opIn_r returns a pointer:Foo[Bar] aa; Bar bar = new Bar(); Foo* fooPtr = bar in aa; // this line is disallowed in SafeDI wonder why it returns a pointer, and shouldn't it return Nullable!(Foo) (or just Foo? as an alias) instead?Foo? foo = bar in aa; // null, if item is missingNote that now it became slightly more efficient, because we don't need an additional pointer dereference, which was present when we returned Foo*!
Jan 14 2009
On Thu, Jan 15, 2009 at 2:38 AM, Denis Koroskin <2korden gmail.com> wrote:I think I have found one more reason for Nullable concept to appear in D: Since SafeD disallows pointers, it needs something safe to replace them. For example, it is impossible to use AA in SafeD, because opIn_r returns a pointer:How do you distinguish between the key being missing and there being a null reference in the AA? Also, if you have an AA like: Foo?[Bar] aa; What is the return type of in? "Foo??" ?Foo[Bar] aa; Bar bar = new Bar(); Foo* fooPtr = bar in aa; // this line is disallowed in SafeDI wonder why it returns a pointer, and shouldn't it return Nullable!(Foo) (or just Foo? as an alias) instead?Foo? foo = bar in aa; // null, if item is missingNote that now it became slightly more efficient, because we don't need an additional pointer dereference, which was present when we returned Foo*!
Jan 15 2009
"Denis Koroskin" wroteI think I have found one more reason for Nullable concept to appear in D: Since SafeD disallows pointers, it needs something safe to replace them. For example, it is impossible to use AA in SafeD, because opIn_r returns a pointer:Because you can change the value, not what the value poitns to. For example, in an AA, if I want to replace the object found at a specific key, I need a reference to the class reference, i.e. a pointer. I think what might be useful is to make references nullable (and have some operator to determine if that is the case), and use references instead of pointers wherever a null return value is a possibility. The danger of pointers is that you can willy-nilly point them to any memory location. References stick to where they were set (and in SafeD, cannot be created with pointer arithmetic). so something like: ref Foo fooPtr = bar in aa; if(fooPtr !is nullref) // new keyword { ... } or if(fooPtr !refis null) or with no new keywords: if(ref fooPtr !is null)Foo[Bar] aa; Bar bar = new Bar(); Foo* fooPtr = bar in aa; // this line is disallowed in SafeDI wonder why it returns a pointer, and shouldn't it return Nullable!(Foo) (or just Foo? as an alias) instead?Then how do you change the value at that location? To further demonstrate the point, imagine Foo is a struct, not a class. -SteveFoo? foo = bar in aa; // null, if item is missingNote that now it became slightly more efficient, because we don't need an additional pointer dereference, which was present when we returned Foo*!
Jan 15 2009