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

This is usually referred to as an "embedded DSL" - you have a DSL embedded in a normal programming language using its first class constructs.



Yep the original term DSL was for custom languages, the eventual introduction of using it for these kinds of literate APIs was done later. Using it in the original way unqualified is fine imo.


Meh. By this definition all libraries expose an "embedded DSL" — their API. I'm honestly not sure this is a useful definition.


Whether you like it or not; internal DSLs became a thing with Ruby back in the day. And these days things like Kotlin also lend themselves pretty well to creating internal DSLs. Java is not ideal for this. Kotlin and Ruby have a few syntax features that make it very easy.


What's the distinguishing difference between a regular library and an embedded DSL?


The difference is making an effort to expose the features of the library via a syntactically friendly way. Most Kotlin libraries indeed have nice APIs that are effectively mini DSLs.

If you need an example, Kotlin uses a nice internal DSL for HTML where you write things like

  Div {
     P { 
        +"Hello world"
     }
  }

This is valid Kotlin that happens to construct a dom tree. There is no magic going on here; just usage of a few syntax features of the language. Div and p here are normal functions that take a receiver block as the last parameter that receive an instance of the element they are creating. The + in front of the string is function with a header like this in the implementation of that element.

   operator fun String.unaryPlus()

The same code in Java would be a lot messier because of the lack of syntactical support for this. You basically end up with a lot of method chaining, semicolumns and their convoluted lamda syntax. The article has a few examples that would look a lot cleaner if you'd rewrite them in Kotlin.




Join us for AI Startup School this June 16-17 in San Francisco!

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

Search: