Existentials in Swift


(Toni Suter) #1

Hi everyone,

I am trying to understand existentials in Swift. I understand that in the
following code, P1 & P2 is an existential type, because it supports values
from all types that conform to both P1 and P2.

protocol P1 {}
protocol P2 {}
var x: P1 & P2

However, this raises a few questions for me:

1. In the following code, is x also an existential?
protocol P {}
var x: P

The proposals / blog posts that I read always talk about the protocol composition syntax,
but to me, this is the same as the first example, just with only a single protocol requirement.

2. Are Any and AnyObject also existentials?
So far, my understanding is:
Any is an existential with no requirements
AnyObject is an existential with only a class requirement

3. Why can I not extend Any / AnyObject?
For example, AnyObject is a protocol defined in the standard library. Why is it not extensible?

Thanks and best regards,
Toni


(Karl) #2

1. Yes

2. Yes

3. I don't know the actual reason, but Any and AnyObject have special meaning to the compiler and objects implicitly "conform"" to them. Either the implementation doesn't support extending those types, or we explicitly decided that extending these implicit existentials was a bad idea (what could you even write in an extension in Any? You'd almost certainly have to cast it to Something. Extend Something instead.)

- Karl

···

On Nov 21, 2016 at 11:09 am, <Toni Suter via swift-users (mailto:swift-users@swift.org)> wrote:

  Hi everyone,

I am trying to understand existentials in Swift. I understand that in the

following code, P1 & P2 is an existential type, because it supports values

from all types that conform to both P1 and P2.

protocol P1 {}

protocol P2 {}

var x: P1 & P2

However, this raises a few questions for me:

1. In the following code, is x also an existential?

protocol P {}

var x: P

The proposals / blog posts that I read always talk about the protocol composition syntax,

but to me, this is the same as the first example, just with only a single protocol requirement.

2. Are Any and AnyObject also existentials?

So far, my understanding is:

Any is an existential with no requirements

AnyObject is an existential with only a class requirement

3. Why can I not extend Any / AnyObject?

For example, AnyObject is a protocol defined in the standard library. Why is it not extensible?

Thanks and best regards,

Toni
_______________________________________________ swift-users mailing list swift-users@swift.org (mailto:swift-users@swift.org) https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #3

You’re welcome. If you want to learn how Metatypes and Existential Metatypes work in Swift, take a look at our proposal where we discuss the current situation and how we want these types to be in Swift.

Link

When learning about existential types, our proposal might be a little bit hard to follow. Feel free to ask.

···

--
Adrian Zubarev
Sent with Airmail

Am 21. November 2016 um 20:42:12, Toni Suter (tonisuter@me.com) schrieb:

Hi Adrian,

Thanks for the link!

Best regards,
Toni

Am 21.11.2016 um 11:52 schrieb Adrian Zubarev <adrian.zubarev@devandartist.com>:

Private message (I currently don’t have enough time to answer everything):

Take a look at his small part: https://en.wikipedia.org/wiki/Type_system#Existential_types

--
Adrian Zubarev
Sent with Airmail

Am 21. November 2016 um 11:09:43, Toni Suter via swift-users (swift-users@swift.org) schrieb:

Hi everyone,

I am trying to understand existentials in Swift. I understand that in the
following code, P1 & P2 is an existential type, because it supports values
from all types that conform to both P1 and P2.

protocol P1 {}
protocol P2 {}
var x: P1 & P2

However, this raises a few questions for me:

1. In the following code, is x also an existential?
protocol P {}
var x: P

The proposals / blog posts that I read always talk about the protocol composition syntax,
but to me, this is the same as the first example, just with only a single protocol requirement.

2. Are Any and AnyObject also existentials?
So far, my understanding is:
Any is an existential with no requirements
AnyObject is an existential with only a class requirement

3. Why can I not extend Any / AnyObject?
For example, AnyObject is a protocol defined in the standard library. Why is it not extensible?

Thanks and best regards,
Toni
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Toni Suter) #4

Hi Karl,

1. Yes
2. Yes
3. I don't know the actual reason, but Any and AnyObject have special meaning to the compiler and objects implicitly "conform"" to them. Either the implementation doesn't support extending those types, or we explicitly decided that extending these implicit existentials was a bad idea (what could you even write in an extension in Any? You'd almost certainly have to cast it to Something. Extend Something instead.)

Ok. Yes, I agree it makes sense to disallow that. Thanks for your answer!

Best regards,
Toni

···

- Karl

On Nov 21, 2016 at 11:09 am, <Toni Suter via swift-users <mailto:swift-users@swift.org>> wrote:

Hi everyone,

I am trying to understand existentials in Swift. I understand that in the
following code, P1 & P2 is an existential type, because it supports values
from all types that conform to both P1 and P2.

protocol P1 {}
protocol P2 {}
var x: P1 & P2

However, this raises a few questions for me:

1. In the following code, is x also an existential?
protocol P {}
var x: P

The proposals / blog posts that I read always talk about the protocol composition syntax,
but to me, this is the same as the first example, just with only a single protocol requirement.

2. Are Any and AnyObject also existentials?
So far, my understanding is:
Any is an existential with no requirements
AnyObject is an existential with only a class requirement

3. Why can I not extend Any / AnyObject?
For example, AnyObject is a protocol defined in the standard library. Why is it not extensible?

Thanks and best regards,
Toni
_______________________________________________ swift-users mailing list swift-users@swift.org <mailto:swift-users@swift.org> https://lists.swift.org/mailman/listinfo/swift-users