[swift-evolution] Optional dictionary subscripts

Brent Royal-Gordon brent at architechies.com
Wed Dec 23 03:21:56 CST 2015


> I think this would be the standard syntax:
> 
> if let value = key.map({ dict[$0] }) {
> 
> }

In this case, I think you actually want a flatMap, to merge the two `nil` cases instead of nesting them.

	if let value = key.flatMap({ dict[$0] }) {
		
	}

I wish that could be this, though:

	if let value = key.flatMap(dict.subscript) {
		
	}

In any case, I think two different approaches to do this (chained `if let` and `flatMap`) are probably enough; we don't need a third.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list