Closed plonk-75 closed 5 months ago
This also rises another question. Suppose we have a function returning a function:
pub fn iReturnAFn(a: float, b:float) -> fn(float) -> float = {
fn(param: float) -> {
// some magic happening here, setting variable result to a float
result
}
}
Is the returned function sync or async and how is it determined? Should I open a new issue for this question?
No that is a bug in the compiler. Will be fixed in the next release.
As for the sync/async: At the moment all lambda-expressions (like fn(param: float) -> { ... }
) are sync-functions.
I think it would be possible to also support async-lamdbas if there is a need for it
Should work in 0.4.1.3
KS2 0.4.1.3 only partially fixes the issue on my side.
While now the first example compiles fine ...
pub type FnTypeA = fn() -> float
pub type FnTypeB = fn(float) -> float
pub fn fn2(v: Vessel, a: float, typeAFn: FnTypeA, typeBFn: FnTypeB) -> bool = {
const aRes = typeAFn() // compiles fine now
const bRes = typeBFn(a) // compiles fine now
false
}
Result: No error as expected
...problems arise when using the returned values:
pub type FnTypeA = fn() -> float
pub type FnTypeB = fn(float) -> float
pub fn fn3(v: Vessel, a: float, typeAFn: FnTypeA, typeBFn: FnTypeB) -> bool = {
const aRes = typeAFn() // compiles fine now
const bRes = typeBFn(a) // compiles fine now
if(aRes < bRes) return true // ERROR: IncompatibleTypes Cannot Lt a Unit with a Unit
false
}
Result:
ERROR:IncompatibleTypes Cannot Lt a Unit with a Unit
ERROR: InvalidType Condition of if is not a boolean
I still can't get the second sample to compile, no changes in new KS2 version here
pub type myScalarType = float
pub type FnTypeC = fn(myScalarType) -> myScalarType // compiles fine ...
// ERROR: InvalidType - Expected parameter dt of lambda to have type myScalarType, found myScalarType
pub sync fn getFnTypeC() -> FnTypeC = {
fn(dt: myScalarType) -> {
1.2345
}
}
Result:
ERROR: InvalidType Expected parameter dt of lambda to have type myScalarType, found myScalarType
These cases should work now in 0.4.2.1
There still might be some remaining quirks with type-aliases though.
Fix confirmed. Thank you very much! Regarding remaining quirks, I hope you won't mind me submitting more issues as soon as I find them.
Closing this for now
KS2 0.4.1 / KSP 0.1.2.0.22258
Maybe I do not fully understand the correct use of
type
but I get some interesting error messages. A funny one isERROR: InvalidType - Expected parameter dt of lambda to have type myScalarType, found myScalarType
.I tried a simple type definition as well:
Is that some kind of ID10T error on my side, or a compiler bug?