Hacker News new | past | comments | ask | show | jobs | submit login

For some people, after looking at Scala a bit, being "watered" might be a good thing. Kotlin doesn't have as many syntax surprises as Scala does.

That being said, the one really cool feature Kotlin has that Scala doesn't is the ability to build strongly-typed builder DSLs:

http://confluence.jetbrains.net/display/Kotlin/Type-safe+Gro...

You simply can't accomplish this in Scala with the same elegance.




That Kotlin page calls them "Groovy-style", perhaps because many of those working on Kotlin (e.g. James Strachan, Alex Tkachman) used to develop the Groovy Language.

But doesn't Ruby also have those builders? Wouldn't it be easy enough for Scala to implement them in a library if they haven't already? Wouldn't Python also provide this type of syntax? E.g. a possible translation into Python from the Kotlin code on the linked page...

    html:
      head:
        title {"XML encoding with Python"}
      body:
        h1 {"XML encoding with Python"}
        p {"this format can be used as an alternative markup to XML"}
 
        // an element with attributes and text content 
        a(href = "http://python.org") {"Python"}

        // mixed content 
        p:
          "This is some" 
          b {"mixed"} 
          "text. For more see the" 
          a(href = "http://python.org") {"Python"} 
          "project" 
        p{"some text"} 
 
        // content generated by 
        p: 
          for (arg in args) arg 
So what are they called "Groovy-style" builders for?


I program in Groovy daily. "Groovy-style" builders are just sugar over the Builder pattern via heavy use of closures - so in reality they can be used anywhere an Object needs to be constructed.

So yea, any language which supports closures should be able to do "Groovy-style" builders.


Presumably because, among the languages (and standard libraries) you mentioned that have implemented this pattern, Groovy is the language that is most tightly associated with the JVM (which Kotlin is obviously targeting).


> Groovy is the language that is most tightly associated with the JVM

That could've been very true 6 years ago, when Groovy 1.0 was released, but I'd have to challenge that for 2012 (and 2013).


What exactly is special about that DSL? I'm pressed for time, but it seems like it would be trivial to reproduce in Scala.


In Kotlin, you can write a function that takes, as its last argument, a function literal that is scoped to a specific class. Last I checked, function scoping does not work this way in Scala and you'd have to explicitly pass around objects to get the same result.

In the end, we end up with a builder DSL like this:

  div() {
      h1("header")
      p("paragraph")
      div() {
          p("another paragraph")
      }
      h2("another header")
  }
In Kotlin, each function call is properly scoped inside (unseen) classes for each level, allowing the code to generate the appropriate object tree.

In Scala, all functions would have to be visible in the local scope of the top-level function and we'd end up with a flat object tree instead.

To get the proper object tree in Scala, you'd need to pass explicit references to each containing object into the appropriate function literals, which makes it much more verbose and extremely error prone.

EDIT: not nearly as convincing without proper formatting...


That's not very hard to do with Scala. In fact, there's a few projects that have implemented whole other languages in Scala. Most languages that let you go near functional style do very well with DSLs and writing a DSL is one of the often highlighted strengths of Scala.

For a good example check out the Specs2 unit testing framework.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: