M
Modularβ€’4d ago
toasty

Is it possible to get the origin of `strs` in `fn name(*strs: String)`?

Minimal repro, LSP shows invalid call to 'append': method argument #0 cannot be converted from 'List[StringSlice[strs]]' to 'List[StringSlice[strs]]' Seems like a bug, but maybe there's a proper way to do this?
from utils import StringSlice


fn split(*strs: String):
var blocks: List[List[StringSlice[__origin_of(strs)]]]
for s in strs:
blocks.append(s[].as_string_slice().splitlines())


fn main():
split("a", "b", "c")
from utils import StringSlice


fn split(*strs: String):
var blocks: List[List[StringSlice[__origin_of(strs)]]]
for s in strs:
blocks.append(s[].as_string_slice().splitlines())


fn main():
split("a", "b", "c")
9 Replies
Martin Vuyk
Martin Vuykβ€’4d ago
Hey @toasty cool to see my code being used in ways I never imagined 🀣 . No that won't work, you're trying to use the origin of the VariadicList as the origin of the strings themselves. This is a hard limitation of our current origin system, you can't have a heterogeneous list which is what this would need to be to be able to carry the origin of each of the strings inside the VariadicList. Now, what I like about Mojo is that you can point a πŸ”« to the compiler's head and say "trust me bro":
from utils import StringSlice


fn main():
items = List[String]("a", "b", "c")
alias S = StringSlice[ImmutableOrigin.cast_from[__origin_of(items)].result]
blocks = List[List[S]](capacity=len(items))
for s in items:
blocks.append(
rebind[List[S]](rebind[S](s[].as_string_slice()).splitlines())
)
for item in items:
print(item[], end=" ")
print("")
from utils import StringSlice


fn main():
items = List[String]("a", "b", "c")
alias S = StringSlice[ImmutableOrigin.cast_from[__origin_of(items)].result]
blocks = List[List[S]](capacity=len(items))
for s in items:
blocks.append(
rebind[List[S]](rebind[S](s[].as_string_slice()).splitlines())
)
for item in items:
print(item[], end=" ")
print("")
basically this is the hacky way of binding the origin to the list of strings itself
toasty
toastyOPβ€’4d ago
Ahhh okay, now it makes sense with that explanation! I thought it would take a union of all the origins through some magic πŸ˜‚
Martin Vuyk
Martin Vuykβ€’4d ago
I have a PR open on nightly which should remove the need for rebind[S](s[].as_string_slice()).splitlines() well probably the VariadicList is a union of the origins now that I think about it
toasty
toastyOPβ€’4d ago
I appreciate the examples in coercing Mojo to do what I want haha
Martin Vuyk
Martin Vuykβ€’4d ago
yeah not very adviced to the general public but I know you want to get other things done lol
toasty
toastyOPβ€’4d ago
I wanted to see how far I could go without allocating new strings, but avoiding the deep arcane stuff. I probably won’t implement the origin casting, but good to know nonetheless! Doing a lot of split lines -> iterate over code points -> apply ansi sequences and padding -> rejoin
Martin Vuyk
Martin Vuykβ€’4d ago
actually it should also remove the need for the shady casting ImmutableOrigin.cast_from[__origin_of(items)].result yep, I know that's a very common use case you might want to copy some code from this PR I want to introduce a lazy iterator over splitlines
toasty
toastyOPβ€’4d ago
All of your recent changes have brought it very far πŸ™πŸΎ working with strings has felt much better I’ll take a look, thanks!
Martin Vuyk
Martin Vuykβ€’4d ago
Thanks a lot πŸ˜„ . My goal is to make it feel like Python in that sense. It's one of the things I love most about it
Want results from more Discord servers?
Add your server