Michael K
MModular
•Created by aurelian on 10/20/2024 in #questions
why does this String init work without len?
If you don't need the data to be in
buf
anymore then I think you should make buf
a List[Byte]
instead of InlineArray
and then use the String initialization from List (which avoids a copy).52 replies
MModular
•Created by aurelian on 10/20/2024 in #questions
why does this String init work without len?
When you initialize a String from an UnsafePointer the String takes ownership to avoid a copy. So both
buf
and s
own the same pointer here and both try to free it.52 replies
MModular
•Created by aurelian on 10/20/2024 in #questions
why does this String init work without len?
I think you end up using this init from String:
because StringRef has this init which doesn't require the length:
I am not 100% sure I am correct on the UnsafePointer to StringRef auto conversion but I am sure you are not using a
String.__init__
signature requiring a length.
Is you code magically working with the correct String length? The StringRef init I pointed to walks the pointer to find the length.52 replies
MModular
•Created by taalhaataahir01022001 on 5/21/2024 in #questions
x86 Intrinsics
The error is telling you that you are using runtime
var
instead of compile time alias
or parameter. So you can replace var
with alias
on your 3 inputs above or just write them into the function call. And as noted above, the return type is there so that the compile knows what type to assign to the return value. Everything must have a known type at compile time. So AnyType will not work.5 replies
MModular
•Created by Martin Dudek on 5/18/2024 in #questions
How to implement Dependency Injection in Mojo?
I think you are working through an example like "Quackable" here - https://docs.modular.com/mojo/manual/traits#using-traits. A trait is not a concrete type so I don't think it can be used as an argument type. The type can be inferred at the call site though so you don't need to use the square brackets when calling
lets_print
. Lastly there is a note in the link saying they hope to make the syntax less verbose in the future.10 replies
MModular
•Created by tzl on 5/14/2024 in #questions
Why is this in-place operation not modifying the Python object in-place?
Your signature needs to be
def do_numpy_stuff(inout ar: PythonObject) -> PythonObject:
. Note the addition of inout
. That means changes to ar
inside the function also show up outside the function. Otherwise you are working with a copy and as you see the changes don't get reflected.
I am not sure if this will always be the case for def
functions since the expectation in Python is that everything is inout
.8 replies
MModular
•Created by Analista on 5/13/2024 in #questions
How to read args from environment
Here is an example using it. The gist is that
argv()
returns a VariadicList
so you can index into it, iterate over it, or get the length to work with the arguments. The docs on VariadicList are here.5 replies
MModular
•Created by Maxim on 1/17/2024 in #questions
how to use the gather function
Documentation is here. Example usage is here. 😃 If you really want to use the LLVM intrinsic, example usage is here.
6 replies
MModular
•Created by bunny on 4/22/2024 in #questions
Type State Pattern
This seems fine for now: https://discord.com/channels/1087530497313357884/1098713601386233997/1209357999529926696
42 replies
MModular
•Created by Henk-Jan Lebbink on 2/10/2024 in #questions
How to rewrite this code into something not ugly
10 replies
MModular
•Created by Maxim on 1/17/2024 in #questions
how to use the gather function
I am not sure much larger than SIMD width will ever be faster but you would need to experiment to check. There are trade offs between pure SIMD speed, number of reads from heap, and how cache gets used. So testing different widths in your actual use case is worth doing.
6 replies
MModular
•Created by Maxim on 1/17/2024 in #questions
how to use the gather function
Leandro Campos, has a branch with tests demonstrating how to do this. That exercises the new methods he is adding to DTypePointer. The use of the intrinsic itself is here.
The pointer manipulation to use the intrinsic looks simple now that someone else figured it out for me. 🤦♂️ 🤷 😃
6 replies
MModular
•Created by Tuatini on 4/6/2024 in #questions
official package manager
I think it will come from Modular. It is being planned and if you want to have input comment on this proposal.
4 replies
MModular
•Created by artemiogr97 on 4/4/2024 in #questions
Lifecycle doc clarification
Int
and Pointer
are register passable and "trivial". So they can only be copied. For simple things in register copying is more efficient than moving.5 replies
MModular
•Created by AndryxaUA on 3/17/2024 in #questions
What is the size of the Int type in bits?
The thread with that answer also mentions the difficulty supporting 128-bit curently.
14 replies
MModular
•Created by AndryxaUA on 3/17/2024 in #questions
What is the size of the Int type in bits?
I think it can vary by system to be 32- or 64-bit but is currently 64-bit because Mojo isn't targeting any 32-bit systems. See this previous answer from Modular. This is the MLIR Index type that answer refers to.
14 replies
MModular
•Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
I don't think you can use a trait as the parameter when you need to make a concrete type. You need to pass an actual fully known type to Dict. using
Pet[YourPet]
works for me. If you want a Dict that holds a Pet with T either MyPet or YourPet, I think you would need to use a Variant[MyPet, YourPet]. I am not sure how well that will work but that is how you allow for two possible concrete types.9 replies
MModular
•Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
You need to provide the complete type as a parameter to Dict. Pet requires a parameter T and without that being specificed it is not concrete type that can be passed to Dict. so like this:
Also I think Dict now works witht a plain String. No need to use StringKey.
9 replies
MModular
•Created by EzRyder on 3/14/2024 in #questions
Mojo Dict: Store struct instances based on a common trait as CollectionElement.
Mark Pet as conforming to CollectionElement. Like this:
If it is a simple struct that is not allocating or owning any pointers then mark it with
@value
decorator. That will ensure it has the functions necessary to be moved and copied the way Collections expect.9 replies
MModular
•Created by swig. on 3/9/2024 in #questions
Defining A DynamicVector containing References
I think you would probably want your vector to own the original objects and then it can use
__refitem__
to return your References. I am not 100% clear on what you are trying to achieve but there are some examples of using References and linking them to lifetimes here.
But for a lot of uses Ehsan is probably correct, though some cool stuff is already possible.5 replies