[swift-evolution] allowing to specify optionality with type inference

Hugues Bernet-Rollande hugues at xdev.fr
Thu Mar 31 10:34:44 CDT 2016


Hey All,

  

I think Andrew already pointed out the solution.

  

In first place I wasn't sure if there was a syntax to express the Optionality
of a variable while keeping the inference type.

  

I was just suggesting to use the "?" syntax to do so:

  

```

// Optional string  with an existing without declaring a specific type except
it's optionality, gaining for free the String type inference

var c: ? = "I could become nil"

```

  

But Andrew pointed out that an alternative syntax already exist:

  

```

var a: Optional = "I could become nil"  
var b:Optional<String> = "me too"  
```

  

And thanks to Erika, another one:

  

```

let c = Optional("Also me, thanks to Erika")  

```

  

I think it could still make sense to allow the "?" syntax previously
described, as it is more synthetic than the other one and match the Optional
usage (with "?") and could also be completed with a "!" syntax:

  

```

// Optional String with an existing value as a String using inference type

var e:? = "I'm a synthetic version of the above"

// equivalent of

var f: Optional =  "I'm a synthetic version of the above"

  

// Unwrapped Optional with an existing as a String using inference type

var g:! = "I could be nil but will crash if access directly"

// equivalent of

var h:Optional = "I could be nil but will crash if access directly"

```

  

To close (or open for who wants to) this debate, I think Optional have already
enough syntax.

  

Hugues

  

On Mar 31 2016, at 4:22 pm, Erica Sadun <erica at ericasadun.com> wrote:  

>  

>

>> On Mar 24, 2016, at 4:22 AM, Hugues Bernet-Rollande via swift-evolution &lt
;[swift-evolution at swift.org](mailto:swift-evolution at swift.org)> wrote:

>>

>> ```

>>

>> var a:String? = String()

>>

>> // or

>>

>> var b = String() as String?

>>

>> // or

>>

>> var c:Optional<String> = String()  

>>

>> ```

>

>  

>

> When I need to force an optional (primarily for sample code) I can do this
already:

>

>  

>

> ```

>

> var myOptionalString = Optional("")

>

> ```

>

>  

>

> In this example, `myOptionalString ` picks up the type as well as the
optionality.

>

> Does this not meet your requirement of "_Allowing to specify only the
optionality part of _

>

> _a variable type __while still having the actual wrapped type infered by the
assignement_" in existing

>

> Swift?

>

>  

>

> Beyond that, can you offer any compelling use-cases that motivate this
change?

>

>  

>

> \-- E

>

>  

>

>  

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


More information about the swift-evolution mailing list