Skip to content

Proposal: Static Members #2513

Description

@xushiwei

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 method

In 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 (.) in T.name and .name must immediately follow the identifier with no intervening whitespace. Forms such as T .name or . name are 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 constant

When 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 var block in total. All variable declarations — whether T.name, .name, or bare name — must appear within this single var block. A second or subsequent top-level var block 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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions