-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Function
getters to be overriden as actual functions
#4159
Comments
A method overriding a function typed getter is probably sound. Overriding in that other direction is probably a bad idea. If nothing else, it will probably hurt performance. Also probably can't be allowed if there is a setter. Or maybe it can, but that risks breaking (more) internal assumptions in the compilers and runtimes. Or we could allow putting |
I like the idea of adding typedef MyAPI = int Function(int a, int b);
abstract class Base {
MyAPI get m;
}
class BaseA extends Base {
@override
final int m(int a, int b) => a + b; // <--- Changed this
}
class B {
void m(MyAPI api) {
print(api(1, 2));
}
}
void main() {
var b = B();
var a = BaseA();
b.m(a.m);
} |
Yes, basically that. So: class C {
final int x, y;
C(this.x, this.y)
static final int add(C c) => c.x + c.y;
final C swap() => C(y, x);
var C updateWith({int? x, int? y}) => x == null && y == null => this : C(x ?? this.x, y ?? this.y);
} would be mostly equivalent to: class C {
final int x, y;
C(this.x, this.y)
static final int Function(C) add = _$add;
static int _$add(C c) => c.x + c.y;
late final C Function() swap = _$swap;
C _$swap() => C(y, x);
late C Function({int? x, int? y}) updateWith = _$updateWith;
C _$updateWith({int? x, int? y}) => x == null && y == null => this : C(x ?? this.x, y ?? this.y);
} except that I expect the compiler to be able to optimize away the (Not saying this feature is worth its own complexity, just that if we were to do something like this, that's how I would do it.) |
If you have code like the following:
Say you are creating a package and have not added testing or actually called the last line above (
b.m(a.m)
) anywhere. There is no way for you to know that the call will break.If you have something like this:
The code will also work but the declaration at
BaseA
form
is not the best for writing down.I'd like to ask for function getters to be able to be written as actual functions since they would work the same way.
The text was updated successfully, but these errors were encountered: