M
Modular4mo ago
toasty

Is it possible to convert an Int to a String at comp time?

Hey all, I'm trying to make some comp time friendly structs for my project. I'm struggling with converting an int to a string for some string concatenations. str() does not work at comp time. Minimal example to reproduce the error.
fn main() -> None:
alias s = str(1)
fn main() -> None:
alias s = str(1)
10 Replies
benny
benny4mo ago
well i guess my question would be, if it’s at comp time then you know the value, why not just use a string?
sora
sora4mo ago
I think this should work. Could you file an issue on GH
toasty
toasty4mo ago
Here's an example of where it's blocking
@value
struct ANSIColor(Color, Stringable):
"""ANSIColor is a color (0-15) as defined by the ANSI Standard."""
var value: Int

fn sequence(self, is_background: Bool) -> String:
var modifier: Int = 0
if is_background:
modifier += 10

if self.value < 8:
return str(modifier + self.value + 30)
else:
return str(modifier + self.value - 8 + 90)
@value
struct ANSIColor(Color, Stringable):
"""ANSIColor is a color (0-15) as defined by the ANSI Standard."""
var value: Int

fn sequence(self, is_background: Bool) -> String:
var modifier: Int = 0
if is_background:
modifier += 10

if self.value < 8:
return str(modifier + self.value + 30)
else:
return str(modifier + self.value - 8 + 90)
I need to perform some operations with it as an Int, then return a String.
benny
benny4mo ago
got it, understood
toasty
toasty4mo ago
I'll file an issue and see what happens, thanks! Wasn't sure if I was just doing something that doesn't make sense at comp time
benny
benny4mo ago
i agree that this is an issue but i also think there could be a workaround unsafe but maybe try creating a string with the necessary space then putting the correct values into the buffer for it not sure if that would work at comp time tho
toasty
toasty4mo ago
Worth a try! I'm not too pressed to make it work immediately, but whenever it's possible it'll be nice time saver haha.
Three chickens in the green bag
Whats the issue number :)?
toasty
toasty4mo ago
GitHub
[BUG] Mojo crashes when attempting run str() during compilation. ...
Bug description Running str() within in an alias causes the compiler to crash. I think we should be able to convert Stringables to String during compilation unless I'm missing something. Thanks...
ModularBot
ModularBot4mo ago
Congrats @toasty, you just advanced to level 5!
Want results from more Discord servers?
Add your server