[swift-evolution] [swift-evolution-announce] [Review] SE-0068: Expanding Swift Self to class members and value types

Erica Sadun erica at ericasadun.com
Sun Apr 24 21:17:30 CDT 2016


> On Apr 24, 2016, at 7:57 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 	https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md
> 
> I'm not totally clear what's being proposed here.
> 
> Is this valid?
> 
> 	class Foo {
> 		static func bar() { … }
> 		func baz(other: Foo) {
> 			Self.bar()
> 		}
> 	}

This is valid. Foo.bar(). 

> 
> Is this valid?
> 
> 	struct Foo {		// note: not class
> 		static func bar() { … }
> 		func baz(other: Foo) {
> 			Self.bar()
> 		}
> 	}
> 

This is valid. Foo.bar()

> Is this valid?
> 
> 	class Foo {
> 		static func bar() { … }
> 		func baz(other: Foo) {
> 			other.Self.bar()	// note: not self
> 		}
> 	}
> 

This is valid. Foo.bar(). All instances of the class share the static bar() member.

> Is this valid?
> 
> 	struct Foo {		// note: not class
> 		static func bar() { … }
> 		func baz(other: Foo) {
> 			other.Self.bar()	// note: not self
> 		}
> 	}
> 

This is should be valid although it currently errors with `dynamicType` (error: type 'Foo' has no member 'bar'). 

You missed subclassing, although being static members, methods are final and cannot be overridden. But if class Boo : Foo calls Self.bar() and Foo implements the static member, it's called from the Boo instance and uses the Foo implementation.

> Can a class have a variable (inside a method) of type Self? A return value? A parameter? A property?

I think it should, however that technically may need to be of type #Self (per the original draft) instead of Self and have it expanded to the defining type at compile time.

> Can a value type have a variable (inside a method) of type Self? A return value? A parameter? A property?

Ditto.

-- E

> 
> -- 
> Brent Royal-Gordon
> Architechies
> 
> _______________________________________________
> 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/20160424/2e4a60a3/attachment.html>


More information about the swift-evolution mailing list