-
Notifications
You must be signed in to change notification settings - Fork 59
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
Constructor this.arg
syntax
#97
base: master
Are you sure you want to change the base?
Conversation
I like it and I think we actually discussed this idea on the dev Slack before, but I struggle to remember if we had any unclarities or something against it... |
I just found a discussion that this actually comes from Dart language, and also a link to a macro I wrote to implement similar thing: https://gist.github.com/nadako/273497023683e20f964ccd6ac0643422 |
How do we express this in the AST? An extra (nullable) flag on |
There's still a bit of redundancy, that I would quite like getting rid of. Consider a somewhat complex class, with members grouped by concerns: class Airplane {
var captain:Pilot;
var copilot:Pilot;
// ... and more crew related stuff
var thrust = .0;
var pitch = .0;
var roll = .0;
var yaw = .0;
var lat:Float;
var lang:Float;
// ... a dozen of functions to control the aircraft's rotation/position/acceleration
// ... radio
// ... baggage
// ... and so on ...
} But there is only one constructor. So one has to go back and forth between that and declarations to see which ones are initialized in it. In tink_lang you can for example write |
I like This also opens question, if we should assign |
Hmmm. Valid points. In the spirit of assigning arbitrary meanings to
Yeah, that's a good point. That should probably make In the end short hands and complex classes as the example I gave before are perhaps not the best fit, so maybe I'm pushing this in a somewhat useless direction ;) |
class People_ {
public function new( public final this.name: String, private var this.age: Int ){}
}
abstract People( People_ ){
public inline function child(){
return this.age < 16;
}
}
function main(){
var boy: People = { name: Oliver, age: 12 };
if( boy.child() == true ) trace( '$(boy.name) is child' );
} |
oops forgot the 'new'.. but may have over simplified from real haxe anyway. class People_ {
public function new( final name: String, public var age: Int ){}
} |
for lots of properties splitting by line would work fine. class People_ {
public function new(
final name: String
, public var age: Int ){}
} |
Kotlin allows the paramaters to be put in the class header
|
@nanjizal I'm not a fan of this. it trades clarity for brevity, adding a totally different spot for something as fundamental as field declaration to go into. it could introduce sneaky fields if you ever want to mixmatch between regular declaration and in-constructor declaration: class Person {
public var name: String;
public var age: Int;
public var friends: List<Person>;
public function new(this.name, age: Int, ?friends: List<Person>, var id: Int) {
this.age = age;
this.friends = friends ?? new List<Person>();
}
} |
The thing is, that's true with or without the new syntax. class Airplane {
var captain:Pilot;
var copilot:Pilot;
// ... and so on ...
public function new(captain:Pilot) {
this.captain = captain;
}
}
class Airplane {
final captain:Pilot;
var copilot:Null<Pilot>;
// ... and so on ...
} Now you know that Alternatively, just write comments. I'd argue that if you're writing a class that's big enough for this to be a problem, you can take responsibility for code clarity. Haxe doesn't need to force it, at least not at the expense of constructor usability. (More on that later.)
Agreed. If I was searching for a certain field, I would definitely not think to check the constructor. I do want shorter code, but not at the expense of clarity. So at this point I've spoken out both for and against clarity. Evidently this isn't a simple issue. Let's talk about that. I see this as a tug-of-war between brevity and clarity/usability.
Aside: code completion (and related tools) can improve clarity/usability. However, it isn't always available, so I'd argue we shouldn't rely too heavily on it.
|
My opinion: I prefer RblSb's original proposal over the others, but I find it hard to read and I'm not ready to endorse it. I feel like I'd have trouble justifying any of the proposals to a newbie Haxe programmer. They're all hard to decipher at first glance. Why are some of these arguments prefixed with
Edit: I take that back. If |
Rendered version