[swift-evolution] Brainstorming: New operator type - chaining

Chris Lattner clattner at apple.com
Sat Jan 30 23:20:49 CST 2016


On Jan 30, 2016, at 3:06 PM, Joseph Lord via swift-evolution <swift-evolution at swift.org> wrote:
> Use case 2 - Chaining on custom types
> 
> And this especially may not be feasible and I may be out of my depth describing the requirement but it feels like a more general case so at least worth airing):

I’m not certain I understand the problem you’re looking to solve here, but I think it would be great (though probably very low priority) to open up the functionality of the postfix optional chaining operator to other types.  This could allow its monadic binding (aka railroad short circuiting) behavior to be extended to other types like Result, a left-biased-either, or an arbitrary type defined by the user.  A natural approach would be to capture this behavior in an “chainable” protocol of some sort.

This is low priority because I don’t know a lot of killer applications of it, I’m motivated by my ivory tower desire to push compiler magic into the standard library.  It bugs me that optional has sugar and other behavior that other types can’t participate in.

-Chris

> 
> It would be good for code like the following to be possible (hopefully with a little more type inference).
> 
> let c:Either<Int, Error> = Either<String, Error>(.Left(“boo"))^^.count
> 
> Now the user’s definition of the operator would probably have to look something like this:
> 
> func ^^<A,B, C>(lhs: Either<A,B>)->(ChainValueResult<A, C, Either<C,B>>   {
>  	switch lhs {
>  	case .Left(let a):
>    		return .continue(a, { Either<C,B>(.Left($0) })
> 	 case .Right(let b):
>    		return .stop( Either<C, B>(.Right(b))
> 	}
> }
> 
> I couldn't find any significant discussion of optional chaining in the mailing list so far.
> 
> Joseph
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list