Either I'm holding something wrong or function fields aren't callable at the moment.
This test will fail to compile even though I would assume it should:
/// Expect:
/// - output: "PASS\nPASS\n"
struct FunctionHaver {
cb: fn() -> String
fn call_cb(this) -> String {
return .cb()
}
}
fn main() {
let haver = FunctionHaver(cb: fn() => "PASS")
println("{}", haver.cb())
println("{}", haver.call_cb())
}
Jakt error:
Error: Could not find βcbβ
ββββ¬β src/function_as_a_property.jakt:8:16
7 β fn call_cb(this) -> String {
8 β return .cb()
β βββββ¬
β β°β Could not find βcbβ
9 β }
ββββ΄β
Error: Could not find βcbβ
βββββ¬β src/function_as_a_property.jakt:15:19
14 β
15 β println("{}", haver.cb())
β ββββββββββ¬
β β°β Could not find βcbβ
16 β println("{}", haver.call_cb())
βββββ΄β
I tried assigning the function as a temp var first. This will codegen, but the resulting cpp won't compile.
/// Expect:
/// - output: "PASS\nPASS\n"
struct FunctionHaver {
cb: fn() -> String
fn call_cb(this) -> String {
let cb = this.cb
return cb()
}
}
fn main() {
let haver = FunctionHaver(cb: fn() => "PASS")
let cb = haver.cb
println("{}", cb())
println("{}", haver.call_cb())
}
C++ Error:
build/function_as_a_property.cpp:14:40: error: call to deleted constructor of 'const Function<AK::DeprecatedString ()>'
Function<DeprecatedString()> const cb = ((haver).cb);
^ ~~~~~~~~~~~~
jakt/build/include/runtime/AK/Function.h:67:25: note: 'Function' has been explicitly marked deleted here
AK_MAKE_NONCOPYABLE(Function);
^
build/function_as_a_property.cpp:38:40: error: call to deleted constructor of 'const Function<AK::DeprecatedString ()>'
Function<DeprecatedString()> const cb = ((*this).cb);
^ ~~~~~~~~~~~~
jakt/build/include/runtime/AK/Function.h:67:25: note: 'Function' has been explicitly marked deleted here
AK_MAKE_NONCOPYABLE(Function);
Makes sense to me that the function isn't copyable, but probably this should fail earlier? π€
Avoiding copying by using a pointer will work for outside property access.
/// Expect:
/// - output: "PASS\n"
struct FunctionHaver {
cb: fn() -> String
}
fn main() {
let haver = FunctionHaver(cb: fn() => "PASS")
let cb = &haver.cb
println("{}", cb())
}
However, it won't work for the method access case because the compiler thinks the reference is outlived (is that right? π€):
/// Expect:
/// - output: "PASS\nPASS\n"
struct FunctionHaver {
cb: fn() -> String
fn call_cb(this) -> String {
let cb = &.cb
return cb()
}
}
fn main() {
let haver = FunctionHaver(cb: fn() => "PASS")
let cb = &haver.cb
println("{}", cb())
println("{}", haver.call_cb())
}
Jakt error:
Error: Cannot assign a reference to a variable that outlives the reference
βββββ¬β src/function_as_a_property.jakt:8:18
7 β fn call_cb(this) -> String {
8 β let cb = &.cb
ββ°β Cannot assign a reference to a variable that outlives the reference
10 β return cb()
βββββ΄β
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too