Xcur
Xcur
MModular
Created by Xcur on 1/8/2024 in #questions
Alias does not work with raises String methods
I have a string i know at compile time and I was writing a function to split it into each of the new lines, but because split has a raises flag it cannot be run at compile time and stored as an alias. I noticed there is a TODO to allow this, and im curious when will alias work with potentially raising functions? Here is my sample code:
alias string: String = \
"""Hello World
Hello World
Hello World"""

# does not work due to raises flag
alias lines = string.split("\n")

fn main() raises:
# uncomment and this works
# let lines = string.split("\n")

for i in range(len(lines)):
print(lines[i])
alias string: String = \
"""Hello World
Hello World
Hello World"""

# does not work due to raises flag
alias lines = string.split("\n")

fn main() raises:
# uncomment and this works
# let lines = string.split("\n")

for i in range(len(lines)):
print(lines[i])
2 replies
MModular
Created by Xcur on 12/8/2023 in #questions
Can i have a list of structs with different parameters?
Lets say I have a struct like the one below, is it possible to have a list of structs that have different values inside their compile time parameters? This is just a sample struct, I know it's trivial and you wouldn't create a Name struct but this is just demonstrate the concept.
trait Proto(Stringable, CollectionElement):
fn print(self) -> None: ...

struct Name[DataSize: Int](Proto):
var name: String

fn __init__(inout self, owned name: String) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
var result: String = ""
for i in range(DataSize):
result += self.name[i]
return result

fn print(self) -> None:
print(self)
trait Proto(Stringable, CollectionElement):
fn print(self) -> None: ...

struct Name[DataSize: Int](Proto):
var name: String

fn __init__(inout self, owned name: String) -> None:
self.name = name^

fn __moveinit__(inout self, owned existing: Self) -> None:
self.name = existing.name^

fn __copyinit__(inout self, other: Self) -> None:
self.name = other.name

fn __str__(self) -> String:
var result: String = ""
for i in range(DataSize):
result += self.name[i]
return result

fn print(self) -> None:
print(self)
I could only implement it if I had DataSize as the same for each, but I would like to have a list of different DataSize name structs.
# this worked
var names = DynamicVector[Name[DataSize = 5]]()
# this does not work
var names = DynamicVector[Name]()
# this worked
var names = DynamicVector[Name[DataSize = 5]]()
# this does not work
var names = DynamicVector[Name]()
Is there a way to abstract it away, by using traits with all the necessary methods and then a wrapper struct that holds a name field with the trait as its type that I put each Name struct into? Then I could put the wrapper struct as a parameter in DynamicVector?
2 replies
MModular
Created by Xcur on 12/5/2023 in #questions
Mojo GPU Syntax or specific GPU stdlib data types?
I'm looking forward to using MAX, I'm curious though outside of MAX how will the GPU syntax look? Will we need to use different data types other than SIMD that will be specific for GPU's like what is kind of done with cuda in C++? I'm hoping to be able to test this outside of AI paradigms that I couldn't use MAX for, like maybe improving the speed of opencv-like smoothing kernels or ray tracing.
4 replies
MModular
Created by Xcur on 11/14/2023 in #questions
How will the syntax look when mojo supports GPUs?
I'm wondering when people build operators like matmul will they have to write a function for CPU and a very different function for the GPU? Or will the syntax remain relatively similar, and the same function can be used on both? It will just take advantage of hardware acceleration when it is available.
2 replies