Closed a-skua closed 4 months ago
You can use !
to bypass linter:
class Point {
x!: i32;
y!: i32;
}
or
class Point {
x: i32 = 0;
y: i32 = 0;
}
@MaxGraey
Thanks for the info!
However, it seems that !
is not currently supported in AS.
PEDANTIC AS906: Unnecessary definite assignment.
:
19 │ x!: i32;
│ ~
└─ in assembly/index.ts(19,3)
PEDANTIC AS906: Unnecessary definite assignment.
:
20 │ y!: i32;
│ ~
└─ in assembly/index.ts(20,3)
Therefore, it would be written like this.
class Point {
x: i32 = 0;
y: i32 = 0;
}
But what I want to do here is not initializing with zero, but defining a type. I understand that AS is like TS but is a different language. How closely do you intend to mimic TS syntax?
https://www.assemblyscript.org/compiler.html#host-bindings
Type | Strategy | Description |
---|---|---|
Object | Copy | If a plain object. That is: Has no constructor or non-public fields. |
Object | By reference | If not a plain object. Passed as an opaque reference counted pointer. |
This proposal arose from the observation that "no constructor" classes are treated differently than regular classes, prompting the question of whether we could adopt a writing style closer to TS.
I'm now thinking that supporting !
instead of type aliases would also be acceptable.
class Point {
x!: i32;
y!: i32;
}
That's PEDANTIC. You can safely ignore that.
I see, I misunderstood PEDANTIC as a build failure. It's already supported. The issue is resolved, so I'll close this. Thank you for your help!
Feature suggestion
I've understood that by defining a no constructor class, it's possible to pass object literals from JavaScript.
AS:
JS:
However, no constructor classes trigger syntax errors in TypeScript linters.
Therefore, I'm considering whether it's feasible to use type aliases as syntactic sugar for no constructor classes.
This
type Point
is treated asclass Point
in AssemblyScript, so the output ofasbuild
will be the same code. I think for TypeScript users, object type aliases might be more familiar.Feature suggestion
We will permit the following syntax as syntactic sugar for classes.
e.g. https://github.com/a-skua/assemblyscript/commit/d9d5e4e49188fcaedf82350fa196856d435cb7fd