amber-lang / amber-lang/amber

[Feature] Support for code organization using classes

Offen
#80 0 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement syntax
Vorherrschende Sprache
Rust
Sterne
5.2k
Forks
145
Ø Merge
5 T. 3 Std.
Gemergte PRs (30 T.)
7

Beschreibung

Because of how flexible function names are in Bash, something I've been doing for years is emulating classes something very similar to this pattern:

```bash
foo.bar () {
local this="$1" arg="$2"
a=$(jq --null-input --raw-output --argjson this '.a')
b=$(jq --null-input --argjson this '.b')
jq --null-input --compact-output \
--arg a "$(( a + arg ))" \
--argjson b "$b" \
'{a: $a, b: $b}'
}

foo='{"a": 4, "b": "stuff"}'
baz=$(foo.bar foo 6)
# baz is {"a": 10, "b": "stuff"}'
```

It would be so much better if this could be written in Amber as:
```
class Foo(a: Num, b: Text) {
fun bar (arg: Num): Foo {
this.a += arg
return this
}
}

let foo = Foo(4, "stuff")
let baz = foo.bar(10)
```

Which might be translated using a more efficient encoding than JSON:
```bash
Foo() {
local a="$1" b="$2" this
# Type checking omitted
this=('type:Foo' "$a" "$b")
# Mechanics of returning an array omitted
}

_Foo.bar() {
local tpe="$1" this=("$2" "$3") arg="$4"
# Argument and type checking omitted
this[0]=$(( this[0] + arg ))
# Mechanics of returning an array omitted
}

Foo 4 'stuff'
foo=# Mechanics of retrieving a returned array omitted

_Foo.bar "${foo[@]}" 6
baz=# Mechanics of retrieving a returned array omitted

# baz is (10 'stuff')
```

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.