bpr
bpr
MModular
Created by Hammad Ali on 11/10/2024 in #questions
Struct containing a list of itself?
I just compiled and ran your code on nightly and it worked. My Rust instincts are aligned with @toasty, and I thought you'd need to ensure you had a reference to the Node type.
5 replies
MModular
Created by jmky on 10/7/2024 in #questions
Is there a disdain for Rust from the Mojo team?
Why is it that when we store the result of return_immutable_ref from main9 into a variable, we get a mutable deep copy of the original list?
from collections import InlineList

# Utility fn for printing list
fn fmt_list[T: StringableCollectionElement](l: List[T]) -> String:
if len(l) == 0:
return "[]"
var res: String = "["
res += str(l[0])
for i in range(1, len(l)):
res += ", "
res += str(l[i])
res += "]"
return res

# return immutable auto dereference
fn return_immutable_ref[L:ImmutableOrigin](ref[L]arg: List[String])->
ref[L]List[String]:
return arg

fn main():
var a: List[String] = List(str("One"), str("Two"), str("Three"))
var imm_a = return_immutable_ref(a)
a[1] += "2"
print("a is ", fmt_list(a))
print("imm_a is ", fmt_list(imm_a))
var il = InlineList[String]("Four", "Five", "Six")
for s in il:
imm_a.append(str(s[]))
imm_a[0] += "1"
print("a is ", fmt_list(a))
print("imm_a is ", fmt_list(imm_a))
from collections import InlineList

# Utility fn for printing list
fn fmt_list[T: StringableCollectionElement](l: List[T]) -> String:
if len(l) == 0:
return "[]"
var res: String = "["
res += str(l[0])
for i in range(1, len(l)):
res += ", "
res += str(l[i])
res += "]"
return res

# return immutable auto dereference
fn return_immutable_ref[L:ImmutableOrigin](ref[L]arg: List[String])->
ref[L]List[String]:
return arg

fn main():
var a: List[String] = List(str("One"), str("Two"), str("Three"))
var imm_a = return_immutable_ref(a)
a[1] += "2"
print("a is ", fmt_list(a))
print("imm_a is ", fmt_list(imm_a))
var il = InlineList[String]("Four", "Five", "Six")
for s in il:
imm_a.append(str(s[]))
imm_a[0] += "1"
print("a is ", fmt_list(a))
print("imm_a is ", fmt_list(imm_a))
23 replies
MModular
Created by a2svior on 9/30/2024 in #questions
Runtime Reflection in Mojo?
TIL something new!
16 replies
MModular
Created by a2svior on 9/30/2024 in #questions
Runtime Reflection in Mojo?
I'd find it helpful to see a whole bunch of examples of MLIR reflection with type system access before accepting a pronouncement about how they fare vs macros. Currently, I have no idea what it's supposed to be. Also, while I like Rust, I wouldn't put it's macro system up as the only macro system to compare with. There's also Nim, Scala, and Typed Racket, amongst others.
16 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
Show me what you mean. Please rewrite that code with your refined version of variant and your union type sugar. I find Mojo's Variant unwieldy even compared to Nim's object variant and Ada's variant records. In those, you have a tag field to discriminate the cases rather than some generic type. If that's the refinement you have in mind, that and the addition of Python 3.10+ match-case construct would go a long way to making it look better in my eyes.
44 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
If Mojo had enums like Rust, that solution would be nicer.
44 replies
MModular
Created by banananas on 9/23/2024 in #questions
"Linking" structs together?
from utils.variant import Variant

@value
struct MyStruct(CollectionElement):
var name: String
fn __str__(self) -> String:
return self.name

@value
struct MyOtherStruct(CollectionElement):
var id: Int64
fn __str__(self) -> String:
return str(self.id)

alias MyObject = Variant[MyStruct, MyOtherStruct]

def main():
var list = List[MyObject]()
list.append(MyObject(MyStruct("Brian")))
list.append(MyObject(MyOtherStruct(23)))

for i in range(len(list)):
v = list[i]
if v.isa[MyStruct](): print(str(v[MyStruct]))
if v.isa[MyOtherStruct](): print(str(v[MyOtherStruct]))
from utils.variant import Variant

@value
struct MyStruct(CollectionElement):
var name: String
fn __str__(self) -> String:
return self.name

@value
struct MyOtherStruct(CollectionElement):
var id: Int64
fn __str__(self) -> String:
return str(self.id)

alias MyObject = Variant[MyStruct, MyOtherStruct]

def main():
var list = List[MyObject]()
list.append(MyObject(MyStruct("Brian")))
list.append(MyObject(MyOtherStruct(23)))

for i in range(len(list)):
v = list[i]
if v.isa[MyStruct](): print(str(v[MyStruct]))
if v.isa[MyOtherStruct](): print(str(v[MyOtherStruct]))
44 replies
MModular
Created by DobyDabaDu on 8/17/2024 in #questions
Mojo Build size
Right, I should have left the C out of the comparison as it was misleading. In any case, the stripped Mojo is close enough to the stripped Rust that I wouldn't sweat the size. I hope that future versions of "mojo build" will provide optimization flags that will allow us to make tradeoffs, but the biggest size one here is, as you said, dynamic vs static linking.
14 replies
MModular
Created by DobyDabaDu on 8/17/2024 in #questions
Mojo Build size
Running strip on Ubuntu takes the size of the Mojo "hello world" executable from 4MB to 350K. For comparison, a C version is 16K and a Rust version built with the "--release" flag is 390K on my machine, and strips down to 327K.
14 replies