Julia Experience language feature meta programming, macro

Source: Internet
Author: User

On the basis of language, release-1.1, the individual feels that the language is far from the ideal country of his heart. This language because the audience is not only the programmer has a lot of confusing design, but it is strange that its syntax and other imagery design, although it is not intended for programmers, the intrinsic is provided a large number of non-programmers are not available advanced features, libraries.

With enthusiasm still, I pick some interesting things to write again.

Meta-programming

Metaprogramming is code that processes code that can be Meta.parse() expressed using the class AST that resolves the parameter code, or it can be quote ... end simplified:

julia> multiStmt = Meta.parse(raw"a=1;b=2;t=a;a=b;b=t;println(a,b)"):($(Expr(:toplevel, :(a = 1), :(b = 2), :(t = a), :(a = b), :(b = t), :(println(a, b)))))julia> typeof(multiStmt)Exprjulia> ast = quote       x=1       y=2       res=x+y       endquote    #= REPL[21]:2 =#    x = 1    #= REPL[21]:3 =#    y = 2    #= REPL[21]:4 =#    res = x + yendjulia> typeof(ast)Expr

Use dump() to get a more readable representation:

julia> dump(multiStmt)Expr  head: Symbol toplevel  args: Array{Any}((6,))    1: Expr      head: Symbol =      args: Array{Any}((2,))        1: Symbol a        2: Int64 1    2: Expr      head: Symbol =      args: Array{Any}((2,))        1: Symbol b        2: Int64 2    3: Expr      head: Symbol =      args: Array{Any}((2,))        1: Symbol t        2: Symbol a    4: Expr      head: Symbol =      args: Array{Any}((2,))        1: Symbol a        2: Symbol b    5: Expr      head: Symbol =      args: Array{Any}((2,))        1: Symbol b        2: Symbol t    6: Expr      head: Symbol call      args: Array{Any}((3,))        1: Symbol println        2: Symbol a        3: Symbol b

Expr has two parts, expr.head representing the type of the expression, expr.args indicating the remaining arguments:

julia> multiStmt.head:topleveljulia> multiStmt.args6-element Array{Any,1}: :(a = 1) :(b = 2) :(t = a) :(a = b) :(b = t) :(println(a, b))

If we typeof head will find that it is a type named Symbol :

julia> typeof(multiStmt.head)Symbol

The symbol type can be :name defined using, or it can be created using the symbol type's construct:

julia> :str:strjulia> typeof(:str)Symboljulia> Symbol("str2"):str2julia> typeof(Symbol("str2"))==typeof(:str)true

Finally we use the eval() H function to pass in the expr type parameter evaluation:

julia> eval(ast)3julia> eval(multiStmt)21

This gives us a way to manipulate code using code:

julia> add = Expr(:call,:-,:a,:b):(a - b)julia> a = 11julia> b= 22julia> eval(add)-1
Macro

Julia's macro macro ... end defined by

julia> macro hello(name)       return "hello,my name is "*name       end@hello (macro with 1 method)julia> println(@hello("Andrew"))hello,my name is Andrewjulia> println(@hello "Andrew")hello,my name is Andrew

You can use a macro like a function to add parentheses or宏名 参数1 参数2 ...
Like the concept of a C + + macro, Julia's macro is also an implementation of the substitution action
So the above will be replaced with a println(@hello "Andrew") println("hello, my name is Andrew") result that can be used to @macroexpand get the expanded

julia> @macroexpand println(@hello "Andrew"):(println("hello,my name is Andrew"))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.