I like the idea as associatedtype is playing the role of generic type and in extension we conforms to the protocol with some specific generic type as associated type.

I mean the first idea probably could be
protocol From<T> {
     init(_ value: T)
 }
but "protocols do not allow generic parameters; use associated types instead", so it seems natural to express concrete type as associated type for protocol in generic syntax <Type>

Probably alternative syntax could look like:

extension Int : From where .FromType = Float { }

Also, it seems like this proposal could help to solve a problem with the same name of associated type in different protocols:

protocol One {
     associatedtype Element
     func foo(t: Element)
 }

protocol Two {
     associatedtype Element
     func bar(t: Element)
 }

struct OneTwo : One, Two {
    func foo(t: Int) {}
    func bar(t: String) {}
}
// type 'OneTwo' does not conform to protocol 'Two'
// candidate has non-matching type '(t: String) -> ()' [with Element = Element]

So, as I understand, will be possible
struct OneTwo : One, Two<String> {
    func foo(t: Int) {} // OneTwo.Element will be Int
    func bar(t: String) {}
}

On 08.06.2016 22:07, Антон Жилин via swift-evolution wrote:
==Motivation==

protocol From {
    associatedtype FromType
    init(_ value: FromType)
}

The problem is, one type cannot implement multiple From "conversions".

==Proposed solution==

Allow specifying all associated types using generic syntax.

extension Int : From<Float> { }
extension Int : From<Double> { }

This is only allowed in conformance declarations.

==Future directions==

We can replace all *Convertible protocols with From and Into, which will be
defined similarly to Rust.

- Anton


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

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

Reply via email to