www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17578] New: Propagate the common qualifier of fields to the

https://issues.dlang.org/show_bug.cgi?id=17578

          Issue ID: 17578
           Summary: Propagate the common qualifier of fields to the
                    containing type
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: andrei erdani.com

This is a language enhancement that simplifies the use of qualifiers and
eliminates the necessity of boilerplate code.

Consider:

struct Widget
{
    // state {
    immutable int a;
    immutable double b;
    // } state
    void fun() { ... }
    void gun() immutable { ... }
}

The important tidbit is that all fields have the immutable qualifier. Now fun
can only be called against a mutable Widget, whereas gun can only be called
against an immutable Widget. However, note that the difference between Widget
and immutable(Widget) is artificial because fun and gun have the same exact
constraints. Even though type Widget is de jure "mutable", no mutation can be
done within it.

This proposes that Widget and immutable(Widget) are the same type, i.e. the
artificial distinction between the two types is removed. That means the two may
be use interchangeably, same as e.g. applying immutable(T) to a template
parameter T that already was immutable yields T.

This enhancement relaxes constraints on qualified functions (both free and
methods) and eliminate the need for copies, casts, and boilerplate to vacuously
adjust qualifiers.

There is generalization to all qualifiers and also to combinations of
qualifiers. Consider:

struct Midget
{
    // state {
    immutable int a;
    const double b;
    // } state
    ...
}

In this case, the common qualifier of const and immutable is const, so Midget
and const(Midget) are the same type.

The rules for figuring the common qualifier of all fields in an object are
derived from https://dlang.org/spec/const3.html#implicit_conversions.

--
Jun 30 2017