Scala macro to print code? -


I want to do something like this:

  assuming DIF [A] (condition: = & Gt; boolean) (F: = & gt; A): A = {requirement (condition, / * print source-code condition * /) f}   

sample usage: < / P>

  def fib (n: int) = n match (yes, yes i know that this is not a smart matter 0 = & gt; 0 matter 1 = & gt; 1 case i = & gt; For example, if you call  Phib (-20), then I believe that (i & gt; 0) {fib (i-1) + fib (i-2)}}   

, I would like to throw an exception with the message like failure has failed: -20 & gt; 0 or Failure to fail: i & gt; 0

Man, this is one of the basic use cases you use Macro is not how to use macros

OK, that's what I thought too.

In my other answer to "guinea snippets" I mean, what space 2 does.

Or, you can represent an arbitrary representation, such as.

I thought I would write your example in REPL in two instances.

What can be easier?

Of course, it is easy <<>

People (for example, pallps, etc.)

who is Voices PALPULY) want trees to attach to the "source written on my keyboard" because you know, maybe I want it for message or to find out that What the user is trying to do

It seems that there is no range status of the prevalence p . So the second idea is that we know the beginning of the macro app, which is the second bracket of the ultimate list, so it is possible to do backward work through the source, it is possible to match the closing clauses of the first list.

Note that showCode is not useful because conditional such as 10 & lt; 5 it shows false , neatly connects.

  object x {import reflect.macros.blackbox.Context def implant [A: c.WeakTypeTag] (c: reference) (p: c.exp [boolean]) (body: c. xp [a]) = {import c.universe._drf tree line (t: tree): string = line at (tpoos) def lineAt (position: position): string = if (pos. isRange) pos.lineContent.drop (pos.column-1) .take (pos.end - pos.start + 1) and "???" Val msg = if (p.tree.pos.isRange) {// Oh, Anand Tree Line (P.TETE)} and {/ * console printline "content $ {p.tree.pos.lineContent} "Console printlane s" column $ {p.tree.pos.column} "// alas, it is the column of the top point of the tree, such as, & lt; In "a & lt; b", Val len = body.tree.pos.start - p.tree.pos.start.p.tree.pos.lineContent drop (p.tree.pos.column - 1) Lane * / / Take / Take it, I get it: Position is a big secret, the ghosts that make the wandering noise. // What do we know is the beginning of implementation, in which the match should have a close parent or brace: // apply show (condition) (body) showCode (p.tree)} q "require ($ p), $ Msg); $ body "} def x [A] (p: boolean) (body: = & gt; a): A = macro x.imappal [a]}   

this bus This happened with me to get the status:

  object X {import reflect.macros.blackbox.Context def impl (c: context) (p: c.exp [boolean]) = {Import c.universe._ def lineAt (status: position): string = if (pos.isRange) pos.lineContent.drop (pos.column-1) .take (pos.end - pos.start + 1) Other "???" New message (F) [A] (body: => A): A = body} "DEF (CAMCA) X (P: boolean): {def} [a] (body: => A) : A} = macro X.impl}   

is close to use x (10 <5) (println ("hi")) : Required fail: (10 <5> (p . Margin for error.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -