Function Overloading #782
Replies: 2 comments
-
Thank you for the detailed issue 🙏 Unfortunately, this is the intended behavior. Variants are not allowed as types (they can only be referenced as types in patterns). In your example, it's clear that the function only takes a variant as the parameter, but it's not feasible for bigger programs. In order to facilitate what you are asking for, we would need to implement function overloading. Most functional programming language do not implement function overloading because it usually has a runtime cost: basically we need to check at runtime which version of the function needs to run based on the parameters. What you wanted to do can be done using only the parent type (but it comes with a little boilerplate): fun processMoves(arr : Array(Modification)) : Array(Modification) {
for item of arr {
case item {
Move => { item | ... } // Update the item
=> item // Ignore other variants
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Since nothing there is nothing actionable, I'm converting it into a discussion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Code example causing compiler error
Issue
Function processMoves() expects an array of explicitly the Modification.Move variant, however the compiler simplifies the parameter to accept the whole type (accepting all variants), instead of specific variant of the type.
I believe this should be allowed because my workaround would be to create a struct for passing parameters and returning the result, which then I would have to map back to the ADT variant anyway, so there's more of manual code.
Alternatively, the function processMoves() should be errored first on the parameter type, i.e. it should be explicitly explained that ADT variants are not allowed as types. This option is less preferrable but it would specify the limits of the system.
Error
Sandbox
https://mint-lang.com/sandbox/hRoQ-3GqY5xNhg
Beta Was this translation helpful? Give feedback.
All reactions