[dmd-internals] Questions: how consttructors and postblits work with qualifiers

kenji hara k.hara.pg at gmail.com
Wed May 9 02:10:29 PDT 2012


I have some questions for the spec of struct object construction and copying.
I hope that Walter and Andrei gives the answers.

Question 1:
    This is about syntax question.

    If struct S has no constructor, struct literal syntax fills the
fields with arguments.
    -> S(a0, a1, ...) is similar to:
       { S s; s.field0=a0, s.field1=a1, ..., return s; }()
    -> S() is valid and it is same as S.init.

    If struct S has some constructors, it is always used for struct
literal syntax.
    -> S(...) always calls one of constructors. It there is no match,
raises compile error.
    -> S() is always invalid, because constructor should have one or
more parameters.

    This is my understanding, is it right?

Question 2:
    If S has a constructor which it gets one argument and its type is
not a kind of S:

        struct S {
            this(int n) { ... }
        }
        S s1 = 1;    // runs this(int n) implicitly.
        S s2 = S(1); // same as above

    This is filed as issue 7019 and 4875 in bugzilla.
    Is this a bug or feature? I think it is useful feature for such
types like BigInt.
    And should it work in module scope?

Question 3:
    If S has a constructor which it gets one argument and its type is
a kind of S:

        struct S {
            this(S src) { ... }
            this(const S src) { ... }
            this(immutable S src) { ... }
        }

    It works as *copy constructor* and called on initialization with copying.

        S sm;
        immutable S si = immutable(S)(sm); // calls this(immutable S
src) explicitly
        immutable S si = sm; // calls this(immutable S src) instead of
blit copying

    This is a feature explained in TDPL, but I think it is not good.
    a. If S has a postblit, it seems to me that they conflicts each other.
    b. If S has no mutable indirection and no postblit, we can switch
object qualifier
       with blit copying, like built-types. (e.g. int n; immutable int m = n;)
       In the case, I think copy constructors won't work. Is it right?

    It seems to me that copy constructor concept conflicts with postblit one.

Question 4:
    Qualified postblits should work?

        struct S {
            this(this) {}
            this(this) immutable {}
        }
        S sm;
        const S sc = sm;     // runs this(this)? or runs only blit copy? [X]
        immutable S si = sm; // runs this(this)immutable?

    But, with current dmd, multiple postblits conflicts each other.
    Even if should do, I can't imagine how they works in some cases, like [X].

Regards.

Kenji Hara


More information about the dmd-internals mailing list