[swift-dev] How is `static` a contextual keyword?

Quinn "The Eskimo!" eskimo1 at apple.com
Fri May 12 02:09:17 CDT 2017


On 11 May 2017, at 23:48, Jose Cheyo Jimenez via swift-dev <swift-dev at swift.org> wrote:

> `class` is a contextual keyword because it can be used to declare an object and declare a class variable. Right?
> 
> I believe static can only be used inside Classes, Structs, Enums, Protocols and Extensions so how is `static` a _contextual_ keyword?

I think you’ve misunderstood the meaning of “contextual keyword”.  Here it means that the string may or may not be a keyword depending on the context.  Consider:

enum SomeEnum {
    case `static`
    case `class`
}

print(SomeEnum.class)
print(SomeEnum.static)

func `class`(class: Int) { }

func `static`(static: Int) { }

`class`(class: 0)
`static`(static: 1)

Through this I’d like to use `static` and `class` as identifiers.  I have to escape them in some contexts, where they are being treated as keywords, but not in the others.

This is a neat-o feature because it avoids unnecessary conflicts.  The example that most readily springs to mind is the use of `for` as a parameter label in many places in the standard library and Foundation.

> Is there a reason why static is a keyword and not a “buildIn” modifier like final, public etc?

I can’t help you with that one I’m afraid.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware




More information about the swift-dev mailing list