mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-23 03:31:44 +00:00
Implement object literal parsing; Instantiate classes from object literals
Essentially, if the contextual type is a class with a constructor that takes zero arguments or doesn't have a constructor at all, an object literal can be used to initialize a new instance of that class with preset values.
This commit is contained in:
26
tests/compiler/object-literal.ts
Normal file
26
tests/compiler/object-literal.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import "allocator/arena";
|
||||
|
||||
class Foo {
|
||||
bar: i32;
|
||||
baz: string;
|
||||
}
|
||||
|
||||
function bar(foo: Foo): void {
|
||||
assert(foo.bar == 1);
|
||||
assert(foo.baz == "hello world");
|
||||
}
|
||||
|
||||
bar({ bar: 1, baz: "hello world" });
|
||||
|
||||
class Foo2 {
|
||||
bar: i32;
|
||||
constructor() {
|
||||
this.bar = 1;
|
||||
}
|
||||
}
|
||||
|
||||
function bar2(foo: Foo2): void {
|
||||
assert(foo.bar == 2);
|
||||
}
|
||||
|
||||
bar2({ bar: 2 });
|
Reference in New Issue
Block a user