[swift-evolution] [Pitch] Rename `x.dynamicType` to `x.Self`

Taras Zakharko taras.zakharko at uzh.ch
Thu Apr 14 16:12:36 CDT 2016


I do not think that I was conflating these two aspects. Using #type(self) would return the particular type of the current instance (dynamic type) while using #type(A.var) would return the declared (static) type of the property. Now, 

  let a = A()
  #type(a.var) 

would again return the actual (dynamic) type of var

In essence, I don’t see why we need to separate the mechanisms for querying these types when the distinction can be made on the grounds whether we are querying the type of a particular value/instance or a particular (not instantiated) declaration. 

— Taras

P.S. Does Swift today even have the means of querying the static type of a variable once it has been instantiated, i.e. can I programmatically  find out that  x has been declared as P in this scope:

let x : P = TypeThatConformsToP()

If you really want to differentiate between these two then we’d need two separate mechanisms, e.g. #statictype(), #dynamictype(), with #dynamictype() referring to the  instantiated value and $statictype() referring to the variable declaration.



> On 14 Apr 2016, at 22:59, Joe Groff <jgroff at apple.com> wrote:
> 
>> 
>> On Apr 14, 2016, at 1:11 PM, Taras Zakharko <taras.zakharko at uzh.ch> wrote:
>> 
>> I do not understand why we can’t do
>> 
>> type(self)
>> 
>> or even
>> 
>> #type(self)
>> 
>> Personally, I find .Self business to be quite confusing and idiosyncratic. Having a generic #type() directive would be a general improvement for the language design and it would also open up future possibilities such as
>> 
>> extension A where A.B == #type(A.C.property1) {
>> 
>> }
>> 
>> and other potentially useful things. 
> 
> Swift's type system has static and dynamic manifestations, and you're conflating them here. #type in a situation like `where A.B == #type(A.C.property1)` would have to produce the static type of property, whereas `x.dynamicType` today produces the dynamic type of a class or existential.
> 
> -Joe

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160414/08772c05/attachment.html>


More information about the swift-evolution mailing list