On Wed, Dec 9, 2015 at 3:57 PM, Erik Schnetter <schnet...@gmail.com> wrote: > Comparing expressions is useful if you want to write a test case for a > function that transforms expressions, such as e.g. `@fastmath`. I recently > added some test cases, but was unable to test the result of > > macroexpand(:@fastmath :+) > > since I don't know how to explicitly construct the expected result. I know > how to do it in the REPL, but this doesn't work in test cases.
What's the difference between REPL and test script? julia -f -e 'println(macroexpand(:(@fastmath +)) == :(Base.FastMath.add_fast))' true > > -erik > > > On Wed, Dec 9, 2015 at 3:46 PM, Yichao Yu <yyc1...@gmail.com> wrote: >> >> On Wed, Dec 9, 2015 at 3:05 PM, <vish...@stanford.edu> wrote: >> > Interesting. I didn't think to use dump to check differences. >> > >> > Another followup question. After using dump on some simple if >> > statements, >> > I've noticed that all blocks induce this LineNumberNode which is messing >> > up >> > the equality. >> > Is there a way to ignore these nodes in the equality check? They show up >> > in >> > any kind of block statement (function, if, etc) >> > >> >> May I ask what do you need the equality of expressions for? It doesn't >> sound like a too useful concept. There can be expressions that are >> equal but have different side effects due to the variables they >> capture (try constructing `Expr(:call, :push!, [1, 2], 1)` twice and >> eval/compare them). There can also be expressions that are equivalant >> but appears differently. >> >> In general, I never find `==` of expressions too useful. When >> processing it, you can just ignore the line number nodes if you don't >> care too much about debug info (also see base/docs/Docs.jl for some >> example of stripping unwanted part from a expression). There's also >> MacroTools.jl which provides a nice way to process expressions. >> >> > eg: >> > >> > dump(:(if true 1 else 0 end)) >> > >> > Expr >> > >> > head: Symbol if >> > >> > args: Array(Any,(3,)) >> > >> > 1: Bool true >> > >> > 2: Expr >> > >> > head: Symbol block >> > >> > args: Array(Any,(2,)) >> > >> > 1: LineNumberNode >> > >> > file: Symbol none >> > >> > line: Int64 1 >> > >> > 2: Int64 1 >> > >> > typ: Any >> > >> > 3: Expr >> > >> > head: Symbol block >> > >> > args: Array(Any,(2,)) >> > >> > 1: LineNumberNode >> > >> > file: Symbol none >> > >> > line: Int64 1 >> > >> > 2: Int64 0 >> > >> > typ: Any >> > >> > typ: Any >> > >> > >> > dump(Expr(:if, true, Expr(:block, 0), Expr(:block,1))) >> > >> > Expr >> > >> > head: Symbol if >> > >> > args: Array(Any,(3,)) >> > >> > 1: Bool true >> > >> > 2: Expr >> > >> > head: Symbol block >> > >> > args: Array(Any,(1,)) >> > >> > 1: Int64 0 >> > >> > typ: Any >> > >> > 3: Expr >> > >> > head: Symbol block >> > >> > args: Array(Any,(1,)) >> > >> > 1: Int64 1 >> > >> > typ: Any >> > >> > typ: Any >> > >> > >> > They're the same minus LineNumberNodes, because block type expressions >> > always create those things. >> > >> > >> > On Wednesday, December 9, 2015 at 7:21:55 AM UTC-8, STAR0SS wrote: >> >> >> >> You can also use dump() on your expressions to see how they differ >> >> exactly. The normal printing doesn't really show you much. > > > > > -- > Erik Schnetter <schnet...@gmail.com> > http://www.perimeterinstitute.ca/personal/eschnetter/