-
Notifications
You must be signed in to change notification settings - Fork 0
Multi dispatch
Jonathan edited this page Aug 8, 2021
·
2 revisions
Firefly supports multi-dispatch through @MultiDispatch annotation. By annotation functions with this annotation, you inform the compiler to generate switch-case branches, which selects the correct function to handle the invocation, for example:
@MultiDispatch
fun print(o: Any) {
println("Printing any: $o")
}
@MultiDispatch
fun print(o: String) {
println("Printing string: $o")
}
@MultiDispatch
fun print(o: List<Int>) {
println("Printing List<Int>: $o")
}
fun main(args) {
val str: Any = "A"
val int: Any = 9
val intList = List<Int>(0, 1, 2)
print(str) // Printing string: A
print(int) // Printing any: 9
print(intList) // Printing List<Int>: [0, 1, 2]
}
Compiler does that by generating switch-case branches inside the annotated function, but can use invokedynamic for this kind of invocation when --multi-dispatch-with-invoke-dynamic=true is set.