?? operator question


#1

Why does this compile?
let t1: Int? = 2print(t1 ?? "asdf")
The type of ?? is this:
@warn_unused_resultpublic func ??<T>(optional: T?, @autoclosure defaultValue:
() throws -> T) rethrows -> T
The type syas that T must be the same type throughout. But above I could
successfully use Int? and then String. Why?


(Jordan Rose) #2

Swift will infer types from the context of an expression as well as the inputs. In this case, although 'Int' and 'String' do not share a type, they are both convertible to 'Any', the argument to 'print'. So because of the call to 'print', 'T' becomes 'Any', and the expression becomes, essentially

print((t1 as Any?) ?? ("asdf" as Any))

(Actually, the signature of 'print' is a little more complicated than that, but that's the gist of what's going on.)

Jordan

···

On Apr 18, 2016, at 2:51, tuuranton--- via swift-users <swift-users@swift.org> wrote:

Why does this compile?

let t1: Int? = 2
print(t1 ?? "asdf")

The type of ?? is this:

@warn_unused_result
public func ??<T>(optional: T?, @autoclosure defaultValue: () throws -> T) rethrows -> T

The type syas that T must be the same type throughout. But above I could successfully use Int? and then String. Why?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Marius-Ioan Serban) #3

Because in your example T = CustomStringConvertible

···

On Mon, Apr 18, 2016 at 10:51 AM, tuuranton--- via swift-users < swift-users@swift.org> wrote:

Why does this compile?

let t1: Int? = 2
print(t1 ?? "asdf")

The type of ?? is this:

@warn_unused_result
public func ??<T>(optional: T?, @autoclosure defaultValue: () throws -> T)
rethrows -> T

The type syas that T must be the same type throughout. But above I could
successfully use Int? and then String. Why?

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

--
Marius Serban


(Marco Feltmann) #4

Well, I guess you try to re-typedef (??) a language construct.
According to NSHipster ?? is the languages |nil| coalescing operator.

So it simply compiles because t1 isn't nil.

Further reference:
https://en.wikipedia.org/wiki/Null_coalescing_operator#Swift

···

Am 18.04.2016 um 11:51 schrieb tuuranton--- via swift-users:

Why does this compile?
let t1: Int? = 2print(t1 ?? "asdf")


(Zhao Xin) #5

I do think there is something wrong here.

// Xcode 7.3.1, Swift 2.2

let t1: Int? = 2 // struct Int?
let y = t1 ?? "abcdf" // error
let x = t1 ?? NSFont(name: "", size: 0) // x: NSObject?

for x, how could it be NSObject?, as t1 is a struct?

zhaoxin

···

On Mon, Apr 18, 2016 at 6:59 PM, Marco Feltmann via swift-users < swift-users@swift.org> wrote:

Am 18.04.2016 um 11:51 schrieb tuuranton--- via swift-users:

Why does this compile?
let t1: Int? = 2print(t1 ?? "asdf")

Well, I guess you try to re-typedef (??) a language construct.
According to NSHipster ?? is the languages nil coalescing operator.

So it simply compiles because t1 isn't nil.

Further reference:
https://en.wikipedia.org/wiki/Null_coalescing_operator#Swift

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


(Erica Sadun) #6

My guess would be NSNumber.

Strings can bridge to NSString (struct to class), enums of ErrorType to NSError (enum to class), etc.

-- E

···

On Apr 18, 2016, at 7:23 AM, zh ao via swift-users <swift-users@swift.org> wrote:

I do think there is something wrong here.

// Xcode 7.3.1, Swift 2.2

let t1: Int? = 2 // struct Int?
let y = t1 ?? "abcdf" // error
let x = t1 ?? NSFont(name: "", size: 0) // x: NSObject?

for x, how could it be NSObject?, as t1 is a struct?

zhaoxin


(Zhao Xin) #7

I don't think NSNumber is OK here. As t1 is a struct not a literal value.
If NSNumber is OK. I think the compiler is too smart than it should be.

Also, you should aware that right side of let y works in print(), but does
not work in let, which is not consistent somehow.

Owen

···

On Mon, Apr 18, 2016 at 9:49 PM, Erica Sadun <erica@ericasadun.com> wrote:

On Apr 18, 2016, at 7:23 AM, zh ao via swift-users <swift-users@swift.org> > wrote:

I do think there is something wrong here.

// Xcode 7.3.1, Swift 2.2

let t1: Int? = 2 // struct Int?
let y = t1 ?? "abcdf" // error
let x = t1 ?? NSFont(name: "", size: 0) // x: NSObject?

for x, how could it be NSObject?, as t1 is a struct?

zhaoxin

My guess would be NSNumber.

Strings can bridge to NSString (struct to class), enums of ErrorType to
NSError (enum to class), etc.

-- E


(Marco Feltmann) #8

Just guessed since I try to separate Swift from Objective-C frameworks.

I don't think NSNumber is OK here. As t1 is a struct not a literal value.

In fact t1 in an OPTIONAL struct.
That means it can point to anything from `nil` to Int Struct, NSInteger and NSNumber.
AFAIR it automatically bridges to NSNumber as soon as you import Foundation or a similiar framework. Same works with String. String is a struct used in plain Swift and NSString used with Foundation.

If NSNumber is OK. I think the compiler is too smart than it should be.

Indeed it is. Way to smart.
It is that smart that it sometimes stands in its own way.
At least when used with Objective-C. Your question outlines one of those strange side effects.

Also, you should aware that right side of let y works in print(), but does
not work in let, which is not consistent somehow.

It is consistent.
You can map an OPTIONAL integer to NSNumber (using Foundation framework) but you cannot map an OPTIONAL integer to NSString.

"But wait a sec, how can NSFount(name: "", size: 0) result in an NSNumber?" you may ask.
I don't now.
The behaviour of NSFonts +fonttWithName:size: for empty/not found font name is not defined in the documentation, so ANY result is correct.

···

Am 19.04.2016 um 03:15 schrieb zh ao:


(Jens Alfke) #9

That’s not true. The result has to be an NSFont? as declared in the class interface — it can’t be an NSNumber. This has nothing to do with whether the font is not found at runtime; we’re talking about compile-time type inference.

—Jens

···

On Apr 19, 2016, at 4:09 AM, Marco Feltmann via swift-users <swift-users@swift.org> wrote:

"But wait a sec, how can NSFount(name: "", size: 0) result in an NSNumber?" you may ask.
I don't now.
The behaviour of NSFonts +fonttWithName:size: for empty/not found font name is not defined in the documentation, so ANY result is correct.


(Zhao Xin) #10

Thanks for your explanation.

I still think the compiler should not be that smart, causing only itself
could understanding what is going on. It will be a nightmare.

Owen

···

On Tue, Apr 19, 2016 at 7:09 PM, Marco Feltmann via swift-users < swift-users@swift.org> wrote:

Just guessed since I try to separate Swift from Objective-C frameworks.

Am 19.04.2016 um 03:15 schrieb zh ao:

I don't think NSNumber is OK here. As t1 is a struct not a literal value.

In fact t1 in an OPTIONAL struct.
That means it can point to anything from `nil` to Int Struct, NSInteger
and NSNumber.
AFAIR it automatically bridges to NSNumber as soon as you import
Foundation or a similiar framework. Same works with String. String is a
struct used in plain Swift and NSString used with Foundation.

If NSNumber is OK. I think the compiler is too smart than it should be.

Indeed it is. Way to smart.
It is that smart that it sometimes stands in its own way.
At least when used with Objective-C. Your question outlines one of those
strange side effects.

Also, you should aware that right side of let y works in print(), but does
not work in let, which is not consistent somehow.

It is consistent.
You can map an OPTIONAL integer to NSNumber (using Foundation framework)
but you cannot map an OPTIONAL integer to NSString.

"But wait a sec, how can NSFount(name: "", size: 0) result in an
NSNumber?" you may ask.
I don't now.
The behaviour of NSFonts +fonttWithName:size: for empty/not found font
name is not defined in the documentation, so ANY result is correct.

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