April 29, 2013 Re: Is the other-kind-of-null really necessary in Nullable and Variant? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On 2013-04-29, 18:02, Idan Arye wrote: > On Monday, 29 April 2013 at 15:39:47 UTC, Simen Kjaeraas wrote: >> On 2013-04-29, 17:34, Idan Arye wrote: >> >>> On Monday, 29 April 2013 at 12:23:04 UTC, deadalnix wrote: >>>> On Sunday, 28 April 2013 at 16:33:19 UTC, Idan Arye wrote: >>>>> When you use `std.typecons.Nullable` with a type that already accept `null` values, you get two types of nulls - the `Nullable`'s null state the the regular type's `null`: >>>>> >>>>> Nullable!string a; >>>>> writeln(a.isNull()); //prints "true" >>>>> a = null; >>>>> writeln(a.isNull()); //prints "false" >>>>> a.nullify(); >>>>> writeln(a.isNull()); //prints "true" >>>>> >>>> >>>> All types should be non nullable. Problem solved. >>> >>> *All* types? Even object references and pointers? >> >> That would be nice, yes. > > And what would they be initialized to? When you write: > Object obj; > what will `obj` refer to? It won't. That would be a compile-time error: 'Variable obj needs an initializer'. We have some of this already in @disable this(). However, a true non-nullable reference is, I believe, not possible in D today. -- Simen |
April 29, 2013 Re: Is the other-kind-of-null really necessary in Nullable and Variant? | ||||
---|---|---|---|---|
| ||||
Posted in reply to deadalnix | On Monday, 29 April 2013 at 16:14:02 UTC, deadalnix wrote: > On Monday, 29 April 2013 at 16:02:11 UTC, Idan Arye wrote: >> On Monday, 29 April 2013 at 15:39:47 UTC, Simen Kjaeraas wrote: >>> On 2013-04-29, 17:34, Idan Arye wrote: >>> >>>> On Monday, 29 April 2013 at 12:23:04 UTC, deadalnix wrote: >>>>> On Sunday, 28 April 2013 at 16:33:19 UTC, Idan Arye wrote: >>>>>> When you use `std.typecons.Nullable` with a type that already accept `null` values, you get two types of nulls - the `Nullable`'s null state the the regular type's `null`: >>>>>> >>>>>> Nullable!string a; >>>>>> writeln(a.isNull()); //prints "true" >>>>>> a = null; >>>>>> writeln(a.isNull()); //prints "false" >>>>>> a.nullify(); >>>>>> writeln(a.isNull()); //prints "true" >>>>>> >>>>> >>>>> All types should be non nullable. Problem solved. >>>> >>>> *All* types? Even object references and pointers? >>> >>> That would be nice, yes. >> >> And what would they be initialized to? When you write: >> Object obj; >> what will `obj` refer to? >> >> Also, what about the C&C++ interface? Without null values, how can you use an extern function that accepts or returns pointers? > > Data flow analysis can smash your face if you try to use that before initializing it. In fact, this is already done in many languages. The point is that you are forcing ref variables to point to data, even in paths where there is no data for them to point to. Let's say we have something like: MyClass myObject; auto myVar1=DEFAULT_VALUE_FOR_MY_VAR_1; bool objectCreated=false; if(myCondition()){ myObject=new MyClass(/*some arguments*/); objectCreated=true; myVar1=myObject.calculateSomething(); } auto myVar2=calculateSomethingElse(myVar1); if(objectCreated){ myObject.doSomething(myVar2); } doSomethingElse(myVar2); It would be nice to create `myObject` in the second `if`, right before we use it, but this is not possible, since if `myCondition` is `true` and we do allocate `myObject`, we need to use it to calculate `myVar2`. And we can't pull the call for `myObject.doSomething` to the first `if` either - because we need to calculate `myVar2` before we use it. So, why not bring the calculation of `myVar2` into the first `if` as well? Because we need it for `doSomethingElse`, which should be invoked whether or not we allocate `myObject`! As humans, we can easily see that if we didn't allocate `myObject` that will also mean that `objectCreated` remains false, and therefore the program will not enter the second `if`. I would like to see a data flow analysis mechanism that figures that out - and that's a simple case! If `myObject` was `int` it would be easy - we could initialize it to `0` at declaration. But object references are not that simple - if you take away `null`, what will you init `myObject` to? Remember - it needs to be an instance of `MyClass` or of a subclass of `MyClass`. That means that: a) You have to allocate memory for an object you will not use. b) You need to call a constructor, that might have side-effects. c) You end up with an object that has a meaningless state. Now, let's look at that meaningless object we created. What if `MyClass` has fields which are object references themselves? They can't be in the "uninitialized state" - I would like to see the data flow analysis mechanism that can follow that! So, that means we need to set them to meaningless objects as well. Now, consider implementing a linked list. |
April 29, 2013 Re: Is the other-kind-of-null really necessary in Nullable and Variant? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On 2013-04-29, 19:57, Idan Arye wrote: > On Monday, 29 April 2013 at 16:14:02 UTC, deadalnix wrote: >> On Monday, 29 April 2013 at 16:02:11 UTC, Idan Arye wrote: >>> On Monday, 29 April 2013 at 15:39:47 UTC, Simen Kjaeraas wrote: >>>> On 2013-04-29, 17:34, Idan Arye wrote: >>>> >>>>> On Monday, 29 April 2013 at 12:23:04 UTC, deadalnix wrote: >>>>>> On Sunday, 28 April 2013 at 16:33:19 UTC, Idan Arye wrote: >>>>>>> When you use `std.typecons.Nullable` with a type that already accept `null` values, you get two types of nulls - the `Nullable`'s null state the the regular type's `null`: >>>>>>> >>>>>>> Nullable!string a; >>>>>>> writeln(a.isNull()); //prints "true" >>>>>>> a = null; >>>>>>> writeln(a.isNull()); //prints "false" >>>>>>> a.nullify(); >>>>>>> writeln(a.isNull()); //prints "true" >>>>>>> >>>>>> >>>>>> All types should be non nullable. Problem solved. >>>>> >>>>> *All* types? Even object references and pointers? >>>> >>>> That would be nice, yes. >>> >>> And what would they be initialized to? When you write: >>> Object obj; >>> what will `obj` refer to? >>> >>> Also, what about the C&C++ interface? Without null values, how can you use an extern function that accepts or returns pointers? >> >> Data flow analysis can smash your face if you try to use that before initializing it. In fact, this is already done in many languages. > > The point is that you are forcing ref variables to point to data, even in paths where there is no data for them to point to. > > Let's say we have something like: > > MyClass myObject; > auto myVar1=DEFAULT_VALUE_FOR_MY_VAR_1; > bool objectCreated=false; > if(myCondition()){ > myObject=new MyClass(/*some arguments*/); > objectCreated=true; > myVar1=myObject.calculateSomething(); > } > auto myVar2=calculateSomethingElse(myVar1); > if(objectCreated){ > myObject.doSomething(myVar2); > } > doSomethingElse(myVar2); > > It would be nice to create `myObject` in the second `if`, right before we use it, but this is not possible, since if `myCondition` is `true` and we do allocate `myObject`, we need to use it to calculate `myVar2`. And we can't pull the call for `myObject.doSomething` to the first `if` either - because we need to calculate `myVar2` before we use it. So, why not bring the calculation of `myVar2` into the first `if` as well? Because we need it for `doSomethingElse`, which should be invoked whether or not we allocate `myObject`! > > As humans, we can easily see that if we didn't allocate `myObject` that will also mean that `objectCreated` remains false, and therefore the program will not enter the second `if`. I would like to see a data flow analysis mechanism that figures that out - and that's a simple case! > > If `myObject` was `int` it would be easy - we could initialize it to `0` at declaration. But object references are not that simple - if you take away `null`, what will you init `myObject` to? Remember - it needs to be an instance of `MyClass` or of a subclass of `MyClass`. That means that: > a) You have to allocate memory for an object you will not use. > b) You need to call a constructor, that might have side-effects. > c) You end up with an object that has a meaningless state. > > Now, let's look at that meaningless object we created. What if `MyClass` has fields which are object references themselves? They can't be in the "uninitialized state" - I would like to see the data flow analysis mechanism that can follow that! So, that means we need to set them to meaningless objects as well. > > Now, consider implementing a linked list. Now, consider the fact we have Nullable in Phobos. -- Simen |
April 29, 2013 Re: Is the other-kind-of-null really necessary in Nullable and Variant? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On Monday, 29 April 2013 at 18:20:35 UTC, Simen Kjaeraas wrote:
> On 2013-04-29, 19:57, Idan Arye wrote:
>
> Now, consider the fact we have Nullable in Phobos.
Yes, we have `Nullable` in Phobos. It works by having two member fields - `_value`, which stores the value, and `_isNull`, which specifies if the `Nullable` is null or not. Let's implement a bare bones Linked List:
class Node(T){
T value;
Nullable!(Node!T) next;
this(){}
this(T value){
this.value=value;
}
this(T value,Node!T next){
this.value=value;
this.next=next;
}
}
Now lets create an instance:
auto myList=new Node("Hello");
What will happen?
We create a new `Node!string`. it's `value` will be set to "hello", and since the one-argument constructor does not modify `next`, it will it will remain as the init value of `Nullable!(Node!string)`. What is the init value of `Nullable!(Node!string)`?
The init value of `Nullable!(Node!string)` is an object with two member fields - `value` of type `string` and `next` of type `Nullable!(Node!string)`. The default constructor modifies neither, so they will both remain with their initial values. The init value of `string` is an empty string. What is the init value of `Nullable!(Node!string)`? To find the answer, return to the beginning of this paragraph and read it again.
You are not supposed to be reading this paragraph. You are supposed to be stuck in an infinite recursion in the previous paragraph. As a human, you can cheat and escape it, but the computer is not so lucky - it'll allocate more and more objects until it'll crash with a stack overflow.
Actually - that won't happen, since the compiler is smart enough to detect type recursion and emit e compile time error. But the problem remains - we are left unable to implement any type of recursive structure.
|
April 29, 2013 Re: Is the other-kind-of-null really necessary in Nullable and Variant? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Idan Arye | On Monday, 29 April 2013 at 20:00:32 UTC, Idan Arye wrote:
>
> The init value of `Nullable!(Node!string)` is an object with two member fields - `value` of type `string` and `next` of type `Nullable!(Node!string)`. The default constructor modifies neither, so they will both remain with their initial values. The init value of `string` is an empty string. What is the init value of `Nullable!(Node!string)`? To find the answer, return to the beginning of this paragraph and read it again.
I made a mistake here - `Nullable!(Node!string)` is a struct with a boolean member `_isNull` and a `Node!string` member named `_value` which is an object as described in this paragraph. Still, the point remains - even if `_isNull` is true, `_value` still need to refer to a `Node!string` object.
|
Copyright © 1999-2021 by the D Language Foundation