# As Operator Overload

**URL:** <https://forums.swift.org/t/as-operator-overload/26038>\
**Category:** Pitches\
**Created:** [June 18, 2019, 10:07pm UTC](https://forums.swift.org/t/as-operator-overload/26038 "2019-06-18T22:07:40Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [June 19, 2019, 2:04pm UTC](https://forums.swift.org/t/as-operator-overload/26038/21 "2019-06-19T14:04:45Z")

</div>

> [@xwu](#):
>
> Here, you are indeed attempting to use `as` to convert between types, and it is invalid in Swift because `as` is not a conversion operator but a coercion operator. The error is:

My bad. I'm so used to have `as` everywhere in my code to bridge types that I forgot it didn't work for numeric types.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 2:06pm UTC](https://forums.swift.org/t/as-operator-overload/26038/22 "2019-06-19T14:06:03Z")

</div>

> [@Jean-Daniel](#):
>
> Yes, but String's storage is not `String` . You actually convert from a NSString to a String with a custom storage, so there is actually a conversion here IMHO.

I’m not sure what you mean by this. [When you bridge an `NSString` to `String`](https://forums.swift.org/t/piercing-the-string-veil/21700), its representation in memory is unchanged, and all operations are handled through resilient function calls. When bridging in the other direction, `String` storage is an `NSString` subclass, as I’ve already cited previously. That there are optimizations possible to improve string performance in no way changes the semantics or syntax of the language, where `as` is a _coercion_ operator that does not convert between types.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [June 19, 2019, 2:14pm UTC](https://forums.swift.org/t/as-operator-overload/26038/23 "2019-06-19T14:14:13Z")

</div>

I mean that this is not real `toll-free` as in Obj-C where the casting required zero instruction as it was just a casting.

In Swift, you still have to execute code to create a String value, populate it with the NSString and set the right flags to tell Swift to handle it specially.

This proposition is about letting custom type support that kind of bridging / conversion, where the compiler generate a function call to convert a type into an other type.

You may argue that in case of NSString\<-\>String, this is more a wrapping that a conversion, but the principle is the same. You still need to call a special function that convert a representation into an other one (either by wrapping it or converting it).

And String is not the only bridges types. When you use `as` on an NSNumber to convert it to a base type, there is a real type conversion, not just a wrapping of the underlying value.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [June 19, 2019, 2:19pm UTC](https://forums.swift.org/t/as-operator-overload/26038/24 "2019-06-19T14:19:31Z")

</div>

> [@xwu](#):
>
> where `as` is a _coercion_ operator that does not convert between types.

I'm not native English, so maybe my understanding of `coercion` is not right, but from Wikipedia, _coercion_ is just an implicit conversion.

> [@](#):
>
> Implicit type conversion, also known as _coercion_ , is an automatic type conversion by the compiler.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 2:23pm UTC](https://forums.swift.org/t/as-operator-overload/26038/25 "2019-06-19T14:23:48Z")

</div>

> [@Jean-Daniel](#):
>
> And String is not the only bridges types. When you use `as` on an NSNumber to convert it to a base type, there is a real type conversion, not just a wrapping of the underlying value.

No, you **can’t** use `as` for NSNumber bridging. You must use `as?` or `as!`: these are distinct operators with semantics even more complicated than those of `as`. Critically, those are _not_ coercion operators like `as` is.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [June 19, 2019, 2:26pm UTC](https://forums.swift.org/t/as-operator-overload/26038/26 "2019-06-19T14:26:25Z")

</div>

> [@xwu](#):
>
> No, you **can’t** use `as` for NSNumber bridging. You must use `as?` or `as!` : these are distinct operators with semantics even more complicated than those of `as` . Critically, those are _not_ coercion operators like `as` is.

Absolutely can.

```swift
import Foundation
print(true as NSNumber)

```

Compiles, runs, prints "1".

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 2:28pm UTC](https://forums.swift.org/t/as-operator-overload/26038/27 "2019-06-19T14:28:00Z")

</div>

> [@ahti](#):
>
> Absolutely can.
> 
> ```swift
> import Foundation
> print(true as NSNumber)
> 
> ```
> 
> Compiles, runs, prints "1".

That is not bridging. That is telling Swift that you want the literal `true` to be of type `NSNumber`. There is no other type involved. See the examples given above for literals.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [June 19, 2019, 2:28pm UTC](https://forums.swift.org/t/as-operator-overload/26038/28 "2019-06-19T14:28:31Z")

</div>

```swift
import Foundation
let b: Bool = true
print(b as NSNumber)

```

Same result.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 2:47pm UTC](https://forums.swift.org/t/as-operator-overload/26038/29 "2019-06-19T14:47:38Z")

</div>

> [@ahti](#):
>
> ```swift
> import Foundation
> let b: Bool = true
> print(b as NSNumber)
> 
> ```
> 
> Same result.

Ah, right. This is to imitate a subtyping relationship between `Bool` and `NSNumber`. Confusing, though, as you can see. [Full of subtleties as well.](https://numerics.diploid.ca/numeric-types-in-foundation.html)

There’s been talk about whether this feature is a good idea at all, and as you can guess I would be in favor of eventually removing it.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [June 19, 2019, 2:52pm UTC](https://forums.swift.org/t/as-operator-overload/26038/30 "2019-06-19T14:52:29Z")

</div>

> [@xwu](#):
>
> Confusing, though, as you can see. [Full of subtleties as well.](https://numerics.diploid.ca/numeric-types-in-foundation.html)

Very confusing. Especially as you are right telling that you can't bridge from NSNumber to a numerical type, but only from a numerical/boolean type to NSNumber.

---

<div class="post-metadata">

**Author:** ![Paulo\_Faria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulo_faria/32/20351_2.png) [@Paulo\_Faria](https://forums.swift.org/u/Paulo_Faria)\
**Post date:** [June 19, 2019, 2:54pm UTC](https://forums.swift.org/t/as-operator-overload/26038/31 "2019-06-19T14:54:36Z")

</div>

Sorry for the confusion. When I said bridging I didn't mean as in toll-free bridging. I meant bridging simply in as a way to bridge together two different APIs like the Swift Standard Library and Foundation as in the `String` and `NSString` example. Talking about this specific example; The toll-free bridging between `String` and `NSString` is just an implementation detail. An important one, sure, but a detail nevertheless. The important aspect here is the semantics of the `as` operator in `someString as NSString`, which it pretty straightforward, "treat this type as this type". It doesn't matter if it's toll-free bridging, coercion, conversion, etc. This shouldn't make a difference and certainly, I don't think we should add yet another keyword for conversion.

---

<div class="post-metadata">

**Author:** ![Paulo\_Faria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulo_faria/32/20351_2.png) [@Paulo\_Faria](https://forums.swift.org/u/Paulo_Faria)\
**Post date:** [June 19, 2019, 3:03pm UTC](https://forums.swift.org/t/as-operator-overload/26038/32 "2019-06-19T15:03:37Z")

</div>

> [@xwu](#):
>
> There’s been talk about whether this feature is a good idea at all, and as you can guess I would be in favor of eventually removing it.

I disagree, I don't think using `as` to convert between `Bool` and `NSNumber` is confusing. From an API perspective, it makes a lot of sense. Again, that's super straightforward, as would `HTTPRequest as URLRequest` be. I don't think it really matters "how" the `as` operator is working beneath the cover. On the other hand, I think the language would benefit a lot in having an idiomatic and consistent way to express "treat this type as this type" for our custom types as well.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 3:39pm UTC](https://forums.swift.org/t/as-operator-overload/26038/33 "2019-06-19T15:39:33Z")

</div>

> [@Paulo\_Faria](#):
>
> On the other hand, I think the language would benefit a lot in having an idiomatic and consistent way to express "treat this type as this type" for our custom types as well.

Swift has standardized on `OneType(instanceOfAnotherType)` as the idiomatic way of converting between two types. It is _not_ the role of `as`.

> [@Paulo\_Faria](#):
>
> The toll-free bridging between `String` and `NSString` is just an implementation detail. An important one, sure, but a detail nevertheless. The important aspect here is the semantics of the `as` operator in `someString as NSString` , which it pretty straightforward, "treat this type as this type". It doesn't matter if it's toll-free bridging, coercion, conversion, etc.

Those are _not_ the semantics of the `as` operator. Bridging between Foundation and standard library types has been engineered carefully to maximize interoperability between Swift and Objective-C APIs. That characteristic is not an “implementation detail” but rather a tentpole feature of Swift. In other words, it would _not_ have been acceptable to change the implementation of Swift `String` so that bridging from `NSString` involved eagerly converting from UTF-16 to UTF-8.

Now, if you want to discuss the possibility of value subtyping relationships...

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [June 19, 2019, 3:52pm UTC](https://forums.swift.org/t/as-operator-overload/26038/34 "2019-06-19T15:52:33Z")

</div>

> [@xwu](#):
>
> Now, if you want to discuss the possibility of value subtyping relationships...

This.

---

<div class="post-metadata">

**Author:** ![Paulo\_Faria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulo_faria/32/20351_2.png) [@Paulo\_Faria](https://forums.swift.org/u/Paulo_Faria)\
**Post date:** [June 19, 2019, 5:12pm UTC](https://forums.swift.org/t/as-operator-overload/26038/35 "2019-06-19T17:12:36Z")

</div>

> [@xwu](#):
>
> Swift has standardized on `OneType(instanceOfAnotherType)` as the idiomatic way of converting between two types. It is _not_ the role of `as` .

Right, this is why I'm proposing the overload of the `as` operator. Initializing a type with another type with no label is not always used to convey conversion. One example using SwitUI:

```swift
Text("Hello world!")

```

We're not converting a `String` to `Text` here, we're initializing `Text` with a `String` (using the `StringProtocol`, but with a `String` nonetheless). `Text` here is a `View` used for UI, not a container for characters as `String` is.

Now let's compare:

```swift
URLRequest(httpRequest)
// vs
httpRequest as URLRequest

```

The version with the `as` operator is much more clearer.

> [@xwu](#):
>
> Those are _not_ the semantics of the `as` operator. Bridging between Foundation and standard library types has been engineered carefully to maximize interoperability between Swift and Objective-C APIs. That characteristic is not an “implementation detail” but rather a tentpole feature of Swift. In other words, it would _not_ have been acceptable to change the implementation of Swift `String` so that bridging from `NSString` involved eagerly converting from UTF-16 to UTF-8.

I honestly don't understand why you're insisting on the topic of toll-free bridging. The `as` operator is used as a way to perform toll-free bridging, but it's not the only use of the `as` operator. Right now I think we've seen three different uses just in this conversation:

Toll-free bridging as in:

```swift
let string: String = "Hello world!" 
let nsstring = string as NSString

```

Conversion to imitate a subtyping relationship (If I understood correctly) as in:

```swift
let bool: Bool = true
let nsnumber = bool as NSNumber

```

Subtyping relationship as in:

```swift
class Parent {}
class Child : Parent {}
let child: Child = Child()
let parent = child as Parent

```

or

```swift
protocol Protocol {}
struct Child : Protocol {}
let child: Child = Child()
let protocol = child as Protocol

```

From the type system perspective, the underlying method which is used by the `as` operator is irrelevant. All I want to be able to do is to state that one type can be expressed **as** another. Now my question is; If we already have toll-free bridging and conversion for `Foundation` types using the `as` operator, why can't we have them for our custom types?

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 5:38pm UTC](https://forums.swift.org/t/as-operator-overload/26038/36 "2019-06-19T17:38:58Z")

</div>

> [@Paulo\_Faria](#):
>
> All I want to be able to do is to state that one type can be expressed **as** another. Now my question is; If we already have toll-free bridging and conversion for `Foundation` types using the `as` operator, why can't we have them for our custom types?

As you’ve shown, “as” has a variety of meanings.

Do you mean a subtyping relationship? Value subtyping is certainly a large topic.

Do you mean you want custom toll-free bridging? That’s also an interesting topic, but is probably best tackled after Swift gets more facilities to control type layout.

But `as` is not equivalent to or an alternative to the standard syntax for converting between types, which is expressed as an initializer.

---

<div class="post-metadata">

**Author:** ![Paulo\_Faria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulo_faria/32/20351_2.png) [@Paulo\_Faria](https://forums.swift.org/u/Paulo_Faria)\
**Post date:** [June 19, 2019, 5:40pm UTC](https://forums.swift.org/t/as-operator-overload/26038/37 "2019-06-19T17:40:36Z")

</div>

> [@xwu](#):
>
> But `as` is not equivalent to or an alternative to the standard syntax for converting between types, which is expressed as an initializer.

Yes, that's why I'm proposing this, so it becomes.

---

<div class="post-metadata">

**Author:** ![Paulo\_Faria](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paulo_faria/32/20351_2.png) [@Paulo\_Faria](https://forums.swift.org/u/Paulo_Faria)\
**Post date:** [June 19, 2019, 5:43pm UTC](https://forums.swift.org/t/as-operator-overload/26038/38 "2019-06-19T17:43:41Z")

</div>

Can you tell me any disadvantage of having the `as` operator as the standard syntax for converting between types?

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [June 19, 2019, 5:44pm UTC](https://forums.swift.org/t/as-operator-overload/26038/39 "2019-06-19T17:44:29Z")

</div>

> [@Paulo\_Faria](#):
>
> All I want to be able to do is to state that one type can be expressed **as** another.

There _is_ a way and it's standardized 🙂 :

> [@xwu](#):
>
> Swift has standardized on `OneType(instanceOfAnotherType)` as the idiomatic way of converting between two types. It is _not_ the role of `as` .

To reiterate, `as` does _not_ do Type conversion. It's used to:

- Cast from one Type to an _underlying_ type that the variable _already_ is.
- Clarify Type inference from literal values
- Specialized _bridging_ between Swift and Obj-C.

The last of which is more/less a compromise to make interop easy.

There is currently _zero_ usage that converts one Swift Type to another Swift Type

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [June 19, 2019, 5:44pm UTC](https://forums.swift.org/t/as-operator-overload/26038/40 "2019-06-19T17:44:32Z")

</div>

> [@Paulo\_Faria](#):
>
> Yes, that's why I'm proposing this, so it becomes.

That’s a very different topic from custom bridging.

Do I understand that your position is that you do not like the established syntax for converting between types, and therefore you want to reuse an existing syntax which is not for converting between types for this purpose?

[Previous page](https://forums.swift.org/t/as-operator-overload/26038.md?page=1)

[Next page](https://forums.swift.org/t/as-operator-overload/26038.md?page=3)
