3 ms·
> The compiler should just know that my `Int` should be a `Maybe Int` and cast it for me. The compiler should not "just" know it. It would know it because we t
by magicalhippo 2y ago
> The compiler should just know that my `Int` should be a `Maybe Int` and cast it for me.
The compiler should not "just" know it. It would know it because we told it how.
Consider a function that takes a float and returns a complex number. I then change the function to take a complex number ("Complex Float" in Haskell if I read the docs right), and returns a float.
I could then tell the compiler, by implementing an implicit cast operator, how to cast float to complex. The implicit part is then that the compiler tries it without me telling it to use the cast explicitly.
Then any code that worked with the old function should work perfectly fine using the modified function without modifications, since per definition the reals are contained in the complex numbers.
This is how I do it in several languages I've used.
- yakshaving_jgt 2y agoBut now you’re talking about something else aren’t you? Now you’re talking about generalising. You can generalise, for example, from Float to Floating a => a. But I don’t understand how the original Int to Maybe Int change could be sensible. How does that work?
- magicalhippo 2y agoThe exact same way? Or perhaps you could tell me, in which cases can an Int not be turned into a Maybe Int? Again, I don't know Haskell, so from the outside it looks like much the same as the float -> complex conversion.
- yakshaving_jgt 2y agoIt sounds like you need to learn about parametricity. https://www.well-typed.com/blog/2015/05/parametricity/ https://www.well-typed.com/blog/2015/05/parametricity/
- cubefox 2y agoIt doesn't sound like that to me. Could you not just answer his question?
- yakshaving_jgt 2y agoTo whom shall I send the invoice?