amber-lang / amber-lang/amber

[Feature] Support for code organization using classes

Đang mở
#80 0 bình luận 4 reaction 0 người được giao Xem trên GitHub
enhancement syntax
Ngôn ngữ chính
Rust
Star
5.2k
Fork
145
Merge trung bình
5 ngày 3 giờ
Pull request đã merge (30 ngày)
7

Mô tả

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')
```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.