[Issue 5984] New: add new keywords obj_const and obj_immutable to make it possible to declare mutable reference variables referencing const or immutable class instances

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 11 13:12:30 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=5984

           Summary: add new keywords obj_const and obj_immutable to make
                    it possible to declare mutable reference variables
                    referencing const or immutable class instances
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: ultimatemacfanatic at gmail.com


--- Comment #0 from Christopher the Magnificent <ultimatemacfanatic at gmail.com> 2011-05-11 13:08:28 PDT ---
Suppose we have struct A:

struct A
{
    ...
}


Next, suppose we have a function that takes two immutable pointers to As
(plural of A) 

immutable(A)*
func(immutable A* a1, immutable A* a2)
{
    ...
}


Finally suppose that we need a variable that can alternately point to the
object pointed to by a1 or a2 (which and be changed later during the function. 
This is straightforward in D.  It looks like this:



immutable(A)*
func(immutable A* a1, immutable A* a2)
{
    //TAKE NOTE OF THIS LINE--DECLARES MUTABLE POINTER TO IMMUTABLE STRUCT
OBJECT
    immutable(A)* a_ptr = null; 

    a_ptr = a1;

    <do stuff with a_ptr>

    if (<somecondition>)
        a_ptr = a2;

    <do more stuff with a_ptr>

    return a_ptr;
}

This is all fine and good.  But what happens if A is not a struct but a class? 
Well the pointer syntax becomes implied and <immutable(A)> means IMMUTABLE
reference variable pointing to immutable object which is *NOT WHAT IS NEEDED*.

There is no equivalent code to my knowledge that I can write in D that does the
EXACT same thing with class object variables that I just did with struct
pointers above while preserving the compiler's enforcment of the constness or
immutability of objects referenced by a1 and a2!  

Yes, I could have pointer variables pointing to object reference variables a1
and a2 which reference (implicitly point to without actual pointer syntax) the
actual heap objects; however, that double indirection (first explicit with
pointers and second implicit with the object reference variable) is inefficient
and somewhat confusing.  At worst, the D language, by its lacking the
appropriate constness-typing solution is actively encouraging the programmer to
simply cast away the constness or immutability of objects referenced by a1 and
a2 to gain the simplicity of storing reference to a1's object or a2's object in
a fully mutable reference variable (e.g. <A a_ptr;>)

THE SOLUTION:

The solution is two new keywords "obj_const" and "obj_immutable" or some
variation on those names which when applied as either a storage specifier (as
in <obj_const ClassName variableName;>) or as a type modifier (as in
<obj_immutable(ClassName) variableName;>) would create a MUTABLE REFERENCE
VARIABLE REFERRING TO A CONST OR IMMUTABLE CLASS OBJECT.

This would extend to declaring arrays of mutable references to const or
immutable objects like so

    obj_const(ClassName)[] myArray;

    myArray.length = 2;

    immutable ClassName immutableRef = cast(immutable) new
ClassName(<argument>);
    const ClassName constRef = new ClassName(<argument>);

    obj_const ClassName myMutableRef = immutableRef;

    myArray[0] = myMutableRef;

    myMutableRef = constRef;

    myArray[1] = myMutableRef

This new functionality would be backwards compatible with the existing meaning
of <const(ClassName)> and <immutable(ClassName)> which would continue to
declare an un-modifiable reference variable pointing to a const or immutable
class object.

The only compatibility issue I can see is with variables, types, and functions
clashing with the new keywords, which I expect would be unheard of, given the
highly specific nature of these keywords; it is very unlikely for there to be
any significant name clashes.

POSSIBLE KEYWORDS:

objconst, objimmutable
obj_const, obj_immutable
tailconst, tailimmutable
tail_const, tail_immutable

CONCLUSION:

Hopefully, these additions can add to the expressiveness and power of the D
language and increase the ability of the compiler to enforce constness and
immutability in ways that are useful to the programmer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list