Proposal: Static Members
XGo introduces static members — static constants, static variables, and static methods — as type-scoped declarations distinct from global declarations and instance members.
Syntax Overview
In Normal XGo
const T.name = ... // static constant
var T.name V = ... // static variable
func T.Method(arg1 T1, arg2 T2, ..., argN TN) (ret1 R1, ..., retM RM) // static methodIn XGo Classfile
const T.name = ... // static constant (explicit form)
const .name = ... // static constant (shorthand form)
var T.name V = ... // static variable (explicit form)
var .name V = ... // static variable (shorthand form)
func T.Method(...) // static method (explicit form)
func .Method(...) // static method (shorthand form)Syntax constraint: The dot (
.) inT.nameand.namemust immediately follow the identifier with no intervening whitespace. Forms such asT .nameor. nameare rejected as syntax errors at parse time.
Referring to Static Members
T.Method(arg1, arg2, ..., argN) // call static method
T.name // access static variable or constantWhen accessing from within the same classfile, or accessing the corresponding project class from a work class, the type qualifier can be omitted:
Method(arg1, arg2, ..., argN) // call static method (shorthand)
name // access static variable or constant (shorthand)Declaration Semantics by Declaration Kind
type
| Context | Forms | Semantics |
|---|---|---|
| Normal XGo | type Name ... |
Global type name |
| Classfile | type Name ... |
Global type name |
type declarations are always global in both normal XGo and classfiles. There is no T.Name or .Name syntax for types.
const
| Context | Form | Semantics |
|---|---|---|
| Normal XGo | T.name |
Static member |
| Normal XGo | name |
Global constant |
| Classfile | T.name |
Static member |
| Classfile | .name |
Static member |
| Classfile | name |
Global constant |
In both normal XGo and classfiles, a bare name (without a type qualifier) is a global constant, not a static member.
var
| Context | Form | Semantics |
|---|---|---|
| Normal XGo | T.name |
Static variable |
| Normal XGo | name |
Global variable |
| Classfile | T.name |
Static variable |
| Classfile | .name |
Static variable |
| Classfile | name |
Class (instance) member |
Classfile restriction: A classfile may contain at most one top-level
varblock in total. All variable declarations — whetherT.name,.name, or barename— must appear within this singlevarblock. A second or subsequent top-levelvarblock is a parse-time error.
func / method
| Context | Form | Semantics |
|---|---|---|
| Normal XGo | T.Method |
Static method |
| Normal XGo | name |
Global function |
| Classfile | T.Method |
Static method |
| Classfile | .Method |
Static method |
| Classfile | name |
Instance method |
Summary Table
| Kind | Normal XGo T.name |
Normal XGo name |
Classfile T.name |
Classfile .name |
Classfile name |
|---|---|---|---|---|---|
type |
(not allowed) | Global | (not allowed) | (not allowed) | Global |
const |
Static member | Global | Static member | Static member | Global |
var |
Static variable | Global variable | Static variable | Static variable | Class (instance) member † |
func |
Static method | Global function | Static method | Static method | Instance method |
† A classfile may only have one top-level var block in total; all var declarations (T.name, .name, and bare name) must appear within it.
Source: goplus/xgo