[swift-evolution] Enhanced existential types proposal discussion

Thorsten Seitz tseitz42 at icloud.com
Fri May 27 03:12:59 CDT 2016


> Am 26.05.2016 um 22:07 schrieb Matthew Johnson via swift-evolution <swift-evolution at swift.org>:
> 
> 
> 
> Sent from my iPad
> 
> On May 26, 2016, at 2:49 PM, Austin Zheng via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 
>> I alway enjoy hearing your ideas.
>> 
>> This is quite interesting. It's basically a way to define an ad-hoc interface that a type doesn't need to explicitly declare it conforms to. I know Golang works similarly; if a Go type implements all the requirements of an interface it conforms automatically.
>> 
>> There are positives and negatives to allowing this sort of ad-hoc interface.
> 
> Agree.  It would definitely make the language "feel" a bit more fluid.  But it doesn't add any expressive power and could have undesirable consequences.
> 
>> This would make for a good standalone proposal -- both because it's complex enough to deserve its own discussion, and because if the community is interested someone would have to work through all the implications in order to put together a proposal. It would be quite a big change.
> 
> I don't see how this is different from a protocol other than the lack of requirement to declare conformance explicitly.  The need to explicitly declare conformance is a design decision that I believe the core team feels pretty strongly about.  
> 
> That said, it hasn't been debated by the community yet so if someone feels strongly about dropping explicit conformance declarations it might be worth pitching the idea, if for not other reason than to have a discussion about it on the lost.
> 
>> 
>> Best,
>> Austin
>> 
>> On Thu, May 26, 2016 at 11:56 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> I’d like to throw one idea of mine in the room I couldn’t stop thinking when I read one of Thorsten’s replies on SE–0095 review thread.
>> 
>> This wiki section <https://en.wikipedia.org/wiki/Type_system#Existential_types> explains the existential types where we have something like this:
>> 
>> "T = ∃X { a: X; f: (X → int); } This could be implemented in different ways; for example:
>> 
>> intT = { a: int; f: (int → int); }
>> floatT = { a: float; f: (float → int); }
>> We discussed how we could create existential types with constraints for protocols and classes so far. Such an existential can’t create something like in the example above.
>> 
>> 

With Joe’s information about unbound associated types still being usable, e.g. as `a.Element` with the current proposal we should actually already have existential types like defined in Wikipedia or Haskell (except for the name "existential“ being used differently in Swift, e.g. for protocols without associated types, too).

protocol T {
	associatedtype X
	var a: X { get }
	func f(_ value: X) -> Int
}

// use T as existential in Wikipedia’s sense
func foo(t: any<T>) -> Int {
	return t.f(t.a)  // t.X is not bound to a fixed type but as it is used consistently it works for any T
}


struct A : T {
	var a: Int
	func f(_ value: Int) -> Int { return value }
}

struct B : T {
	var a: String
	func f(_ value: String) -> Int { return value.characters.count }
}

let a = A(a: 42)
let b = B(a: "hello")
let x = foo(a) // 42
let y = foo(b) // 5


Actually in this case we could have written foo() also as generic function (without having to bind X!)

func foo<P: T>(t: P) -> Int {
	return t.f(t.a)
}

This works already today.


-Thorsten

>> I’m not sure if we need this at all, I’d say it’s a nice to have idea of mine.
>> 
>> To solve this we could introduce a new scope similar to protocols today but without the need to explicitly conform types to this existential.
>> 
>> // the above example can become
>> existential T {
>>     associatedtype X
>>     var a: X
>>     func f(_ value: X) -> Int
>> }
>> 
>> struct A /* no explicit conformance to T needed */ {
>>     var a: Int
>>     init(a: Int) { self.a = a }
>>     func f(_ value: Int) -> Int { return value }
>> }
>> 
>> let store: T = A() // this could or should work, just because we do have visibility to all constraints from T in A here

Isn’t this just structural subtyping as opposed to nominal subtyping (which I strongly prefer)?

-Thorsten



>> 
>> // if we had `private var a: Int` in A we wouldn't be able to store A inside `store`
>> I din’t though if existential could have potential to replace Any<…> completely. Until now I just wanted to solve that particular issue so please don’t judge with me. :)
>> 
>> Just because of associated types we won’t be able to use store in this example, but there might be more trivial examples where one would use such existential type (for only visible portion at compile or dynamically at run-time) without explicit conformance.
>> 
>> struct B {
>>     var x: Int = 42
>>     var y: Double = -100.5
>> }
>> 
>> struct C: SomeProtocol {
>>     var y: Double = 0.0
>>     var x: Int = 10
>> }
>> 
>> existential SomeShinyThing {
>>     var x: Int
>>     var y: Double
>> }
>> 
>> // we should be safe here because the compiler has visibility for  
>> // internal B and C here
>> let anotherStore: SomeShinyThing = B() /* or */ C()  
>> 
>> // otherwise one could use dynamic casts
>> if let thirdStore = instanceOfCShadowedAsSomeProtocol as? SomeShinyThing { … }
>> Feel to tear this idea apart as you want. :D
>> 
>> 
>> 
>> 
>> -- 
>> Adrian Zubarev
>> Sent with Airmail
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>> 
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160527/8f91546a/attachment.html>


More information about the swift-evolution mailing list