"this" shorthand for constructor?

128 views
Skip to first unread message

Randal L. Schwartz

unread,
Oct 24, 2014, 4:31:57 PM10/24/14
to mi...@dartlang.org

It seems a bit redundant to write:


class SomeLongName extends SomeOtherName {

SomeLongName(this.foo, that.bar);

}


Is there a chance that maybe someday we can just say:

class SomeLongName extends SomeOtherName {

this(this.foo, that.bar);

}

Similar to how constructor redirects work with this(....) on the right
side?


--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix consulting, Technical writing, Comedy, etc. etc.
Still trying to think of something clever for the fourth line of this .sig

Gilad Bracha

unread,
Oct 24, 2014, 4:37:34 PM10/24/14
to mi...@dartlang.org
It is certainly possible. However, I have to wonder whether this sacrifices readability for concision. It's a delicate balance. It also might complicate error reporting if someone forgets a closing brace. So I would not make it a priority.

--
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+uns...@dartlang.org.

Enrique Gavidia

unread,
Oct 24, 2014, 6:01:24 PM10/24/14
to mi...@dartlang.org
I think that might be overloading 'this' a bit, and kinda gives the impression that 'this()' can be called within the class like a function potentially. I definitely agree that long constructor names can be an issue though, but I'd advocate for more of the python/ruby/typescript approach and use 'init'/'initialize'/'constructor' as a fixed way to declare constructors. That approach has always felt more intuitive to me than the java style, personally.

--
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+uns...@dartlang.org.

Rich Eakin

unread,
Oct 24, 2014, 6:29:58 PM10/24/14
to mi...@dartlang.org
On Sat, Oct 25, 2014 at 12:00 AM, Enrique Gavidia <obli...@gmail.com> wrote:
I think that might be overloading 'this' a bit, and kinda gives the impression that 'this()' can be called within the class like a function potentially. I definitely agree that long constructor names can be an issue though, but I'd advocate for more of the python/ruby/typescript approach and use 'init'/'initialize'/'constructor' as a fixed way to declare constructors. That approach has always felt more intuitive to me than the java style, personally.


I would agree that picking a new keywork like 'init', 'constructor', etc is the best of both words, rather than overloading' this' for something else.

And it seems somewhat of value that if 'constructor' were adopted, it would eventually become second nature to people once they are familiar with ECMAScript 6 classes.

Randal L. Schwartz

unread,
Oct 25, 2014, 11:30:52 AM10/25/14
to mi...@dartlang.org
>>>>> "Gilad" == 'Gilad Bracha' via Dart Misc <mi...@dartlang.org> writes:

Gilad> It is certainly possible. However, I have to wonder whether this
Gilad> sacrifices readability for concision. It's a delicate balance. It
Gilad> also might complicate error reporting if someone forgets a
Gilad> closing brace. So I would not make it a priority.

Yes, that's well-reasoned. Thank you for your response.

Günter Zöchbauer

unread,
Oct 25, 2014, 11:44:18 AM10/25/14
to mi...@dartlang.org
I also think that there is room for improvement. Currently when you rename the class (manually, not refactoring feature) a constructor unintentionally becomes a method. Some specific keyword instead of the class name would solve this source of error.
I find 'constructor' rather long. What about 'new' this is already a keyword? - and drop it on the call side ;-) 

kc

unread,
Oct 25, 2014, 1:30:26 PM10/25/14
to mi...@dartlang.org
Familiarity with Java and C# and now post 1.0.

Jacob Goodson

unread,
Oct 26, 2014, 1:41:30 AM10/26/14
to mi...@dartlang.org, mer...@stonehenge.com
"This" seems like an irrelevant request,  I suggest using a code template(or snippet or whatever your favorite ide calls it).

James Gray

unread,
Nov 2, 2014, 10:35:30 AM11/2/14
to mi...@dartlang.org, mer...@stonehenge.com
That doesn't solve the problem of needing to change multiple things when you rename a class.
Reply all
Reply to author
Forward
0 new messages