In C ++ 17 std::optional, I was pleased with this solution until I looked at ref. I know Optional/ Maybefrom Scala, Haskell, and Java 8, where optional is a monad and follows monadic laws. This does not apply to the C ++ implementation 17. How should I use std::optional, without features such as mapand flatMap/ bind, what is the benefit of using std::optionalvs, for example, return -1or nullptrof the function if it can not calculate the result? And more importantly for me, why wasn’t it std::optionalintended for the monad, is there a reason?
std::optional
Optional
Maybe
map
flatMap
bind
-1
nullptr
std:: optional, , map flatMap/bind
Maybe Haskell fmap, , . , .
fmap
std:: optional vs, , -1 nullptr , ?
, ? 0, -1, MAX_INT, nullptr - ? unsigned int, int int, -1, MAX_INT, ? std::optional .
0
MAX_INT
unsigned int
int
, std:: optional, , ?
++ ? , , .
bind return std::optional, .
return
, bind
template<typename T1, typename T2> std::optional<T2> bind(std::optional<T1> a, std::function< std::optional<T2>(T1)> f) { if(a.has_value()) return f(a.value()); return std::optional<T2>{}; }
, , .
, , - , , .
Source: https://habr.com/ru/post/1657522/More articles:Overflow error in Excel VBA with double type - excel-vbaRetrieving Unique Values from a Data Frame Using R - rWhy does __slots__ = ('__dict__',) create smaller instances? - pythonAn idiomatic way to parse a file in Scala - scalaMysql sum for two dates for different and missing and new - phpzip error - do nothing - bashПолучите количество пройденных и неудачных тестов с их именем из командной строки - mavenAngular 2: installing and removing custom pipes? - angularIonic Modal Slider problem: could not find the corresponding element with delegate descriptor = on $ emit - javascriptSimple comma in ASP.NET session value - javascriptAll Articles