Discussion:
[dart-misc] type-safe builders implemented! (in Kotlin)
tatumizer-v0.2
2015-12-16 18:10:03 UTC
Permalink
Another day - another new language. Somebody brainwashed me into learning
Kotlin, so I spent a better part of the afternoon browsing the tutorial.
At first, noticed nothing unusual: after Swift, Kotlin looks like a bit
simplified clone (though it's not clear who cloned whom). Noted some
ugliness of array and map literals - not sure why they chose to replace the
natural notation [1,2,3] with intArrayOf(1,2,3), and for Maps, things get
really bizarre, but fine, we saw stranger things around.
Anyway, it has functions, and integrates with java smoothly, and that's all
I need to know. I was already about to leave to pursue other interests, but
at the last moment stumbled upon a section with nondescript title
"type-safe builders" (everybody these days boast having such builders, so
it didn't sound as such a big deal) - and there, I was in for a big
surprise! You can read the rest here:
https://kotlinlang.org/docs/reference/type-safe-builders.html

Again, not sure if it's Kotlin's patent, or they copied it from somewhere.
But the idea is a good one indeed.
--
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.
Daniel Joyce
2015-12-17 02:47:08 UTC
Permalink
You can do this in Scala as well. You could do it in dart but it wouldn't
be type safe currently.
Post by tatumizer-v0.2
Another day - another new language. Somebody brainwashed me into learning
Kotlin, so I spent a better part of the afternoon browsing the tutorial.
At first, noticed nothing unusual: after Swift, Kotlin looks like a bit
simplified clone (though it's not clear who cloned whom). Noted some
ugliness of array and map literals - not sure why they chose to replace the
natural notation [1,2,3] with intArrayOf(1,2,3), and for Maps, things get
really bizarre, but fine, we saw stranger things around.
Anyway, it has functions, and integrates with java smoothly, and that's
all I need to know. I was already about to leave to pursue other interests,
but at the last moment stumbled upon a section with nondescript title
"type-safe builders" (everybody these days boast having such builders, so
it didn't sound as such a big deal) - and there, I was in for a big
https://kotlinlang.org/docs/reference/type-safe-builders.html
Again, not sure if it's Kotlin's patent, or they copied it from somewhere.
But the idea is a good one indeed.
--
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
--
Daniel Joyce

The meek shall inherit the Earth, for the brave will be among the stars.
--
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.
Daniel Davidson
2015-12-17 11:19:33 UTC
Permalink
I like the style and if I understand it correctly I use a Dartish version
heavily in my dart codegen projects (e.g. ebisu_cpp C++ code generator
<https://www.dartdocs.org/documentation/ebisu_cpp/0.3.10/>).
Dart cascades are great for achieving the same effect.
Here is a fairly large example
<https://www.dartdocs.org/documentation/ebisu_cpp/0.3.10/ebisu_cpp.ebisu_cpp/ebisu_cpp.ebisu_cpp-library.html>
- yet still small relative to the real uses we create.
The plus side is it very convenient and you can have stream of
consciousness creation of stuff.
Post by tatumizer-v0.2
Another day - another new language. Somebody brainwashed me into learning
Kotlin, so I spent a better part of the afternoon browsing the tutorial.
At first, noticed nothing unusual: after Swift, Kotlin looks like a bit
simplified clone (though it's not clear who cloned whom). Noted some
ugliness of array and map literals - not sure why they chose to replace the
natural notation [1,2,3] with intArrayOf(1,2,3), and for Maps, things get
really bizarre, but fine, we saw stranger things around.
Anyway, it has functions, and integrates with java smoothly, and that's
all I need to know. I was already about to leave to pursue other interests,
but at the last moment stumbled upon a section with nondescript title
"type-safe builders" (everybody these days boast having such builders, so
it didn't sound as such a big deal) - and there, I was in for a big
https://kotlinlang.org/docs/reference/type-safe-builders.html
Again, not sure if it's Kotlin's patent, or they copied it from somewhere.
But the idea is a good one indeed.
--
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.
tatumizer-v0.2
2015-12-17 15:30:58 UTC
Permalink
@Daniel I:
I can't find anything like this in scala. The point is not builders per se
(every language has them), but creative use of extension methods, which
gives you builders for free, but it's much more general idea, not limited
to builders.
Please meditate over this definition from kotlin:
fun head(init: *Head.()* -> Unit) = initTag(Head(), init)

Are you sure scala has something like that? Please send a pointer if so.

@Daniel II:
No, cascade is not the same. Too much syntactic noise. But again, it's not
about builders. In kotlin, you get builders as a variant of more general,
beautiful concept: function you pass as parameter becomes a method (kind
of), it can directly access the fields of the object. Cascade looks like an
ad hoc feature (even a hack) in comparison (read the spec - cascade
definition is not very elegant).
--
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.
krupal shah
2015-12-17 16:27:35 UTC
Permalink
About the copying things, it is pretty much clear that Kotlin is open
source project since 2011. So, calling Kotlin as a clone of swift is bad.
Swift was announce in 2010 and open sourced in 2015. During these year, it
was very much like a stealth project. So, it is pretty clear who copied
whom. About the type safe builders: they were implemented during the early
years of open source project Kotlin. You can browse trough milestones and
code though. It was open since 2011.
Post by tatumizer-v0.2
I can't find anything like this in scala. The point is not builders per se
(every language has them), but creative use of extension methods, which
gives you builders for free, but it's much more general idea, not limited
to builders.
fun head(init: *Head.()* -> Unit) = initTag(Head(), init)
Are you sure scala has something like that? Please send a pointer if so.
No, cascade is not the same. Too much syntactic noise. But again, it's not
about builders. In kotlin, you get builders as a variant of more general,
beautiful concept: function you pass as parameter becomes a method (kind
of), it can directly access the fields of the object. Cascade looks like an
ad hoc feature (even a hack) in comparison (read the spec - cascade
definition is not very elegant).
--
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
--
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.
tatumizer-v0.2
2015-12-17 17:49:40 UTC
Permalink
@Krupal:
I have to apologize for my earlier statement. The thing is: I learned swift
one day before I learned kotlin, so the latter looked like a clone. Had the
order been reversed - sure, swift would look like a clone of kotlin instead.

Another theory is that both languages are created by the collective
unconscious, then no wonder they look similar.

In any case, the process of convergence continues: e.g. Swift recently
announced he is planning to get rid of C-style "for" loop, which Kotlin
eschewed right from the start.

If you know the history of Kotlin, please tell me who invented these
extension-based builders, I will send him a genius award
<http://www.amazon.com/Genius-Bud-Powell/dp/B0000046ZP>
--
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.
krupal shah
2015-12-18 16:13:17 UTC
Permalink
They were already implemented in Groovy and inspired from it.
Post by tatumizer-v0.2
I have to apologize for my earlier statement. The thing is: I learned
swift one day before I learned kotlin, so the latter looked like a clone.
Had the order been reversed - sure, swift would look like a clone of kotlin
instead.
Another theory is that both languages are created by the collective
unconscious, then no wonder they look similar.
In any case, the process of convergence continues: e.g. Swift recently
announced he is planning to get rid of C-style "for" loop, which Kotlin
eschewed right from the start.
If you know the history of Kotlin, please tell me who invented these
extension-based builders, I will send him a genius award
<http://www.amazon.com/Genius-Bud-Powell/dp/B0000046ZP>
--
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
--
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.
krupal shah
2015-12-18 16:19:19 UTC
Permalink
I don't know the exact history though. You can see this post :
https://confluence.jetbrains.com/plugins/servlet/mobile#content/view/40701005
Post by krupal shah
They were already implemented in Groovy and inspired from it.
Post by tatumizer-v0.2
I have to apologize for my earlier statement. The thing is: I learned
swift one day before I learned kotlin, so the latter looked like a clone.
Had the order been reversed - sure, swift would look like a clone of kotlin
instead.
Another theory is that both languages are created by the collective
unconscious, then no wonder they look similar.
In any case, the process of convergence continues: e.g. Swift recently
announced he is planning to get rid of C-style "for" loop, which Kotlin
eschewed right from the start.
If you know the history of Kotlin, please tell me who invented these
extension-based builders, I will send him a genius award
<http://www.amazon.com/Genius-Bud-Powell/dp/B0000046ZP>
--
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
--
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.
tatumizer-v0.2
2015-12-18 19:57:56 UTC
Permalink
groovy has builders, and there is more than one way to implement them(*),
but nothing I found is based on extension methods. But the beauty of
Kotlin's builders is in the way HOW it's done. The thread you linked to
doesn't indicate who came up with this idea, and whether it's originated in
Kotlin or elsewhere.
--
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.
Daniel Joyce
2015-12-20 18:52:12 UTC
Permalink
You can create extension methods in Scala as well. Nothing special about
that. In scala you can use case classes, implicit convesions, object
instances, and other tools to define typesafe dsls. Its done all the time.
Post by tatumizer-v0.2
groovy has builders, and there is more than one way to implement them(*),
but nothing I found is based on extension methods. But the beauty of
Kotlin's builders is in the way HOW it's done. The thread you linked to
doesn't indicate who came up with this idea, and whether it's originated in
Kotlin or elsewhere.
--
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
--
Daniel Joyce

The meek shall inherit the Earth, for the brave will be among the stars.
--
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.
Ladislav Thon
2015-12-20 19:07:41 UTC
Permalink
Hi,

so this is really interesting. What they did in Kotlin was that they took the key building block of Groovy builders, i.e. the ability to set a delegate for a closure, and figured out how to do it in a type-safe way.

It's called "extension lambdas" and as the name suggests, it's a combination of lambdas and extension methods (as known e.g. from C#). To the best of my knowledge, this combination is novel, but I might easily be wrong here.

In Groovy, you can set up a particular instance of a closure (their name for lambdas) not to lookup free variables in the lexical environment, but instead on a particular object that you supply. In combination with noSuchMethod and noSuchProperty, it's really powerful, but of course unsafe. In Kotlin, you can instead give lambdas such type that they are, in a small scope, considered extension methods of given class. That allows the body of the lambda to refer to those class's members via "this". And the "this" keyword is usually optional (which is typical). That's of course constrained (e.g. can't generate arbitrary XML structures, only predefined ones), but out gives a very similar feeling to Groovy while being completely safe.

And, as noted, cascades don't even qualify for comparison. See e.g. here:

https://github.com/Ladicek/kotlin-talk-examples/blob/master/src/_10builders/main.kt#L37

(Not sure if it still compiles, it was written against an older Kotlin milestone, but the general idea still applies perfectly.)

Note that inside the lambda, I can use normal control flow structures such as loops or conditions. With cascades and all other typical builder styles, that's impossible.

LT
--
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.
tatumizer-v0.2
2015-12-21 00:01:40 UTC
Permalink
Ladislav: glad to see you here, thanks for explanations - the keyword is
indeed "extension lambdas" (I didn't know this term).
Too bad no one else on this board seem to appreciate the idea or even find
out what it is. O tempora! O mores! :(
.
--
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.
Don Olmstead
2015-12-21 00:21:49 UTC
Permalink
Its the holidays so I would expect the chatter to be low. I wouldn't take
offense. My question is also languishing.
Post by tatumizer-v0.2
Ladislav: glad to see you here, thanks for explanations - the keyword is
indeed "extension lambdas" (I didn't know this term).
Too bad no one else on this board seem to appreciate the idea or even find
out what it is. O tempora! O mores! :(
.
--
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
--
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.
Alex Tatumizer
2016-01-06 22:17:17 UTC
Permalink
In case someone is wondering, I got an answer to "Who invented extension
lambdas?".
https://discuss.kotlinlang.org/t/who-came-up-with-idea-of-builders-via-extension-methods/1370
--
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.
Loading...