Renderingen-US

Experimental through version 1. Version 0 was alpha. This is the first language release: the interface is relatively stable, not frozen. Nothing is promised stable until version 2.

ts-reserved-field-names

Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.

Genus fields, methods, variant fields, parameters and locals may be spelled like ECMAScript reserved words; a field is read, written and built under the name it was declared with.

Syntax: class Pouch { mut int yield; fn delete(int await) → int { let int let ← await + self.yield; return let } }

Category#

identifier

Examples#

radix/corpus/reference/concept/ts-reserved-field-names/ts-reserved-field-names.fab (canonical · concept)#

Genus fields, methods, variant fields, parameters and locals may be spelled like ECMAScript reserved words; a field is read, written and built under the name it was declared with.

# =============================================================================
# ts-reserved-field-names — property names that look like JavaScript keywords
# =============================================================================
#
# What this teaches:
#   • `yield`, `delete`, `default`, `new`, `await`, `let`, `static`, `enum`,
#     `implements`, `interface`, `package`, `private`, `protected` and `public`
#     are ordinary identifiers in Faber. A class field or method, a union
#     variant field, a parameter and a local may all use them.
#   • A field keeps one spelling on every backend: the name it was declared
#     with is the name it is read, written, constructed and displayed under.
#
# Common mistakes:
#   • Renaming a field to dodge a target's reserved word — the compiler keeps
#     the property name and only escapes bindings (locals, parameters).
#
# See also: keyword-as-ident, rust-reserved-names
# =============================================================================

# ts-reserved-field-names — reserved-word spellings as fields, methods, variant fields and bindings
#
# EXPECTED OUTPUT:
#   ts-reserved-field-names.expected — the computed values, then the displayed class.

class Pouch {
    mut int yield
    mut int delete
    mut int default
    mut int static
    mut int enum
    mut int implements
    mut int interface
    mut int package
    mut int private
    mut int protected
    mut int public

    fn new(int await) → int {
        let int let ← await + self.yield
        return let + self.delete + self.default
    }

    fn function() → int {
        return self.static + self.enum + self.implements + self.interface
    }

    fn visibility() → int {
        return self.package + self.private + self.protected + self.public
    }
}

union Shift {
    Move {
        int yield
        int delete
    },
    Stay
}

fn pack(int yield, int default) → int {
    return yield + default
}

fn weigh(Shift s) → int {
    match s {
        case Move let yield, delete {
            return yield * 10 + delete
        }
        case Stay {
            return 0
        }
    }
}

main {
    let Pouch b ← Pouch { yield = 1, delete = 2, default = 3, static = 4, enum = 5, implements = 6, interface = 7, package = 8, private = 9, protected = 10, public = 11 }
    print b.yield
    print b.delete
    print b.default
    print b.new(10)
    print b.function()
    print b.visibility()
    print pack(1, 2)
    mut Pouch c ← Pouch { yield = 4, delete = 5, default = 6, static = 0, enum = 0, implements = 0, interface = 0, package = 0, private = 0, protected = 0, public = 0 }
    c.yield ← 9
    c.delete ← c.delete + 1
    print c.yield + c.delete
    let Shift moved ← variant Move { yield = 3, delete = 4 } ∷ Shift
    print weigh(moved)
    print weigh(variant Stay ∷ Shift)

    # Destructuring reads the properties and binds escaped local names.
    let int yield ← b.yield
    let int new ← b.delete
    print yield
    print new
    print b
}

Expected output:

1
2
3
16
22
38
3
15
34
0
1
2
Pouch { yield = 1, delete = 2, default = 3, static = 4, enum = 5, implements = 6, interface = 7, package = 8, private = 9, protected = 10, public = 11 }