Discussion:
[dart-misc] Proposal : Setters Using CTor Initialise Shortcut
Davy Mitchell
2015-10-23 15:29:11 UTC
Permalink
Hi All,

I was writing some setters for a class and wondered if the constructor
shortcut syntax would make sense.

For example:

set x(this._x) {
updatePos();
}

Instead of:

set x(int newX) {
_x = newX;
updatePos();
}


I like it but it is sugary :-) Does save a line when you have a number of
setters.

Thanks,
Davy

--
Diving Into Dart - http://divingintodart.blogspot.co.uk/
--
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

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
ravi teja
2015-10-23 18:18:46 UTC
Permalink
Sweet as sugar!

+1
Post by Davy Mitchell
Hi All,
I was writing some setters for a class and wondered if the constructor
shortcut syntax would make sense.
set x(this._x) {
updatePos();
}
set x(int newX) {
_x = newX;
updatePos();
}
I like it but it is sugary :-) Does save a line when you have a number of
setters.
Thanks,
Davy
--
Diving Into Dart - http://divingintodart.blogspot.co.uk/
--
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
To unsubscribe from this group and stop receiving emails from it, send an
--
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

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Filipe Morgado
2015-10-23 22:09:27 UTC
Permalink
+1

Generalized "this." function arguments would work pretty well.
--
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

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Matthew Butler
2015-10-26 14:00:49 UTC
Permalink
I can see the desire for that kind of sugar, however I think it's of
limited value in practice. Generally if you are using setters (and getters)
you chose to do so for some validation reasons. So you will not want to
assign the value immediately and in some cases perhaps not at all.

In your instance, you simply want to call a method when a value is updated,
similar to an observable pattern than strictly a setter. Personally, I
don't see a lot of use for it myself based on the code I generally write,
but that may be limited in contrast to others.

Matt
Post by Davy Mitchell
Hi All,
I was writing some setters for a class and wondered if the constructor
shortcut syntax would make sense.
set x(this._x) {
updatePos();
}
set x(int newX) {
_x = newX;
updatePos();
}
I like it but it is sugary :-) Does save a line when you have a number of
setters.
Thanks,
Davy
--
Diving Into Dart - http://divingintodart.blogspot.co.uk/
--
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

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Davy Mitchell
2015-10-26 17:34:02 UTC
Permalink
Thanks for the input guys - I am still 50/50 on it. On the one hand it is
great for one type of setter (observable), and no use for something like a
range check.

Cheers,
Davy
Post by Matthew Butler
I can see the desire for that kind of sugar, however I think it's of
limited value in practice. Generally if you are using setters (and getters)
you chose to do so for some validation reasons. So you will not want to
assign the value immediately and in some cases perhaps not at all.
In your instance, you simply want to call a method when a value is
updated, similar to an observable pattern than strictly a setter.
Personally, I don't see a lot of use for it myself based on the code I
generally write, but that may be limited in contrast to others.
Matt
Post by Davy Mitchell
Hi All,
I was writing some setters for a class and wondered if the constructor
shortcut syntax would make sense.
set x(this._x) {
updatePos();
}
set x(int newX) {
_x = newX;
updatePos();
}
I like it but it is sugary :-) Does save a line when you have a number of
setters.
Thanks,
Davy
--
Diving Into Dart - http://divingintodart.blogspot.co.uk/
--
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
To unsubscribe from this group and stop receiving emails from it, send an
--
--
Diving Into Dart - http://divingintodart.blogspot.co.uk/
--
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

To unsubscribe from this group and stop receiving emails from it, send an email to misc+***@dartlang.org.
Loading...