4 ms·
It can using pure or return or if working with just Maybe specifically then Maybe is defined like so: data Maybe a = Just a | Nothing So to make an X a Maybe
by tobz619 2y ago
It can using pure or return or if working with just Maybe specifically then Maybe is defined like so:
data Maybe a = Just a | Nothing
So to make an X a Maybe X, you'd put a Just before a value of type X.
For example:
one :: Int
one = 1
mOne :: Maybe Int
mOne = Just one -- alternatively, pure one since our type signature tells us what pure should resolve to.
Reason we can do this is because Maybe is also an Applicative and a Monad and so implements pure and return which takes an ordinary value and wraps it up into an instance of what we want.
- cubefox 2y agoIsn't that explicit casting? Implicit casting would be automatically performed by the compiler without the need to (re)write any explicit code.
- ninkendo 2y agoSounds similar to how you need to do Some(x) when passing x to something expecting an Option in rust. Swift interestingly doesn’t require this, but only because Optionals are granted lots of extra syntax sugar in the language. It’s really wrapping it in .some(x) for you behind the scenes, but the compiler can figure this out on its own. This means that in swift, changing a function from f(T) to f(T?) (ie. f(Optional<T>)) is a source-compatible change, albeit not an ABI-compatible one.
- magicalhippo 2y ago> mOne = Just one I'd call that explicit casting. Implicit casting would be mOne = one Compiler already knows what "one" is, it could insert the "Just" itself, no? Possibly due to an operator defined on Maybe that does this transformation? That is, are there some technical reasons it doesn't? Or is it just (no pun inteded) a language choice?
- yakshaving_jgt 2y agoWhy would this be useful? Why do you want the types to change underneath you?
- cubefox 2y agoBetter question: Why would you want your call site code to break when your type signature gets changed in a way that doesn't necessitate breaking anything?
- yakshaving_jgt 2y agoBecause what you're asking for precludes the concept of mathematical guarantees. I'm not taking your question at face value, because you could be asking why call site code should break when the type signature generalises (which is a useful thing), but that's not what you're asking. It seems you're asking for code to be both null safe and not null safe simultaneously. Having a language just decide that it would like to change the types of the values flowing through a system is wild. It's one of the reasons that JavaScript is a trash fire.
- cubefox 2y agoYou are misunderstanding things.
- yakshaving_jgt 2y agoI’m certainly misunderstanding why so many people in this thread insist on speaking authoritatively on a topic they clearly know very little about.
- magicalhippo 2y agoBecause it otherwise forces the caller to have an extra explicit step that doesn't really contribute to anything. It's a trivial transform, and as such just gets in the way of what the code actually does. Of course with great power comes great responsibility, so it's a tool that should be used sparingly and deliberately. Now as mentioned I don't use Haskell, but that's why I like it in other languages. I asked as I was curious if there was something that prevented this in Haskell, beyond a design choice.
- 2y ago