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

Jacob Bandes-Storch jtbandes at gmail.com
Sun Mar 27 18:19:34 CDT 2016


I think it would make sense if a postfix "?" would make the variable
optional:

    var x = "some string"?
    x = nil  // allowed because x has type Optional<String>

This parallels the optional pattern: `if case "foo"? = x { ... }`

Jacob

On Sun, Mar 27, 2016 at 3:13 PM, Maximilian Hünenberger <
swift-evolution at swift.org> wrote:

> Only to be complete:
>
> let str1 = String?() // nil
> let str2 = String?("") // ""
>
> Kind regards
> - Maximilian
>
> Am 24.03.2016 um 11:57 schrieb Hugues Bernet-Rollande via swift-evolution <
> swift-evolution at swift.org>:
>
> Hey All,
>
> I think Andrew already nailed my proposal.
>
> Thank you all.
>
>
> Hugues BERNET-ROLLANDE
>
>
> --
>
> hugues at xdev.fr
>
> http://www.xdev.fr
>
> http://www.linkedin.com/in/huguesbr
>
>> On Mar 24 2016, at 11:56 am, Dan Raviv <dan.raviv at gmail.com> wrote:
>> Hi Hughes,
>>
>> What's wrong with:
>> let s = Optional(String("abc"))
>>
>>
>> On Thu, Mar 24, 2016 at 12:22 PM, Hugues Bernet-Rollande via
>> swift-evolution <swift-evolution at swift.org> wrote:
>>
>> In a strongly typed language, such as Swift is, type inference is greatly
>> appreciated as it declutter the instance type definition.
>>
>> For example, in the following statement, the compiler easily deduct the
>> type of `aString` by the return type of `String.init()`
>>
>> ```
>> let aString = String()
>> ```
>>
>> Optional are generic enum which can contain a value of a generic type or
>> nil (
>> https://github.com/apple/swift/blob/master/stdlib/public/core/Optional.swift
>> )
>>
>> Sometime you may want specify that the instance is of type optional in
>> which even if you are a the same time assigning a value to this instance.
>> In which case you loose the type inference mechanism and you have to
>> define both (the optionality as well as the type).
>> For example, you may want specify an optional String with a default value.
>>
>> ```
>> var a:String? = String()
>> // or
>> var b = String() as String?
>> // or
>> var c:Optional<String> = String()
>> ```
>>
>> But the compiler can already infer the variable type from this
>> assignment, it just miss the "optionality" of the variable.
>> It would be nice to be able to express this.
>>
>> Then for, I propose the following syntax evolution:
>>
>> ```
>> var a:? = String()
>> // and/or (not recommended because more prone to typo and unclear...)
>> var a = String() as?
>> ```
>>
>> This would allow for more synthetic optional declaration.
>>
>> I've draft a proposal on my `swift-evolution` fork:
>>
>> https://github.com/huguesbr/swift-evolution/blob/optionality-type-with-type-inference/proposals/0057-optionality-type-with-type-inference.md
>>
>> Let me know what you think.
>>
>>
>> Hugues BERNET-ROLLANDE
>>
>>
>> --
>>
>> hugues at xdev.fr
>>
>> http://www.xdev.fr
>>
>> http://www.linkedin.com/in/huguesbr
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>>
>> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160327/ec99c7b1/attachment.html>


More information about the swift-evolution mailing list