Hi! I’ve been following a tutorial on how to creat...
# getting-started
d
Hi! I’ve been following a tutorial on how to create a mini compiler, but the tutorial is in JavaScript. I’m trying to convert it to Kotlin, and I’ve ran into the following problem (detailed in the thread) and I’m not sure how to solve it.
What’s the Kotlin equivalent of this anonymous JS object? I’m asking about the
NumberLiteral
and
CallExpression
, they are functions, one only has
node
as parameter and the other one as both
node
and
parent
. Would this be a sealed class? Or just a class with these two functions?
j
You could create an interface with these 2 functions, and make
traverse
accept that interface as second argument. Then you can either create a class implementing that interface, or create an anonymous implementation of the interface on the spot, with the syntax
object : TheInterface { ... }
where you can define the functions in the
...
part
Another option is to make
traverse
accept 2 function arguments instead of that one object. And then you can pass lambdas for those. But I'd rather go the typed way
d
Ohhh yeah, that makes sense! The first approach seems a bit easier, I’ll try implementing that. Thank you Joffrey!