Implicitly type conversion ?

I am confused by the code snippet following:

     func foo(_ value: Int?) {
       // do nothing
     }

     let a: Int = 2
     foo(a) // it works!

Why swift don't warn the type mismatch? I am woking on Xcode Version 7.3.1.

It’s an implicit type-cast. Any type `X` can always be implicitly converted to type `X?` (i.e. `Optional<X>`).

—Jens

···

On Aug 18, 2016, at 12:18 PM, adelzhang via swift-users <swift-users@swift.org> wrote:

Why swift don't warn the type mismatch? I am woking on Xcode Version 7.3.1.

Any other situation when implicit type casting works? It would be better if there is a reference or link.
---Original---

···

From: "Jens Alfke"<jens@mooseyard.com>
Date: 2016/8/19 00:27:33
To: "adelzhang"<adelzhang@qq.com>;
Cc: "swift-users"<swift-users@swift.org>;
Subject: Re: [swift-users] Implicitly type conversion ?

On Aug 18, 2016, at 12:18 PM, adelzhang via swift-users <swift-users@swift.org> wrote:

Why swift don't warn the type mismatch? I am woking on Xcode Version 7.3.1.

It’s an implicit type-cast. Any type `X` can always be implicitly converted to type `X?` (i.e. `Optional<X>`).

—Jens

From the Swift Programming Language book, the section “Optional Type”:

The type Optional<Wrapped> is an enumeration with two cases, none and some(Wrapped), which are used to represent values that may or may not be present. Any type can be explicitly declared to be (or implicitly converted to) an optional type. If you don’t provide an initial value when you declare an optional variable or property, its value automatically defaults to nil.

—Jens

···

On Aug 18, 2016, at 12:54 PM, Adel Zhang <adelzhang@qq.com> wrote:

Any other situation when implicit type casting works? It would be better if there is a reference or link.

I don't know if there's a comprehensive list anywhere. Here are the ones I can think of:

1. A subtype can be used where a supertype is expected; for instance, you can pass an `Int` to a parameter typed `Any` without a cast. The same is true of superclasses: `NSString` can be used where `NSObject` is expected. Obvious, but worth mentioning.

2. Swift 3's `AnyHashable` isn't *really* a supertype of `Hashable` types, but it's sort of treated as one.

3. The built-in `Array`, `Dictionary`, `Set`, and `Optional` types can be implicitly converted to the same data structure, but with supertypes of its generic parameters. For instance, an `Array<Int>` can be passed to a parameter of type `Array<Any>`. This is not a general feature of generics—it's special-cased for these types.

4. As you noticed, a type can be implicitly made more `Optional`; that is, `Int` converts to `Optional<Int>`, `Optional<Optional<Int>>`, and so on.

5. In Swift 2, importing Foundation activates many implicit conversions between Foundation and Standard Library types, including conversions to AnyObject. Many (perhaps all?) are gone in Swift 3. (However, Foundation still has plenty of magical `as` casts.)

Hope this helps,

···

On Aug 18, 2016, at 9:54 AM, Adel Zhang via swift-users <swift-users@swift.org> wrote:

Any other situation when implicit type casting works?

--
Brent Royal-Gordon
Architechies

Any idea why Swift supports implicit casting to AnyHashable, but not to, say, AnySequence?

···

> On Aug 18, 2016, at 9:54 AM, Adel Zhang via swift-users<swift-users@swift.org>wrote:
>
> Any other situation when implicit type casting works?
I don't know if there's a comprehensive list anywhere. Here are the ones I can think of:

1. A subtype can be used where a supertype is expected; for instance, you can pass an `Int` to a parameter typed `Any` without a cast. The same is true of superclasses: `NSString` can be used where `NSObject` is expected. Obvious, but worth mentioning.

2. Swift 3's `AnyHashable` isn't *really* a supertype of `Hashable` types, but it's sort of treated as one.

3. The built-in `Array`, `Dictionary`, `Set`, and `Optional` types can be implicitly converted to the same data structure, but with supertypes of its generic parameters. For instance, an `Array<Int>` can be passed to a parameter of type `Array<Any>`. This is not a general feature of generics—it's special-cased for these types.

4. As you noticed, a type can be implicitly made more `Optional`; that is, `Int` converts to `Optional<Int>`, `Optional<Optional<Int>>`, and so on.

5. In Swift 2, importing Foundation activates many implicit conversions between Foundation and Standard Library types, including conversions to AnyObject. Many (perhaps all?) are gone in Swift 3. (However, Foundation still has plenty of magical `as` casts.)

Hope this helps,
--
Brent Royal-Gordon
Architechies

It's mainly to support heterogeneous dictionary literals. There's not usually a need to upcast to AnySequence regularly, since most things that take sequences should be using generics, but heterogeneity and generics don't play well together.

Jordan

···

On Aug 19, 2016, at 9:00, Tim Vermeulen via swift-users <swift-users@swift.org> wrote:

Any idea why Swift supports implicit casting to AnyHashable, but not to, say, AnySequence?

On Aug 18, 2016, at 9:54 AM, Adel Zhang via swift-users<swift-users@swift.org>wrote:

Any other situation when implicit type casting works?

I don't know if there's a comprehensive list anywhere. Here are the ones I can think of:

1. A subtype can be used where a supertype is expected; for instance, you can pass an `Int` to a parameter typed `Any` without a cast. The same is true of superclasses: `NSString` can be used where `NSObject` is expected. Obvious, but worth mentioning.

2. Swift 3's `AnyHashable` isn't *really* a supertype of `Hashable` types, but it's sort of treated as one.

3. The built-in `Array`, `Dictionary`, `Set`, and `Optional` types can be implicitly converted to the same data structure, but with supertypes of its generic parameters. For instance, an `Array<Int>` can be passed to a parameter of type `Array<Any>`. This is not a general feature of generics—it's special-cased for these types.

4. As you noticed, a type can be implicitly made more `Optional`; that is, `Int` converts to `Optional<Int>`, `Optional<Optional<Int>>`, and so on.

5. In Swift 2, importing Foundation activates many implicit conversions between Foundation and Standard Library types, including conversions to AnyObject. Many (perhaps all?) are gone in Swift 3. (However, Foundation still has plenty of magical `as` casts.)

Hope this helps,
--
Brent Royal-Gordon
Architechies

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

It's mainly to support heterogeneous dictionary literals. There's not usually a need to upcast to AnySequence regularly, since most things that take sequences should be using generics, but heterogeneity and generics don't play well together.

Jordan

···

On Aug 19, 2016, at 9:00, Tim Vermeulen via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

Any idea why Swift supports implicit casting to AnyHashable, but not to, say, AnySequence?

On Aug 18, 2016, at 9:54 AM, Adel Zhang via swift-users<swift-users@swift.org <mailto:swift-users@swift.org>>wrote:

Any other situation when implicit type casting works?

I don't know if there's a comprehensive list anywhere. Here are the ones I can think of:

1. A subtype can be used where a supertype is expected; for instance, you can pass an `Int` to a parameter typed `Any` without a cast. The same is true of superclasses: `NSString` can be used where `NSObject` is expected. Obvious, but worth mentioning.

2. Swift 3's `AnyHashable` isn't *really* a supertype of `Hashable` types, but it's sort of treated as one.

3. The built-in `Array`, `Dictionary`, `Set`, and `Optional` types can be implicitly converted to the same data structure, but with supertypes of its generic parameters. For instance, an `Array<Int>` can be passed to a parameter of type `Array<Any>`. This is not a general feature of generics—it's special-cased for these types.

4. As you noticed, a type can be implicitly made more `Optional`; that is, `Int` converts to `Optional<Int>`, `Optional<Optional<Int>>`, and so on.

5. In Swift 2, importing Foundation activates many implicit conversions between Foundation and Standard Library types, including conversions to AnyObject. Many (perhaps all?) are gone in Swift 3. (However, Foundation still has plenty of magical `as` casts.)

Hope this helps,
--
Brent Royal-Gordon
Architechies

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

It’s a hack until existential support gets better. Explicitly casting to AnyHashable clutters your dictionary literals.

There’s a fair amount of code in the compiler to support it, it’s probably just not worth the effort for other types (and since they were never anything else, nothing has regressed for them as it did for AnyHashable).

···

On 19 Aug 2016, at 18:00, Tim Vermeulen via swift-users <swift-users@swift.org> wrote:

Any idea why Swift supports implicit casting to AnyHashable, but not to, say, AnySequence?

1 Like