How to read the Mojo documentation effectively

Am in the process of learning mojo and i want to know how to read the documenation effectively. For the most part the documentation is quite straight forward. , but ive come across some sections that trip me up. For example when going through the section on collections we have Dynamic vectors , and underneath dynamic vectore we have the description of the data structure and then the methods associated with it. Now to create a dynamic vector the syntax is var vect = DynamicVectordata type . the constructor details are as follows
__init__(inout self: Self, capacity: Int)
Constructs a vector with the specified capacity.
Args:
capacity (Int): The requested capacity of the vector.
__init__(inout self: Self, capacity: Int)
Constructs a vector with the specified capacity.
Args:
capacity (Int): The requested capacity of the vector.
Given this constructor information, I'm trying to figure out how the syntax var vect = DynamicVectordata type is derived from it. How can I interpret this constructor documentation to understand the syntax for initializing a DynamicVector.
4 Replies
ModularBot
ModularBot12mo ago
Congrats @shamal_de_silva, you just advanced to level 1!
Michael K
Michael K12mo ago
In the description of the data structure it lists the single parameter of DynamicVector as "T (CollectionElement): The type of the elements." . That is where DynamicVector[datatype] portion of the initialization comes from. Any time a DynamicVector is created you need to provide the datatype which will be referred to as T within any of the structs methods.. That the initialization requires on argument (capacity) to be provided is from the constructor details __init__(inout self: Self, capacity: Int). By the conventions of Python and therefore Mojo you don't need to write out __init__ and the first argument self is provided automatically when calling a struct's instance methods. self is the varialbe containing the instances fields and parameters if you need to refer to inside the instance method so it is always provided in the function definition but doesn't need to be passed explicitly. So the remaining argument in only capacity which is of type Int. So to initialize a DynamicVector you need to specify the struct with parameters and call its __init__method with any needed argments. So as DynamicVector[DType.int32](10) for example. Lastly on __init__, any of the functions specified as words surrounded by double underscores are called 'dunder' methods and will rarely appear wriiten out in code. The all have calling shorthands most of which are obvious by the names.
shamal_de_silva
shamal_de_silvaOP12mo ago
thanks so much @Michael K :mojo:
Michael K
Michael K12mo ago
You also might do well to find some Python documentation introducing concepts like dunder methods. Mojo is just adopting them and since Python is so ubiquitous the assumption is that people starting in Mojo will already be familiar with them.
Want results from more Discord servers?
Add your server