dart-lang / language

Design of the Dart language
Other
2.67k stars 205 forks source link

Variable/Attribute alias #339

Open qholi opened 5 years ago

qholi commented 5 years ago

I always use this workaround as an alias for a variable.

class A {
    B b;
}

class B {
    bool isLoading;
}

class C {
    A a;
    bool get isLoading => a.b.isLoading;
    set isLoading(bool v) {
        a.b.isLoading = v;
    }
}

Could add a new keyword like below to make it easily?

class C {
    A a;
    gset isLoading = a.b.isLoading;
}

Dart Version: 2.2.0 OS: Windows Browser: Chrome

everton-e26 commented 5 years ago

another solution could be

class C {
 A a;
 get isLoading = a.b.isLoading;
 set isLoading = a.b.isLoading;
}
lrhn commented 5 years ago

Forwarding operations to another object is something that can be useful in multiple situations, especially when you can avoid repeating parameters. We currently only support that for constructors: Superclass(int x) = Subclass; The = syntax for a function declaration could be reused, so you can write:

returnType someFunction(ArgumentType x) = _otherObject._otherFunction;
a14n commented 5 years ago

We currently only support that for constructors: Superclass(int x) = Subclass;

Couldn't it be simplify to Superclass = Subclass; in the case of parameters match?

lrhn commented 5 years ago

We could potentially allow omitting the parameters from the redirecting constructor (which must be a factory constructor). It may make parsing harder, but it's should not be completely impossible for constructors (I can't guarantee it'll extend to other declarations too because then it might look like a variable declaration).