[swift-evolution] Proposal: CustomConvertible protocol
John McCall
rjmccall at apple.com
Fri Dec 4 11:42:28 CST 2015
> On Dec 4, 2015, at 5:51 AM, Adrian Zubarev <adrian.zubarev at devandartist.com> wrote:
> We have finally a corner where people can share their ideas on how Swift can be improved, and I’m glad my ideas might be part of this fun.
>
> Swift introduced a few Convertible protocols, but it still needs some more If you ask me.
>
> Something like this:
>
> public protocol CustomConvertible {
>
> typealias CustomType
>
> public init(value: Self.CustomType) // or in a better way I can't come up with
> }
> But it should work in a way that we could create more different types like enums and ErrorType.
>
> protocol AConvertible: CustomConvertible { /* some stuff here*/ }
> protocol BConvertible: CustomConvertible { /* some stuff here*/ }
>
> struct A {
> var foo: Int = 0
> }
>
> struct B {
> var boo: String = "Hello World"
> }
>
> struct C: AConvertible, BConvertible {
>
> typealias CustomAType = A
> typealias CustomBType = B
>
> var magical: SomeType
>
> init(value: CustomAType) {
> // implement it
> }
>
> init(value: CustomBType) {
> // implement it
> }
> }
>
> //===========================
>
> let firstC: C = A() // this is what I'm missing in Swift
> let secondC: C = B() // this would be a great syntax sugar
> What do you think?
>
This is effectively a request for user-defined conversion operators. We actually did support those at one point in the history of Swift, but we removed them because they both introduced a lot of confusion and errors into common user idioms and were very problematic for the type-checker. It’s easy to look at obviously-contextually-typed examples like this and say that the type-checker should just make it work, but in the context of a general System F-sub type system, it is a massive source of added complexity.
In other words, at present, this is not an extension we feel we can deliver a satisfactory experience for, and to be taken seriously, any proposal is going need to demonstrate a lot of familiarity with type systems.
John.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151204/2cd5dab6/attachment.html>
More information about the swift-evolution
mailing list