-
Notifications
You must be signed in to change notification settings - Fork 562
Description
This proposal outlines the strategic refinement of the XGo specification to simplify the MiniSpec while maintaining full compatibility in the FullSpec. By replacing traditional Go-style structs with Tuples in the MiniSpec, XGo achieves a more cohesive, functional, and modern syntax.
Related files:
- doc/spec-mini.md (MiniSpec)
- doc/spec.md (FullSpec)
- doc/spec/mini/mini.xgo (MiniSpec in EBNF Grammar)
Proposal: Modernizing XGo MiniSpec with Tuple Types
1. Overview
To enhance the simplicity and functional elegance of the XGo MiniSpec, we propose removing traditional struct types and CompositeLit (brace-based initialization) from the core recommended syntax set. These will be replaced by Tuples, which serve as syntactic sugar for anonymous structs but use a more consistent function-style construction.
MiniSpec becomes more focused on modern, lightweight data composition.
FullSpec retains all traditional Go-style struct capabilities for backward compatibility and complex system definitions.
2. Changes to MiniSpec
The MiniSpec will undergo the following structural changes:
A. Removal of Structs and Braced Literals
- StructType is removed from MiniSpec and moved to FullSpec.
- CompositeLit (e.g.,
Type{fields...}) is removed from MiniSpec and moved to FullSpec. - Struct-style Tuple Construction (using {}) is relegated to FullSpec.
B. Introduction of Tuple Types
Tuples are introduced as the primary way to define structured data.
- Type Declaration:
type Point (x, y int) // Named Tuple
type Result (int, error) // Anonymous Tuple- Single-Element Degeneracy: (T) is equivalent to T.
C. Function-Style Construction (MiniSpec Standard)
To unify the syntax for creating objects and calling functions, the MiniSpec will use the Function-Style Construction for Tuples:
- Positional Construction: p := Point(10, 20)
- Keyword Arguments (kwargs): p := Point(x = 10, y = 20) (Using
=to distinguish from CompositeLit-style:used in FullSpec).
D. Anonymous Tuple Construction
Anonymous tuples can be constructed using parenthesized expressions:
t := (10, "hello", true)
E. Explicit Field Access
x := p.x(Named field)val := t.0,name := t.1(Numeric index)
3. Changes to FullSpec
The FullSpec will act as the superset, incorporating the removed MiniSpec elements and advanced Tuple features.
A. Preservation of Legacy Syntax
- StructType: Standard struct { ... } remains valid.
- CompositeLit: Brace-based initialization T{...} remains valid for both Structs and Tuples.
B. Advanced Tuple Features
- Struct-style Construction: The ability to initialize a Tuple using braces (e.g., Point{x: 10, y: 20}) is supported in FullSpec for developers migrating from Go.
- Type Compatibility: Rules for structural equivalence between different Tuple names are defined here.
- Reflection: Details on how Tuples map to X_0, X_1 ordinal fields at runtime.
- Summary Table of Syntax Migration
| Feature | MiniSpec (New) | FullSpec (Legacy/Advanced) |
|---|---|---|
| Data Grouping | Tuples (x, y int) | Structs & Tuples |
| Value Creation | Function-style T(1, 2) | Braced-style T{1, 2} |
| Keyword Args | = syntax: T(x=1) | : syntax: T{x:1} |
| Anon. Construction | (1, "a") | N/A (Handled as anon. struct) |
| Field Access | .0 or .name | .X_0, .0, or .name |
4. Updated mini.xgo (EBNF Grammar)
The following represents the updated mini.xgo logic, replacing StructType and CompositeLit with TupleType and TupleConstruction logic.
// --- Updated XGo MiniSpec Grammar Snippet ---
// 1. Replace StructType with TupleType in TypeLit
TypeLit = PointerType | ArrayType | MapType | FuncType | TupleType | InterfaceType
// 2. Define TupleType (Syntactic sugar for anonymous structs)
TupleType = "(" ?TupleParameterList ")"
TupleParameterList = IDENT % "," Type | ParameterList
// 3. Update PrimaryExpr to handle Function-Style Construction
// CompositeLit is removed; Tuple values are created via CallOrConversion
PrimaryExpr = Operand *(CallOrConversion | SelectorOrTypeAssertion | IndexOrSlice | ErrWrap)
// 4. Update Operand to include Anonymous Tuple Construction
Operand =
INT ?UNIT | FLOAT ?UNIT | STRING | CHAR | RAT | IMAG |
AnonymousTuple | FunctionLit | IDENT | ...
// 5. Define Anonymous Tuple
AnonymousTuple = "(" LambdaExpr % "," ")"
// 6. Update CallOrConversion to support Kwargs (= syntax)
CallOrConversion = "(" ?(ArgList) ?"..." ?"," ")"
ArgList = (LambdaExpr | Kwarg) % ","
Kwarg = IDENT "=" LambdaExpr