Summary: Non-null class references/pointers
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD

--- Comment #0 from 2010-08-02 16:27:19 PDT ---
This is a starting point for an enhancement request about a type modifier to
specify that a class reference or pointer can't be null.

This is not an enhancement request about "nullable values" (values wrapped in a
struct that contains a boolean that is true if the value is "null").

This enhancement request assumes that on default both class references and
pointers are nullable (because today defaulting to non-null class references is
probably an impossible change in D).

A possible D syntax to denote a non-null reference or pointer is to use a @
suffix (better looking alternatives are possible):

class T {}
T nullable_reference;
T@ nonnullable_reference = new T@();

struct S {}
S nullable_pointer;
S@ nonnullable_pointer = new S@();

A possible alternative is to use the - (or +) suffix:

class T {}
T nullable_reference;
T- nonnullable_reference = new T-();

struct S {}
S nullable_pointer;
S- nonnullable_pointer = new S-();

A possible problem with non-null class references can be seen with this D
program that uses the trailing @ syntax:

class Foo {}

class A {
    Foo@ name;
    this(Foo@ s) { = s;

    void m() { /*...*/ }

class B : A {
    Foo@ path;
    this(Foo@ p, Foo@ s) {
        this.path = p;

    override void m() {
        // here this.path is null despite it's a non-null
        assert(this.path !is null);

void main() {
    new B(new Foo, new Foo);

I have adapted that example from this paper, it discusses about partially
uninitialized objects too:

A comment about that program from the paper:

>The problem with the code is that during the base call to A's constructor, the 
>virtual method B.m may be invoked. At this time, field path of the object 
>under construction has not yet been initialized. Thus, accesses of this.path 
>in method B.m may yield a possibly-null value, even though the field has been 
>declared as being non-null.<

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to