Key Concepts and Terminology:

  1. Parameterization: Mojo introduces compile-time metaprogramming through parameterization. Parameters are compile-time variables that become runtime constants.
  2. Parameter Expressions: Parameter expressions are Mojo code expressions that occur where a parameter is expected. They support operators and function calls, just like runtime code.
  3. Parameterized Functions: Functions can have parameters in square brackets ahead of the argument list. The compiler resolves parameter values during compilation and creates concrete versions of the function for each unique parameter value.
  4. Parameterized Structs: Structs can also have parameters, allowing the creation of generic containers. The Self type represents a concrete instance of the struct with all its parameters specified.
  5. Overloading on Parameters: Functions and methods can be overloaded based on their parameter signatures. The compiler uses specific rules to resolve overloads.
  6. Optional and Keyword Parameters: Parameters can have default values, making them optional. They can also be passed by keyword, allowing flexibility in specifying only the desired parameters.
  7. Variadic Parameters: Mojo supports variadic parameters, similar to variadic arguments, but with some limitations.
  8. Alias: An alias declaration is used to name compile-time values, similar to how var defines runtime values.
  9. Fully-bound, Partially-bound, and Unbound Types: A parametric type with all its parameters specified is fully-bound. Types can also be partially-bound or unbound, indicating that some or all parameters are unspecified.
  10. Automatic Parameterization: If a function argument type is a partially-bound or unbound type, the unbound parameters are automatically added as input parameters on the function.

Code Examples: Let's break down a code example to illustrate parameterized functions:

codefn repeat[count: Int](msg: String):
    @unroll
    for i in range(count):
        print(msg)

repeat[3]("Hello")

Explanation:

Prerequisites and Background Knowledge: To understand the concepts in this documentation, familiarity with the following topics would be beneficial: