Another option is Exceptions. The function either does what it's supposed to, or freaks out. You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException. It's still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null. As an aside, I prefer the "Unless" mod…
Maybe Functions
11–20 of 103 posts
Re: Maybe Functions
#12As Sandy Metz is used to say « Nothing is Something »[0]
Re: Maybe Functions
#13 function maybeGetUser(): User | null {
if (!loggedIn) {
return null;
}
return fetchUser();
}
I believe this is an error. The code sample I took from the article is about getting a user, not the user's friends.Since that function will return a list, an empty List might work.
Re: Maybe Functions
#14The "maybe" style has the inconsistency embedded in the type system; it's impossible to have an invocation to getFriends and then not handle the resulting possibility of not being logged in.
Shifting it up to the caller just means that you're going to have to remember to ensure the user is logged in before calling getFriends otherwise you'll get some kind of error, which might give you more control, but now there's no guarantee in the type system that you've handled the case where the user isn't logged in.
Writing ifs everywhere to handle failure conditions might be a bit of a pain, but that's more of a failing of the language than the style.
Re: Maybe Functions
#15Another option is Exceptions. The function either does what it's supposed to, or freaks out. You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException. It's still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null. As an aside, I prefer the "Unless" mod…
Re: Maybe Functions
#16Another option is Exceptions. The function either does what it's supposed to, or freaks out. You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException. It's still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null. As an aside, I prefer the "Unless" mod…
I think that's true for checked exceptions; in Typescript, I'd rather see that a function may return a null, rather than get surprised by a possible exception that's not telegraphed.
Re: Maybe Functions
#17Another option is Exceptions. The function either does what it's supposed to, or freaks out. You can remove the null checks and the software will raise a null pointer exception. In the first example, could raise a NotLoggedInException. It's still a maybe function, but you have a mechanism for expressing the why-notness of the function run, as opposed to returning a generic null. As an aside, I prefer the "Unless" mod…
I am for exceptions but it should not be used for basic control flow. Many techs will treat all exceptions as errors.
Re: Maybe Functions
#18Re: Maybe Functions
#19Re: Maybe Functions
#20What's the advantage of the Monad approach? Doesn't the render function still have to check whether those Maybes contain values or not?
If you have multiple steps then the advantage is that you never have to unpack "in the middle" and you don't have to care - and the compiler has your back.
Classical example: show the street number of the user or show if there is no street number. There can be multiple things missing on the way and multiple transformations might happen on the way. E.g. the user might not even have an adress saved alltogether.
In that case, you only have to "check whether those Maybes contain values or not" once at the very end.