Brainstorming: Optional sugar inferred map


(Craig Cruden) #1

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}


(Paul Ossenbruggen) #2

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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Craig Cruden) #3

Yes

···

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Paul Ossenbruggen) #4

Maybe something like this?

let n : Int? = 5

let r = n? + 5

···

On Jan 27, 2016, at 9:46 PM, Thorsten Seitz <tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #5

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@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@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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Robert S Mozayeni) #6

+1 for Paul and Jacob’s ideas.

I’m not a fan of the original example, because then you could just have one optional variable or constant that just cascades through your code and gives everything an inferred Optional type, and then you lose the power of optionals because the compiler doesn’t make you check for them. Here’s an example for where that could go awry:

Let's say you're writing code that takes a String from user input somewhere and parses it into an Int.

let numCaloriesJustEaten = Int(userInputString) //Let's say userInputString is `two`. Type inferred as `Int?`, and would have value of nil
dict["caloricConsumption"] = numCaloriesJustEaten + (dict["caloricConsumption"] ?? 0)

The intention of this code would be to increment caloricConsumption by the recently-eaten calories. In Swift, this (rightfully) wouldn’t compile. If Swift operated according to the original example, then the whole expression of `numCaloriesJustEaten + (dict["caloricConsumption"] ?? 0)` would compile, and would have a value of nil at runtime.
This would remove the value of `dict["caloricConsumption”]` because when you set a the value of a key to nil, it removes that key from the dictionary entirely. This would be a logic error that is currently caught as a compile-time error in Swift today.

-Robert

···

On Jan 28, 2016, at 2:34 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@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@swift.org <mailto:swift-evolution@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@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Thorsten Seitz) #7

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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Craig Cruden) #8

Something like that would be good.

···

On 2016-01-28, at 12:50:50, Paul Ossenbruggen <possen@gmail.com> wrote:

Maybe something like this?

let n : Int? = 5

let r = n? + 5

On Jan 27, 2016, at 9:46 PM, Thorsten Seitz <tseitz42@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Howard Lovatt) #9

Assuming Paul is correct then you require Optional to be a sequence type
and you would use flatMap. I believe there has been such discussions in the
past but they don't seem to have gone anywhere. Java and Scala both do what
you want with their sequence types, maybe Swift will follow :).

···

On 28 January 2016 at 16:30, Craig Cruden via swift-evolution < swift-evolution@swift.org> wrote:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
  -- Howard.


(Craig Cruden) #10

    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@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@swift.org <mailto:swift-evolution@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@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Joe Groff) #11

Yes, this would be a major problem with this sort of feature. Without some explicit delimiter for the bounds of the map operation, you have either extremely subtle rules for defining the implicit bounds, or an exponential type-checking problem figuring it out from context.

-Joe

···

On Jan 27, 2016, at 11:34 PM, Jacob Bandes-Storch via swift-evolution <swift-evolution@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.


(Paul Ossenbruggen) #12

Kind works like optional chaining. In that the result remains an optional.

+1 for this, in that I agree that too often, control flow must be used for optionals. IMO clean code has fewer control flow statements and is more linear if possible.

···

On Jan 27, 2016, at 10:01 PM, Craig Cruden <ccruden@novafore.com> wrote:

Something like that would be good.

On 2016-01-28, at 12:50:50, Paul Ossenbruggen <possen@gmail.com <mailto:possen@gmail.com>> wrote:

Maybe something like this?

let n : Int? = 5

let r = n? + 5

On Jan 27, 2016, at 9:46 PM, Thorsten Seitz <tseitz42@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Paul Ossenbruggen) #13

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@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@novafore.com <mailto:ccruden@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@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@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #14

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@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@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@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@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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Craig Cruden) #15

Since the foundation of this proposal is Paul’s and yours - will either of you be drawing up the proposal?

···

On 2016-01-29, at 11:41:21, Jacob Bandes-Storch <jtbandes@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@novafore.com <mailto:ccruden@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@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@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Jacob Bandes-Storch) #16

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@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@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@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@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@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@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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(David Sweeris) #17

I'm not sure this is even possible (at least on x86/ARM), but could we do something like how the Mill CPU handles errors?

If you're not familiar with it, my understanding is that it just ignores errors (and cascades them through operations) until you try to write the invalid value to memory, at which point the exception is raised.

Seems like, in principle anyway, if Swift supported pure functions, the compiler could just silently propagate the nil until you try to store it in a non-optional variable, at which point you'd get the standard "didn't unwrap optional" error.

Like I said, I'm not sure this is even possible for compiled code, if the target platform doesn't have hardware support for it anyway.

···

Sent from my iPhone

On Jan 28, 2016, at 20:38, Craig Cruden via swift-evolution <swift-evolution@swift.org> 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@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@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@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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(Paul Ossenbruggen) #18

This is what I came up with so far. As Joe points out, it needs delimiters for more complex maps. Assume this for the examples:

func doSomething(value: Int) -> Int {
    return value
}
let pf : Int? = 5
let pg : Int? = nil

for simple expressions you could do this which is a pretty big win:

let gf = pf? + 5

For cases where the expression should not call a function if a parameter is nil, I came up with this for syntactic sugar which has delimiters. Inside the brackets the variable name is unwrapped automatically and real name of the variable is used. rather than $0. It is a win over the if let approach:

// if let approach
let ff : Int? = nil
if let pf = pf {
ff = doSomething(pf)
}

// the delimited approach is:
let gf = pf?{ doSomething(pf) } // pf not nil doSomething is called
let gg = pg?{ doSomething(pg) } // pg is nil doSomething is not called.

gf -> 5
gg -> nil

// which is the equivalent of the map approach:
let ff = pf.map { doSomething($0) }
let fg = pg.map { doSomething($0) }

ff -> 5
fg -> nil

Assuming that this syntax is not a big enough win over the map version, This means that in this situation, you need to use map.. If doSomething() took an optional though you could do this, because doSomething would always be called.

let gf = doSomething(pf?) // result is optional, could also omit “?"
let hf = doSomething(pf? + 5) // result is still an optional

- Paul

···

On Jan 30, 2016, at 10:28 AM, Joe Groff <jgroff@apple.com> wrote:

On Jan 27, 2016, at 11:34 PM, Jacob Bandes-Storch via swift-evolution <swift-evolution@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.

Yes, this would be a major problem with this sort of feature. Without some explicit delimiter for the bounds of the map operation, you have either extremely subtle rules for defining the implicit bounds, or an exponential type-checking problem figuring it out from context.

-Joe


(Paul Ossenbruggen) #19

I can take a stab at it. I will try to follow your lead and keep it short. :slight_smile:

···

On Jan 28, 2016, at 8:44 PM, Craig Cruden <ccruden@novafore.com> wrote:

Since the foundation of this proposal is Paul’s and yours - will either of you be drawing up the proposal?

On 2016-01-29, at 11:41:21, Jacob Bandes-Storch <jtbandes@gmail.com <mailto:jtbandes@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@novafore.com <mailto:ccruden@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@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@icloud.com <mailto:tseitz42@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@swift.org <mailto:swift-evolution@swift.org>>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@gmail.com <mailto:possen@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@swift.org <mailto:swift-evolution@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@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution


(Paul Ossenbruggen) #20

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@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@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@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@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@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@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@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@swift.org>:

Yes

On 2016-01-28, at 12:28:40, Paul Ossenbruggen <possen@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@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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution