We use cookies on this site to enhance your user experience
By clicking the Accept button, you agree to us doing so. More info on our cookie policy
We use cookies on this site to enhance your user experience
By clicking the Accept button, you agree to us doing so. More info on our cookie policy
Mojo By Example: A Comprehensive Introduction to the Mojo Programming Language (ruhati.net)
title:
style: nestedList # TOC style (nestedList|nestedOrderedList|inlineFirstLevel)
minLevel: 0 # Include headings from the specified level
maxLevel: 0 # Include headings up to the specified level
includeLinks: true # Make headings clickable
debugInConsole: false # Print debug info in Obsidian console
SIMD[dtype, size]
dtype = Dtype.int/ Dtype.float64 ....
[Types | Modular Docs](https://docs.modular.com/mojo/manual/types) |
Basic numerical types defined in Mojo are actually SMID’s of size 1.
Int8 = Scalar[DType.int8]
Float64 = SMID[DType.float64, 1]
Scalar = SIMD[size=1]
var k = 3
k = 3
^
transfer operator: to move the value, unless the value is a trivial type (like Int
) or a newly-constructed, “owned” value.owned
keyword does not guarantee that the received value is the original value—it guarantees only that the function gets unique ownership of a value (enforcing value semantics). This happens in one of three ways:^
transfer operator, which ends the lifetime of that variable (the variable becomes uninitialized) and ownership is transferred into the function without making a copy of any heap-allocated data.^
transfer operator, in which case, the value is copied into the function argument and the original variable remains valid. (If the original value is not used again, the compiler may optimize away the copy and transfer the value).Int
, Float
, and SIMD
are passed directly in machine registers instead of through an extra indirection.compile time temporary value that cant change at runtime
__init__()
, __copyinit__()
, and __moveinit__()
fn function_name[p:dtype #parameter](k:dtype #argument) -> dtype:
def function_name():
[]
to access value.@parameter
decorator on an if
statement or on a nested function to run that code at compile time.UnsafePointer
can dynamically allocate and free memory, or to point to memory allocated by some other piece of code.pointee : value pointed to by a pointer. dereference : retrieve or update pointee using
ptr[]
Check LinkedList for Memory lifecycle example.
var ptr: UnsafePointer[Int]
var ptr: UnsafePointer[Int]()
var ptr = UnsafePointer[dtype].alloc(n)
initialize_pointee_copy(ptr, value) -- copy
or
initalize_pointee_move(ptr, value^) -- move
or
ptr = UnsafePointer[Int].address_of(value) -- assign
ptr.free()
destroy_pointee
: Destroy the pointed-to value.move_from_pointee
: Move the value at the pointer out.initialize_pointee_move
: Emplace a new value into the pointer location, moving from value
.initialize_pointee_copy
: Emplace a copy of value
into the pointer location.move_pointee
: Moves the value src
points to into the memory location pointed to by dest
DTypePointer
: handling numeric dataA DTypePointer
is an unsafe pointer that supports some additional methods for loading and storing numeric data. Like the SIMD
type, it’s parameterized on DType
as described in SIMD and DType.
DTypePointer
works with are trivial types, so no copy or move or destroy methods required.alloc()
, free()
and address_of()
[]
or use the load()
and store()
methods to access values.simd_strided_load()
and simd_strided_store()
methods for efficient SIMD operationsNote: DTypePointer
is depreciated. UnsafePointer
can handle most things that DTypePointer
does except for SIMD operations. In future SIMD
type supports all these operations, so UnsafePointer
can just use them.
Unsafe pointers are unsafe for several reasons:
UnsafePointer
and DTypePointer
values are nullable or can be initialized.UnsafePointer
, so managing memory and knowing when to destroy objects is user responsibility.The Reference
type is essentially a safe pointer type.
Reference
is non-nullable. A reference always points to something.Reference
— only point to an existing value.Reference
only refers to a single value. You can’t do pointer arithmetic with a Reference
.Reference
has an associated lifetime, which connects it back to an original, owned value. The lifetime ensures that the value won’t be destroyed while the reference exists.The Reference
type shouldn’t be confused with the immutable and mutable references used with the borrowed
and inout
argument conventions. Those references do not require explicit dereferencing, unlike a Reference
or UnsafePointer
.
#### Arc Reference-counted smart pointers
Latest Posts