M
Modular2mo ago
Sören

Any way to work around that capturing closures cannot be materialized as runtime values?

I'm trying to implement converting data from an existing, possibly nested data structure into native Mojo collections like List, Dict etc. in a generic way. Here's an example for lists with one approach i've been trying:
@value
struct Column[T: CollectionElement]:
var getter: fn (v: Vector, length: Int, offset: Int) capturing -> List[T]

fn __init__[ChildType: CollectionElement](inout self: Column[List[ChildType]], child: Column[ChildType]):
"""Initialize a list column from a child column."""

fn list_getter(v: Vector, length: Int, offset: Int) capturing -> List[List[ChildType]]:
var l = List[List[ChildType]](capacity=length)
for idx in range(length):
l.append(child.get(v, length, offset + idx))
return l
self.getter = list_getter
# ...
fn get(self, v: Vector, length: Int, offset: Int) -> List[T]:
return self.getter(v, length, offset)
@value
struct Column[T: CollectionElement]:
var getter: fn (v: Vector, length: Int, offset: Int) capturing -> List[T]

fn __init__[ChildType: CollectionElement](inout self: Column[List[ChildType]], child: Column[ChildType]):
"""Initialize a list column from a child column."""

fn list_getter(v: Vector, length: Int, offset: Int) capturing -> List[List[ChildType]]:
var l = List[List[ChildType]](capacity=length)
for idx in range(length):
l.append(child.get(v, length, offset + idx))
return l
self.getter = list_getter
# ...
fn get(self, v: Vector, length: Int, offset: Int) -> List[T]:
return self.getter(v, length, offset)
The issue seems that list_getter needs to close over child in order to access it, but when I add capturing to the function, I get the following error:
TODO: capturing closures cannot be materialized as runtime values
TODO: capturing closures cannot be materialized as runtime values
The TODO seems to indicate that this might be supported in the future, but perhaps there's a way to work around the issue today? Any pointers appreciated.
1 Reply
ModularBot
ModularBot2mo ago
Congrats @Sören, you just advanced to level 2!
Want results from more Discord servers?
Add your server