You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently there are various ways to chain operations, which are similar but not quite the same:
Functor like map function
Various getter methods
etc ...
Motivation: Rust
Here is similar example written in rust with postfix question mark operator ?. Rust does not fully support UFCS like Nim, for example for primitive type i32 we would write something like
traitSafeOps:Sized{fnsafe_div(self,other:Self) -> Result<Self,String>;}implSafeOpsfori32{fnsafe_div(self,other:Self) -> Result<Self,String>{if other == 0{Err(String::from("Division by zero"))}else{Ok(self / other)}}}fncalc() -> Result<i32,String>{8.safe_div(2)?.safe_div(0)?.safe_div(1)}fnmain(){println!("{:?}", calc());}
Further considerations
Doing similar for Option type in standard library
Generalizing Option and Result to some kind of Monads maybe somehow or not
The text was updated successfully, but these errors were encountered:
Generalizing Option and Result to some kind of Monads maybe somehow or not
type Opt[T] = Result[T, void] is something we use in a few places, with good results - it's possible to add a few helpers to make it an almost seamless replacement.
Proposal
Note: I'm new to
Nim
and not sure how to implement it nor is it even possibleSuppose we have function
safeDiv
Currently we have to write:
Since
Nim
supportsUFCS
, it would be a lot nicer to write something like:Currently there are various ways to chain operations, which are similar but not quite the same:
Functor
likemap
functionMotivation: Rust
Here is similar example written in rust with postfix question mark operator
?
.Rust
does not fully supportUFCS
likeNim
, for example for primitive typei32
we would write something likeFurther considerations
Option
type in standard libraryOption
andResult
to some kind ofMonads
maybe somehow or notThe text was updated successfully, but these errors were encountered: