[swift-dev] Optional to String conversion
Greg Parker
gparker at apple.com
Sat Dec 5 06:10:51 CST 2015
> On Dec 4, 2015, at 10:00 PM, Andrew Trick <atrick at apple.com> wrote:
>
> I’m adding runtime functionality to support optional casts and ran into some Optional to String conversion behavior that is currently somewhat accidental—it will break when I add functionality. I want to understand the desired behavior before doing extra work to fix it.
>
> Currently swift does this:
>
>> print(Int?(3))
> Optional(3)
>> print(String?("meow"))
> Optional("meow")
>
> I think swift should do this:
>
>> print(Int?(3))
> 3
>> print(String?("meow"))
> "meow"
>> debugPrint(Int?(3))
> Optional(3)
>> debugPrint(String?("meow"))
> Optional("meow")
>
> When a value already knows how to represent itself as a string, I don't think that the string "Optional" belongs in the textual representation.
There are unfortunate output ambiguities if you don't include it.
Consider the output of all of these:
print(String?("test"))
print(String?("nil"))
print(String?.None)
The current output is unambiguous:
print(String?("test"))
Optional("test")
print(String?("nil"))
Optional("nil")
print(String?.None)
nil
--
Greg Parker gparker at apple.com Runtime Wrangler
More information about the swift-dev
mailing list