How is the term `syntax` used in naming classes or...
# arrow
j
How is the term
syntax
used in naming classes or packages in Arrow or apps that use Arrow? I've seen this pattern before, but I don't recall where. Is it used interchangeably with the term
ops
? Or maybe it was in Scala code...
s
syntax
refers to functions that can be derived from a given contract. As in “syntax” is something you get for free based on core functionality. I.e. when I have a
Monoid<A>
which provides me with an
empty
and
combine
function I can derive the syntax
combineAll
Copy code
fun <A> List<A>.combineAll(MA: Monoid<A>): A = fold(MA.empty()) { acc, a ->
   MA.run { acc.combine(a) }
}
In Scala this also called syntax, but can be provided more elegantly if you require
Monoid<A>
as an implicit.
Copy code
implicit class MonoidOps(val a: A): AnyVal {
   def combineAll(fa: List[A], implicit MA: Monoid[A]): A
}
What we can do however in Kotlin is project syntax. If you define
Monoid<Int>
we can generate an extension function:
fun List<Int>.combineAll(): Int = Int.monoid().run { this@combineAll.combineAll() }
j
Ah, all is illuminated! Thank you @simon.vergauwen.
And when you say "generate an extension function", are you referring to the
@extension
code generation of the Meta plugin?
s
Yes, that’s exactly what I was referring to 👍
👍 1