[swift-evolution] Brainstorming: Optional sugar inferred map

Craig Cruden ccruden at novafore.com
Fri Jan 29 17:15:18 CST 2016


I would think it would be functionally equivalent to Scala.

for {
   o1 <- optional1
   o2 <- optional2
   ..
} yield { manager.doSumething(data, o1, o2) }

which is a combination of maps and flatMaps.

> On 2016-01-30, at 1:06:00, Jacob Bandes-Storch <jtbandes at gmail.com> wrote:
> 
> Another conundrum: are the optional-pattern arguments evaluated before any other arguments? After?
> On Fri, Jan 29, 2016 at 9:54 AM Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
> Yeah I was thinking about that too, multiple optionals. It gets a bit confusing as to the scope.  
> 
> Wondering if we should pass it on as an optional or not allow it? Something else?
> 
> Sent from my iPhone
> 
> On Jan 29, 2016, at 9:26 AM, Jacob Bandes-Storch <jtbandes at gmail.com <mailto:jtbandes at gmail.com>> wrote:
> 
>> Hi Paul,
>> I think the two statements would behave separately (as if you had used map twice).
>> 
>> If you want them to act together, you could do this: 
>> 
>> { manager.doSomething(data: data, count: $0); doSomethingElse($0) }(n?)
>> 
>> Another aspect worth considering is whether this syntax should work with multiple optionals used in the same expression. (e.g. if they are all non-nil, the expression is evaluated.)
>> On Fri, Jan 29, 2016 at 1:53 AM Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>> Jacob,
>> 
>> While working on proposal, I am just trying to understand one thing with this. In your second example using map, it is clear where the block of code begins. How would you deal with multiple statements or ones that returned no value:
>> 
>>     	manager.doSomething(data: data, count: n?); doSomethingElse( n?)
>> 
>> the equivalent with map is
>> 
>> 	n.map { manager.doSomething(data: data, count: $0); doSomethingElse($0)  }
>> 
>> Thanks,
>> - Paul
>> 
>>> On Jan 28, 2016, at 8:41 PM, Jacob Bandes-Storch <jtbandes at gmail.com <mailto:jtbandes at gmail.com>> wrote:
>>> 
>>> That'd be the point. If doSomething were not optional, when "n?" appears in it, it becomes optional (it's basically optional chaining, but for function calls and other expressions instead of just dot-notation).
>>> On Thu, Jan 28, 2016 at 8:38 PM Craig Cruden <ccruden at novafore.com <mailto:ccruden at novafore.com>> wrote:
>>> 
>>>>     manager.doSomething(data: data, count: n?)
>>> 
>>> What if the return value of doSomething is not an optional?  Expressions are easy — but there might be some conflicts with this one.
>>> 
>>> 
>>>> On 2016-01-28, at 14:34:58, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>> I've wanted this sort of thing a lot. It would also work for other functions, such as
>>>> 
>>>>     manager.doSomething(data: data, count: n?)
>>>> 
>>>> which is equivalent to
>>>> 
>>>>     n.map { manager.doSomething(data: data, count: $0) }
>>>> 
>>>> It might be hard to see exactly which operator/function applications such a thing applies to, if used in the context of a complex expression.
>>>> 
>>>> Jacob
>>>> 
>>>> On Wed, Jan 27, 2016 at 9:50 PM, Paul Ossenbruggen via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> Maybe something like this?
>>>> 
>>>> let n : Int? = 5
>>>> 
>>>> let r = n? + 5
>>>> 
>>>> 
>>>>> On Jan 27, 2016, at 9:46 PM, Thorsten Seitz <tseitz42 at icloud.com <mailto:tseitz42 at icloud.com>> wrote:
>>>>> 
>>>>> Too much hidden magic IMO. This would mean loosing the benefits of optionals, i.e. making explicit where optional cases occur and that handling the missing case has to be considered. 
>>>>> 
>>>>> -Thorsten 
>>>>> 
>>>>> Am 28.01.2016 um 06:30 schrieb Craig Cruden via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>>>>> 
>>>>>> Yes
>>>>>>> On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen at gmail.com <mailto:possen at gmail.com>> wrote:
>>>>>>> 
>>>>>>> Trying to see if I got this. So the type of r would be Int? at the end of this? And if n were nil then r would be nil? Otherwise it r is 10?
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>>> On Jan 27, 2016, at 9:19 PM, Craig Cruden via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>>>> 
>>>>>>>> Swift currently encourages a lot of conditional code - especially when it comes to optionals.  In most cases when you are computing etc. on an Optional you would expect that you would want an optional result and things to be able to use optionals.  
>>>>>>>> 
>>>>>>>> In another language I generally just `map` one optional to another - which may not be the most readable code to some not use to optionals.  
>>>>>>>> 
>>>>>>>> I was wondering if maybe an expression is not available that it would rewrite the syntax to map from one to another value.  
>>>>>>>> 
>>>>>>>> So things like:
>>>>>>>> 
>>>>>>>> let n : Int? = 5
>>>>>>>> 
>>>>>>>> let r = n + 5
>>>>>>>> 
>>>>>>>> would actually compile as 
>>>>>>>> 
>>>>>>>> let r = n.map {$0 + 5}
>>>>>>>> _______________________________________________
>>>>>>>> swift-evolution mailing list
>>>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>>>>> 
>>>>>> 
>>>>>> _______________________________________________
>>>>>> swift-evolution mailing list
>>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>> 
>>>> 
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
>> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160130/42ec212a/attachment.html>


More information about the swift-evolution mailing list