I’m comming from Python and I’m very interested on Julia due to its string macros feature.
Is it possible to implement template metaprograming and Metaclasses (Python has) just creating macros?
I’m comming from Python and I’m very interested on Julia due to its string macros feature.
Is it possible to implement template metaprograming and Metaclasses (Python has) just creating macros?
Welcome!
Julia has a lot of great tools for metaprogramming (see the docs), but “template metaprogramming” (something I’m very familiar with in C++) and “metaclasses” (something I’m vaguely aware of from Python) aren’t terms that are generally used to describe it.
Instead, we generally use macros to transform some convenient syntax into something more interesting and useful. For example, the @kwdef
macro takes a type definition and turns it into a new type definition with convenient keyword-based constructors:
julia> using Base: @kwdef
julia> @kwdef struct Foo
x::Int = 5
y::String = "hello"
end
Foo
julia> Foo(y = "world", x = 2)
Foo(2, "world")
That’s pretty similar to the kind of thing you might do with a python metaclass, but approached from a less OOP direction.
Do you have some specific problem you want to solve?
Do you have some specific problem you want to solve?
I’m not an experienced programmer, what I do is some scripts to automate some tasks.
What I’m seeking if for an universal programming language.
I want to choose the language with most advanced and feature full metaprogramming facilities for doing all kinds of programming things.
I’m young and I’m choosing a programming language that will stay by my side the rest of my life. It’s just like choosing the right girlfriend, it takes a lot of time. =)
But, is it possible to implement these two features in Julia via its metaprogramming? <= Depending on the answer I will choose Julia as my definite programming language.
I really do not recommend this approach.
Programming languages are all about trade-offs, and part of being a good programmer is knowing these trade-offs and what is the best tool for each job.
Also, programming evolves, new languages can supplant old ones because were designed with longer experiences of previous languages and do not have to keep the compatibility.
What you can do, is to choose a mainstream language that is good for scripting and general programming; like Python. However, you have to pay a price, that in this case, is the extreme slowness of Python (which is little relevant if all you do are scripts).
Short answer, Julia’s metaprogramming facilities can do anything you want. The question isn’t whether you can do something, but what is the best way to do it and is it worth doing (compared to doing something else).
Julia’s type system and optimizing compiler allow you to do the work of template metaprogramming with less work and cleaner, more readable syntax. I encourage others to share examples, but one I know about is handling arbitrary-dimensional arrays, a task that would commonly be handled by template metaprogramming in C++.