On 11/11/2013 09:16 PM, Oren Ben-Kiki wrote:
At any rate, I'm not claiming this is necessarily the best approach for
Rust; I'm just wondering, what is the proposed way to address this use
case? None of the manual approaches seems very appealing (unless there's a
better one I missed).

My preferred approach is explicite derivation. May look like:

    struct S1 {
        x1 : uint,
        x2 : uint,
        x3 : uint = 1,
        x4 : uint = 1,

        fn f1 () {...},
        fn f2 () {...},
    }

    struct S2 [: S1] {      // optional subtyping for polymorphism
        x1 : uint,          // same field
        x2 : uint = 2,      // same field, with std value
        x3 : uint = 1,      // same field, same std value
        x4 : uint = 2,      // same field, diff std value
        x5 : uint,          // new field
        x6 : uint = 2,      // new field,  with std value

        fn f1 () = S1.f1,   // same func,  same value (reuse)
        fn f2 () {...},     // same func,  diff value (override)
        fn f3 () {...},     // new func
    }

    // more structs derived from S1

An advantage is that each new type is completely defined on place; except for the body of reused functions, but one still has the signature and knows where the body is to be found.

This apparently brings no novel issue and avoids a few know problems due to conventional "inductive" or "recursive" inheritance. All methods are right here. In theory and practice, I guess, one can well reuse from various existing types any suitable method; or more generally any role, trait, in ordinary sense of the terms. In case of conflict, the user explicitely states which one is intended, and there is no diamond problem.

But the main gain in my view is that this scheme provides very good auto-documentation, I guess. I have however no idea of how to implement that in a static lang, esp the subtyping aspect (but do think it's no more complicated, maybe even less) (I implemented it in Lua, rather easy).

Denis

_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to