<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On 26 Jan 2016, at 09:52, Andrew Bennett via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">It seems like the argument comes down to:<div class=""> * An either-like type should be named something meaningful for the context it is used</div><div class=""> * There are a lot of cases where an either-like type is useful, there may generalised functions that are often used on them</div><div class=""> * Is defining or re-defining an either type hard?</div><div class=""> * Is it too niche to be in the standard library</div><div class=""><br class=""></div><div class="">Personally I think defining an either type is not hard:</div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">enum</span> Result<T,E: ErrorType> {</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> <span style="color:rgb(187,44,162)" class="">case</span> Value(T), Error(E)</div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""> }</div></div><div class=""><br class=""></div><div class="">I think it may be too niche to be in the standard library, from the "meaningful context" perspective.</div><div class=""><br class=""></div><div class="">Perhaps what we want is not an either type, but an easy to conform to protocol that allows some reuse with Either-style types. Possibly with some compiler magic to make it easy for enums to conform to it...</div><div class=""><br class=""></div><div class="">I've thrown an example protocol together here:</div><div class=""> <a href="https://github.com/therealbnut/Either" class="">https://github.com/therealbnut/Either</a></div><div class=""><br class=""></div></div>
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""><div class="">Personally I agree that this is pretty niche these days; if I want to return either a result or an error, then I now have the option of throwing an exception, and this in my experience was the most common case where something like this was needed.</div><div class=""><br class=""></div><div class="">If the aim is to allow a function to return one of several possible types, then the question is… why only two? If we were looking at allowing several possible return types then what I’d really rather see is some kind of compiler magic that supports that, so I could do stuff like:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func myFunc() -> Int, Double, String { … }</font></div><div class=""><br class=""></div><div class="">This could then create an implicit enum that I could then check via cases like:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .Double(value): doSomething(value)</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .Int(value): doSomethingElse(value)</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>case .String(value): doSomethingStringy(value)</font></div><div class=""><br class=""></div><div class="">There could also be some kind of naming syntax, to enable returning one of two possible values of the same type, but with different meanings, otherwise the case would be implicitly named for the type of data that it holds.</div><div class=""><br class=""></div><div class="">Otherwise… I think use of Either is too specialised, and too easily done yourself with a name that’s more specific to your use-case.</div></body></html>