Kasper Peulen
2016-08-18 11:17:45 UTC
Okay, this may sound a bit outrageous hehe. It is probably way too much of
change for Dart 2.0, but I find this idea in general interesting for a
language and I would like to discuss it.
Heavily inspired by this
post: https://medium.com/@emilniklas/constructors-considered-harmful-17cccfab8d3#.3nzua8bed
I think I also heard Gilad Brach say something that the constructors in
Dart are a mess, and I agree.
I think there is just too much ways to do essentially the same way, and I
think we can do much better.
So I think instead of having named constructor, factory constructors,
initializer lists, etc. I think you should all remove it from the language.
And there should be just one way to create a new instance of an object, and
that is automatic determined from the public properties of the object.
So say if I have the class
class State {
final String value;
final int length;
bool show;
}
Now the way to create an instance is simply:
var state = new State(value:"bla", length: 10, show: true);
If you want default values, just write:
class State {
final String value = "default";
final int length = 3;
bool show = false;
}
var state = new State(value:"bla");
So a final field, is only really final after the object is initialized, may
sound a bit strange, but I think it is the most sensible way of doing it,
and makes sure there is still just one good way of doing this.
And yes, every parameter is named, because it is good to explicitly state
which field you are setting.
So how do you say if a field could be null, or must be initialized. Well, I
propose the exact same as I do here:
https://groups.google.com/a/dartlang.org/forum/?fromgroups#!topic/misc/Lnj2sRQaITQ
NNBD would really help there, otherwise, you could do:
class State {
String value;
int length;
@required bool show;
}
But I would prefer instead:
class State {
String value;
int length;
bool? show;
}
Now if you want to do some calculation, side effects, error checking or
whatever before you initialize an object, I propose that you use just a
function for that. So there is just one way to `new` an instance. You can
not new it with a factory, because that just don't make sense. However, you
can implement this function of course as a static method of the class. And
I propose to have some syntax to write a default static method of a class.
For example:
class Point {
final num x;
final num y;
final num distanceFromOrigin;
static default(num x, num y) {
return new Point(
x: x, y: y, distanceFromOrigin: sqrt(pow(x,2) + pow(y, 2))
}
}
class Point {
final num x;
final num y;
final num distanceFromOrigin;
Point(x, y)
: x = x,
y = y,
distanceFromOrigin = sqrt(pow(x, 2) + pow(y, 2));
}
change for Dart 2.0, but I find this idea in general interesting for a
language and I would like to discuss it.
Heavily inspired by this
post: https://medium.com/@emilniklas/constructors-considered-harmful-17cccfab8d3#.3nzua8bed
I think I also heard Gilad Brach say something that the constructors in
Dart are a mess, and I agree.
I think there is just too much ways to do essentially the same way, and I
think we can do much better.
So I think instead of having named constructor, factory constructors,
initializer lists, etc. I think you should all remove it from the language.
And there should be just one way to create a new instance of an object, and
that is automatic determined from the public properties of the object.
So say if I have the class
class State {
final String value;
final int length;
bool show;
}
Now the way to create an instance is simply:
var state = new State(value:"bla", length: 10, show: true);
If you want default values, just write:
class State {
final String value = "default";
final int length = 3;
bool show = false;
}
var state = new State(value:"bla");
So a final field, is only really final after the object is initialized, may
sound a bit strange, but I think it is the most sensible way of doing it,
and makes sure there is still just one good way of doing this.
And yes, every parameter is named, because it is good to explicitly state
which field you are setting.
So how do you say if a field could be null, or must be initialized. Well, I
propose the exact same as I do here:
https://groups.google.com/a/dartlang.org/forum/?fromgroups#!topic/misc/Lnj2sRQaITQ
NNBD would really help there, otherwise, you could do:
class State {
String value;
int length;
@required bool show;
}
But I would prefer instead:
class State {
String value;
int length;
bool? show;
}
Now if you want to do some calculation, side effects, error checking or
whatever before you initialize an object, I propose that you use just a
function for that. So there is just one way to `new` an instance. You can
not new it with a factory, because that just don't make sense. However, you
can implement this function of course as a static method of the class. And
I propose to have some syntax to write a default static method of a class.
For example:
class Point {
final num x;
final num y;
final num distanceFromOrigin;
static default(num x, num y) {
return new Point(
x: x, y: y, distanceFromOrigin: sqrt(pow(x,2) + pow(y, 2))
}
}
class Point {
final num x;
final num y;
final num distanceFromOrigin;
Point(x, y)
: x = x,
y = y,
distanceFromOrigin = sqrt(pow(x, 2) + pow(y, 2));
}
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.