I am Software Developer, currently interested in static type languages (TypeScript, Elm, ReScript) mostly in the frontend land, but working actively in Python also. I am available for mentoring.
Using Maybe like that has no sense. If you will use nullable (value | null) then withDefault is just x ?? 0 what means if x is null then fallback to 0. There is no value in Maybe here, only additional abstraction instead of idiomatic construct.
Maybe starts to have sense when you totally work with it in functional way. When u use maps, binds and ap.
If you write statements (and if is a statement) then I would say you don't need Maybe, Nullable is idiomatic and fully ok. If you use TS then compiler will inform you when you need to check if value is there, so win win. Additionally you don't loose latest added feature of optional chaining which works with null values.
But if you go fully functional then many things become harder with idiomatic JS, and then such constructs has a sense.
If we would type arguments as numbers then TS would automatically say the function returns number | null and dev would be enforce to or check the value or set the fallback
Very good point! Could you please describe when using Maybe make sens in JavaScript/TypeScript?
And also do you know some libraries that provides Maybe types?
I am Software Developer, currently interested in static type languages (TypeScript, Elm, ReScript) mostly in the frontend land, but working actively in Python also. I am available for mentoring.
I am Software Developer, currently interested in static type languages (TypeScript, Elm, ReScript) mostly in the frontend land, but working actively in Python also. I am available for mentoring.
Using Maybe like that has no sense. If you will use nullable (value | null) then withDefault is just
x ?? 0
what means ifx
is null then fallback to 0. There is no value in Maybe here, only additional abstraction instead of idiomatic construct.Maybe starts to have sense when you totally work with it in functional way. When u use maps, binds and ap.
If you write statements (and
if
is a statement) then I would say you don't need Maybe, Nullable is idiomatic and fully ok. If you use TS then compiler will inform you when you need to check if value is there, so win win. Additionally you don't loose latest added feature of optional chaining which works with null values.But if you go fully functional then many things become harder with idiomatic JS, and then such constructs has a sense.
Below using Nullable.
If we would type arguments as numbers then TS would automatically say the function returns
number | null
and dev would be enforce to or check the value or set the fallbackVery good point! Could you please describe when using Maybe make sens in JavaScript/TypeScript?
And also do you know some libraries that provides Maybe types?
Will write the whole article about that. So watch out for it :)
And here is the post - t.co/6yHektpzjj?amp=1