Type-safe Builders and LaTeX


#1

Dear Community,

to get a first impression of Kotlin I’ve started a small project to create a DSL for
the creation of LaTeX-files. I’ve worked my way through the html-example in the
documentation but I am stuck at one point: Is it possible to generate an expression
like frac{numerator}{denominator} ? I tried the following which should

  • at least in theory - show the wanted behaviour:
fun frac(numerator: () -> Unit) : (denominator: () -> Unit) -> Unit
{
    ...
}

fun main(args : Array<String>)
{
  frac{}{}
}


Kotlin gives me this error:

“Only one function literal is allowed outside a parenthesized argument list”

Any comments/ideas are appreciated!

Alexander


#2

Exactly this syntax is impossible due to several ambiguities, but you can use infix functions like this:

 
class Fraction
class Numerator {
    fun by(d: () -> Unit) : Fraction {}
}

fun div(n: () -> Unit) : Numerator = Numerator()

fun fn()
{
  div {  } by { }
}


#3

Thanks for the quick answer! I already thought my way might not be possible but then again sometimes things are easy and you just can't see them. ;)