3 ms·
> I thought multiple return was more interesting in the context of multiple possible types: like a type in case of success and one in case of failure (Either in
by masklinn 7y ago
> I thought multiple return was more interesting in the context of multiple possible types: like a type in case of success and one in case of failure (Either in Haskell, Result in Kotlin and Rust).
That's not a "multiple return", that returns a single value which is either one type or the other. Although some languages do implement it via MRV by necessity (e.g. Common Lisp, Go, Erlang).
- cies 7y agoI dont understand the "proper use" for MRVs then. Okay we get nicer types, record types, to do them; but they are otherwise perfectly doable and with little ugly boilerplate using classes. The "either values", or sum types/ tagged unions I mentioned, are the thing that Java really lacks (and because of that the exception system got heavily abused IMHO).
- sullyj3 7y agoTechnically it's not even one type or another, eg in Rust, Some(1) and None are values of the same type, Option<i32>. What's different is the tag of the enum, or the data constructor, or whatever you want to call it.
- masklinn 7y agoThat’s the reification of it in many langage but at its core what you really want if option is T | (). And some langages do express it that way though mostly for historical reasons of not having sum types and / or being dynamically typed (and there have been rumblings in the past of making rust enum variants into proper types). Of course this is not the case for all enum types. But option / either / result, definitely.
- sullyj3 7y agoSure, namely typescript.