[swift-evolution] [RFC] #Self

Matthew Johnson matthew at anandabits.com
Tue May 10 17:53:24 CDT 2016



Sent from my iPad

> On May 10, 2016, at 5:41 PM, Timothy Wood <tjw at me.com> wrote:
> 
> 
>> On May 10, 2016, at 2:01 PM, Chris Lattner via swift-evolution <swift-evolution at swift.org> wrote:
>> That said, I’m not sure I understand the concrete use-cases.  When is this concept important?  When is “Self” not good enough?
> 
> From my old mail on the subject:
> 
>> I would love to see a way to get the type of the "enclosing thing at compile time”. In my particular case, I’m using the type as a generic parameter to tag a created resource with something like:
>> 
>> class Client: PropertyOwner {
>> 	let intProperty = Client.property(“name”, Int(0))
>> }
>> 
>> where PropertyOwner has a static property<Owner, DataType>(...)
>> 
>> With `Self` meaning the static version of the thing being compiled, I could at least write:
>> 
>> class Client: PropertyOwner {
>> 	let intProperty = Self.property(“name”, Int(0))
>> }
>> 
>> which would have the benefit of being harder to mess up due to copy-pasting between different PropertyOwner implementors.
> 
> 
> and
> 
>> I was thinking about the syntax a bit further and it seems like the capability that would be added is like #file, in that it does some compile-time textual replacement. So, perhaps #Self would work?
>> 
>> Also, along these lines, I would find use for call-site interpolation like #file has. I could then do a free function version of my property() call that was something like:
>> 
>> 	func property(ownerType: PropertyOwner.Type = #Self, ...) { }
>> 
>> which would only be callable from w/in things conforming to or subclassing PropertyOwner.

Ahh, this is different than what I have been talking about as it statically evaluates at the call site rather than the declaration site.  Sorry, I think I missed your earlier post.

One question about this - how would it work if this method was called in a lexical con txt without an enclosing type declaration?  Would that be an error or would it just require a parameter to be passed?

If we call what I am talking about Type and we also want this behavior we should call it #Type as it would be the static type at the call site.  But they are separate ideas and should be separate proposals.

> 
> 
> -tim
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160510/eb9af88a/attachment.html>


More information about the swift-evolution mailing list