www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - SafeD and Nullable concept

reply "Denis Koroskin" <2korden gmail.com> writes:
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 SafeD
I 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 missing
Note 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
next sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
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:

 Foo[Bar] aa;

 Bar bar = new Bar();
 Foo* fooPtr = bar in aa; // this line is disallowed in SafeD
I 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 missing
Note that now it became slightly more efficient, because we don't need an additional pointer dereference, which was present when we returned Foo*!
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??" ?
Jan 15 2009
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Denis Koroskin" 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:

 Foo[Bar] aa;

 Bar bar = new Bar();
 Foo* fooPtr = bar in aa; // this line is disallowed in SafeD
I wonder why it returns a pointer, and shouldn't it return Nullable!(Foo) (or just Foo? as an alias) instead?
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? foo = bar in aa; // null, if item is missing
Note that now it became slightly more efficient, because we don't need an additional pointer dereference, which was present when we returned Foo*!
Then how do you change the value at that location? To further demonstrate the point, imagine Foo is a struct, not a class. -Steve
Jan 15 2009