Why is static a keyword?


(Chéyo Jiménez) #1

I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834


(Chéyo Jiménez) #2

Hello Swift Dev,

`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?

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

Or is it just for historic reasons?

“The "type" keyword was split into two: "static" and "class". One can define static functions and static properties in structs and enums like this:…."

https://github.com/apple/swift/commit/571c9b3c5ef995e280b494c751804b385b49e7b9

Thanks!


(Zhao Xin) #3

In Swift, you use `static in struct and enum` and `class in class`. For
example,

struct Foo {

    static func bar() {

    }

}

class ClassFoo {

    class func bar() {

    }

}

Another the `class func bar()` can replace to `static` as well. Here the
`static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {

    static func bar() {

    }

}

Zhaoxin

···

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users < swift-users@swift.org> wrote:

I was expecting static to be a builtin. Does anybody know why it must be a
keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Chéyo Jiménez) #4

I can understand class being a keyword because you use the same word to declare an object. So `class` is used in different contexts like you showed below. That is not the case for static. Class can appear at the top level but IFAIK static will not appear at top level declarations.

In Swift, you use `static in struct and enum` and `class in class`. For example,

struct Foo {
    static func bar() {
        
    }
}

class ClassFoo {
    class func bar() {
        
    }
}

Another the `class func bar()` can replace to `static` as well. Here the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {
    static func bar() {
        
    }
}

static func bar() = final class func bar()
But if you type final class the compiler will say to make it static instead.

static func bar() != class func bar()

Static is only an attribute modifier. Class is a modifier and a declaration depending on context. Why must static be a keyword when every other modifier is not?

···

On May 11, 2017, at 9:59 AM, Zhao Xin <owenzx@gmail.com> wrote:

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users <swift-users@swift.org> wrote:
I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #5

I don’t think this is the answer that was asked for. I bet it’s more a technical question from the internal point of of view.

···

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For example,

struct Foo {
static func bar() {

\}

}

class ClassFoo {
class func bar() {

\}

}

Another the `class func bar()` can replace to `static` as well. Here the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {
static func bar() {

\}

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users <swift-users@swift.org> wrote:
I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #6

No. I think it is just a compromise.

Zhaoxin

···

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev < adrian.zubarev@devandartist.com> wrote:

I don’t think this is the answer that was asked for. I bet it’s more a
technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (
swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For
example,

struct Foo {

    static func bar() {

    }

}

class ClassFoo {

    class func bar() {

    }

}

Another the `class func bar()` can replace to `static` as well. Here the
`static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {

    static func bar() {

    }

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users < > swift-users@swift.org> wrote:

I was expecting static to be a builtin. Does anybody know why it must be
a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Quinn “The Eskimo!”) #7

`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

···

On 11 May 2017, at 23:48, Jose Cheyo Jimenez via swift-dev <swift-dev@swift.org> wrote:
--
Quinn "The Eskimo!" <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


(Austin Zheng) #8

`class` and `static` in classes have subtly different meanings, if I recall
correctly. A `class` declaration can be overriden in subclasses; a `static`
one can't.

Best,
Austin

···

On Thu, May 11, 2017 at 10:04 AM, Zhao Xin via swift-users < swift-users@swift.org> wrote:

No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev < > adrian.zubarev@devandartist.com> wrote:

I don’t think this is the answer that was asked for. I bet it’s more a
technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (
swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For
example,

struct Foo {

    static func bar() {

    }

}

class ClassFoo {

    class func bar() {

    }

}

Another the `class func bar()` can replace to `static` as well. Here the
`static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {

    static func bar() {

    }

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users < >> swift-users@swift.org> wrote:

I was expecting static to be a builtin. Does anybody know why it must be
a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #9

Have you read closely the bug issue before posting your answer?

···

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 19:05:13, Zhao Xin (owenzx@gmail.com) schrieb:

No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:
I don’t think this is the answer that was asked for. I bet it’s more a technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For example,

struct Foo {
static func bar() {

\}

}

class ClassFoo {
class func bar() {

\}

}

Another the `class func bar()` can replace to `static` as well. Here the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {
static func bar() {

\}

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users <swift-users@swift.org> wrote:
I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #10

Furthermore:

In a class declaration, the static keyword has the same effect as marking the declaration with both the class and final declaration modifiers.
Source.

···

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 19:07:53, Austin Zheng (austinzheng@gmail.com) schrieb:

`class` and `static` in classes have subtly different meanings, if I recall correctly. A `class` declaration can be overriden in subclasses; a `static` one can't.

Best,
Austin

On Thu, May 11, 2017 at 10:04 AM, Zhao Xin via swift-users <swift-users@swift.org> wrote:
No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:
I don’t think this is the answer that was asked for. I bet it’s more a technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For example,

struct Foo {
static func bar() {

\}

}

class ClassFoo {
class func bar() {

\}

}

Another the `class func bar()` can replace to `static` as well. Here the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {
static func bar() {

\}

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users <swift-users@swift.org> wrote:
I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #11

You are right. I was wrong. Thank you.

Zhaoxin

···

On Fri, May 12, 2017 at 1:07 AM, Austin Zheng <austinzheng@gmail.com> wrote:

`class` and `static` in classes have subtly different meanings, if I
recall correctly. A `class` declaration can be overriden in subclasses; a
`static` one can't.

Best,
Austin

On Thu, May 11, 2017 at 10:04 AM, Zhao Xin via swift-users < > swift-users@swift.org> wrote:

No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev < >> adrian.zubarev@devandartist.com> wrote:

I don’t think this is the answer that was asked for. I bet it’s more a
technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (
swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For
example,

struct Foo {

    static func bar() {

    }

}

class ClassFoo {

    class func bar() {

    }

}

Another the `class func bar()` can replace to `static` as well. Here
the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {

    static func bar() {

    }

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users < >>> swift-users@swift.org> wrote:

I was expecting static to be a builtin. Does anybody know why it must
be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Zhao Xin) #12

Yes, I had. There was a `class` and `static` post under it. But I disagreed
that and my interpretation was just opposite.

Zhaoxin

···

On Fri, May 12, 2017 at 1:11 AM, Adrian Zubarev < adrian.zubarev@devandartist.com> wrote:

Have you read closely the bug issue before posting your answer?

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 19:05:13, Zhao Xin (owenzx@gmail.com) schrieb:

No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev < > adrian.zubarev@devandartist.com> wrote:

I don’t think this is the answer that was asked for. I bet it’s more a
technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (
swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For
example,

struct Foo {

    static func bar() {

    }

}

class ClassFoo {

    class func bar() {

    }

}

Another the `class func bar()` can replace to `static` as well. Here the
`static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {

    static func bar() {

    }

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users < >> swift-users@swift.org> wrote:

I was expecting static to be a builtin. Does anybody know why it must be
a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Adrian Zubarev) #13

Don’t worry, help is always appreciated :wink: Next time take another minute before acting to fast.

···

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 19:19:46, Zhao Xin (owenzx@gmail.com) schrieb:

You are right. I was wrong. Thank you.

Zhaoxin

On Fri, May 12, 2017 at 1:07 AM, Austin Zheng <austinzheng@gmail.com> wrote:
`class` and `static` in classes have subtly different meanings, if I recall correctly. A `class` declaration can be overriden in subclasses; a `static` one can't.

Best,
Austin

On Thu, May 11, 2017 at 10:04 AM, Zhao Xin via swift-users <swift-users@swift.org> wrote:
No. I think it is just a compromise.

Zhaoxin

On Fri, May 12, 2017 at 1:01 AM, Adrian Zubarev <adrian.zubarev@devandartist.com> wrote:
I don’t think this is the answer that was asked for. I bet it’s more a technical question from the internal point of of view.

--
Adrian Zubarev
Sent with Airmail

Am 11. Mai 2017 um 18:59:58, Zhao Xin via swift-users (swift-users@swift.org) schrieb:

In Swift, you use `static in struct and enum` and `class in class`. For example,

struct Foo {
static func bar() {

\}

}

class ClassFoo {
class func bar() {

\}

}

Another the `class func bar()` can replace to `static` as well. Here the `static` and `class` are equal in functions of classes.

And `class` is a keyword.

class ClassFoo2 {
static func bar() {

\}

}

Zhaoxin

On Fri, May 12, 2017 at 12:17 AM, Jose Cheyo Jimenez via swift-users <swift-users@swift.org> wrote:
I was expecting static to be a builtin. Does anybody know why it must be a keyword?

Background. https://bugs.swift.org/browse/SR-4834

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users