-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for generics in TypeScript target #1595
base: master
Are you sure you want to change the base?
Conversation
|
I think this deserves a second look after merging lf-lang/reactor-ts#168 and #1857. The first issue appears to be here and can be addressed rather quickly: lingua-franca/core/src/main/kotlin/org/lflang/generator/ts/TSReactorGenerator.kt Lines 101 to 104 in 8366599
The source of the second issue appears to be here: lingua-franca/core/src/main/kotlin/org/lflang/generator/ts/TSReactionGenerator.kt Lines 158 to 171 in 8366599
Defined here: lingua-franca/core/src/main/kotlin/org/lflang/generator/ts/TSExtensions.kt Lines 41 to 43 in 8366599
Types were inferred here: lingua-franca/core/src/main/java/org/lflang/generator/ts/TSTypes.java Lines 17 to 24 in 8366599
Which ultimately leads me here: I might need to read the code more times to better understand what's happening; yet, if other target languages' generic type support doesn't suffer this problem, then it would appear a bit weird to me, as it appears to me that the issue is that the type for the port, |
You're right; this is where the problem lies. I'm also unsure how to fix it. I've asked @oowekyala if he has any hints. |
Still todo for other things like actions
So apparently the generated code looks like // there is a field
d: Delay<number>
// and in the ctor
this.addReaction(
[this.startup],
[this.writable(this.d.inp)],
function (this, __d_inp: ReadWrite<T>) {
// =============== START react prologue
const util = this.util;
let d = {inp: __d_inp.get(),}
// =============== END react prologue
try {
d.inp = 42;
} finally {
// =============== START react epilogue
if (d.inp !== undefined) {
__d_inp.set(d.inp)
}
// =============== END react epilogue
}
}
); It is possible to replace the type Ideally we would generate the code in a form that avoids specifying this type explicitly. For instance if we could avoid putting the ports in the parameters of the reaction lambda, we could use TS's type inference to resolve the type of this.addReaction(
[this.startup],
[this.writable(this.d.inp)],
function (this, __d: Delay<number>) { // we don't need to do string replacement here, this is the declared type of d
// =============== START react prologue
const util = this.util;
let d = {inp: __d.inp.get(),} // the type of d is inferred to `{inp: number}` (I suppose)
// =============== END react prologue
try {
d.inp = 42;
} finally {
// =============== START react epilogue
if (d.inp !== undefined) {
__d.inp.set(d.inp)
}
// =============== END react epilogue
}
}
); But I don't know enough about the TS target to know if that's possible easily, or if that has other problems. For instance I don't know what |
I think it's a rather cursed design; in JS you could rebind I don't know the rationale of this design, but changing it require some more efforts...... (Why doesn't gh show the code?!) |
The test in
test/TypeScript/src/target/failing/GenericDelay.lf
should work but does not. There are two issues:extends Present
which is a result of the way we currently deal with present/absentfunction (this, __d_inp: ReadWrite<T>) {
should befunction (this, __d_inp: ReadWrite<number>) {
in the generated code