Coercion improvements
Listing some ideas for improvements to coercion/subtyping.
- [ ] A variant can be coerced to a polyvariant if the polyvariant is closed and the runtime representation matches
- [ ] Investigate dicts - what can be coerced?
- [ ] Handle more coercion + unboxed variants. For example.
"hello" :> JSON.tand1. :> JSON.tworks, but not1 :> JSON.t,true :> JSON.tordict{"test": "hello"} :> JSON.t
A variant can be coerced to a polyvariant if the polyvariant is closed and the runtime representation matches
Not sure if this example falls under that, but it would be great if we could do the following:
let f = (v:s) => ()
type t = | Foo | Bar | Meh
let _ = f(Foo) // without writing `Foo :> string`
I don't think that example is complete...? What is s?
Oh, sorry s for string.
Ah right. So you essentially mean implicit coercion. No, that's something we've intentionally decided against. We want any type coercion to be explicit. There are many reasons:
- You might not actually want a type coercion in various contexts, since types have meaning depending on context
- It needs to be clear from the code where coercion is happening
- Coercion can come with some perf loss in the type checking if the types are complex, so doing it should be explicit
Got it, thanks!