Of all programming languages available now, Mojo has best chance to make the biggest positive difference. With simple and relatively clean syntax of Python, while with types, it also has performances often better than C++, and even memory safety of Rust.
But the biggest surprise in Mojo meta-programing: the same language syntax can be used for "compile time" as well as for "run time". Meaning, for what is usually macros, templates or generics, Mojo is using the same syntax as regular language! The only other modern language with similar feature is Zig language. Or Lisp.
Mojo (programming language) - Wikipedia
excellent interview with creator and lead of Mojo language and related tools.
Mojo and Building a CUDA Replacement with Chris Lattner - Software Engineering Daily
Mojo is a new programming language designed to combine the simplicity of Python with the performance of C and the safety of Rust. It also aims to provide a vendor-independent approach to GPU programming. Mojo is being developed by Chris Lattner, a renowned systems engineer known for his seminal contributions to computer science, including LLVM, the Clang compiler, and the Swift programming language.The cool thing about Mojo is that you can just write code, and you can use normal code, you can use a list or an array, or a string, or like whatever data type that you want at comp time, at the compiler runtime. So now, Mojo has a very clean division between the code that runs runtime, the code that you can use at compile time, and when you're writing these algorithms, it's the same code, and that becomes very powerful.
...there's no trade-off. There's no downside. It strictly makes the language more simple, more consistent, more powerful. There's no trade-off. It makes the compiler more different than previous generation languages, right? But if you look at, again, I'm fond of Mojo. Of course, I'll say nice things about other people's systems. If you go look at Zig, Zig's a relatively very simple language, but has very powerful metaprogramming and generics capabilities. So, they made other decisions in other parts of the language, so they decided not to have a bar checker because that was part of what they were going for, but powerful metaprogramming doesn't have to come with complexity.
Course: The Complete Python Course 2025 | Udemy Business (Mojo section)
by Luka Anicin and Chris HarounDeveloper Console (Mojo online playground)
A common use case for metaprogramming is creating generic functions and types; Python can do this implicitly via duck typing. The Python interpreter is generally unconcerned with the type of a variable until it comes time to perform some operation on it, so as long as the type you provide to a function satisfies those definitions, everything will move along just fine.Without using generics, statically typed languages like Mojo need to rely on creating multiple definitions of that function in order to handle different input types:
return i + i
fn doubler(f: Float64) -> Float64:
return f + f
All parameters must be types or expressions that are known at compile time. Mojo uses traits to inform the compiler about the kinds of types that a type parameter can bind to. Using traits, we can create an Addable trait"This function operates on all types that have the Addable trait and as a result, implement the __add__ method." Compiler then does the work of generating a different function for each data type required.
fn __add__(self, other: Self) -> Self:
... # this is actual syntax
@no_inline
fn doublerx[T: Addable](x: T) -> T:
return x + x
var a = doubler(2)
var b = doubler(20.0)
print(a,b)
var ax = doublerx(2)
var bx = doublerx(20.0)
print(ax,bx)
No comments:
Post a Comment