How can I define class methods OUTSIDE of the class' brackets to improve readability? E.g., something similar to this...
class MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo();
//Public methods:
int getFoo();
}
void _incrementFoo() {
_foo++;
}
int getFoo() {
_incrementFoo();
return _foo--;
}
As you can see, I would like to tell the class about the methods by providing a signature, then I would actually define them outside of the class... Any way to do this in dart?
Well, I don't agree that what you want to do would improve the readability. If you want to split the interface and implementation I think the following are a lot better:
abstract class MyClass {
//Public methods:
int getFoo();
factory MyClass() => _MyClass();
}
class _MyClass implements MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo() => _foo++;
//Public methods implementation:
int getFoo() => _foo;
}
If you really want to do it you way you need to have a way to tell you method that they are part of you class. You could do something like this:
class MyClass {
//Private variables:
int _foo = 0;
//Private methods:
void _incrementFoo() => _incrementFooImpl(this);
//Public methods:
int getFoo() => getFooImpl(this);
}
void _incrementFooImpl(MyClass myClass) {
myClass._foo++;
}
int getFooImpl(MyClass myClass) {
myClass._incrementFoo();
return myClass._foo--;
}
But I think this is rather ugly and you are ending up adding a lot of global methods in your code which can make it hard to see which class each method are belonging to.