Can I store a tree produced in one Scala Def-macro and then pass it into another





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Say I have an expression whose tree I want to capture (without evaluating it!) and then pass it into a another macro sometime later:



// capture a tree from some arbitrary expression
val capturedTree = captureMacro( some(expression(tree())) )

// plug the tree into here
val result = processMacro(capturedTree, otherStuff)


Is there any way to do this? I've tried making capturedTree a refined type hoping that this would preserve the initial tree but that did not happen:



trait WithCapturedTree { def tree:Any }
class ReturnTreeMacro(val c:MacroContext) {
import c.universe._

def run(expression: Tree) =
q"new WithCapturedTree {val tree = ${expression}}"
}
def returnTree(expression:String):WithCapturedTree = macro ReturnTreeMacro.run

reify { returnTree("foo"+"bar") }
// returns Expr[WithCapturedTree{val tree: java.lang.String}](returnTree("foobar"))
// I.e. the result of "foo"+"bar" has already been evaluated!


Is there a way to get this approach to work? Is there a better appraoch for this problem?










share|improve this question


















  • 1





    Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

    – SergGr
    Jan 4 at 1:34











  • @SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

    – ChoppyTheLumberjack
    Jan 4 at 2:41











  • Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

    – SergGr
    Jan 4 at 4:26








  • 1





    Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

    – Dmytro Mitin
    Jan 4 at 18:29


















0















Say I have an expression whose tree I want to capture (without evaluating it!) and then pass it into a another macro sometime later:



// capture a tree from some arbitrary expression
val capturedTree = captureMacro( some(expression(tree())) )

// plug the tree into here
val result = processMacro(capturedTree, otherStuff)


Is there any way to do this? I've tried making capturedTree a refined type hoping that this would preserve the initial tree but that did not happen:



trait WithCapturedTree { def tree:Any }
class ReturnTreeMacro(val c:MacroContext) {
import c.universe._

def run(expression: Tree) =
q"new WithCapturedTree {val tree = ${expression}}"
}
def returnTree(expression:String):WithCapturedTree = macro ReturnTreeMacro.run

reify { returnTree("foo"+"bar") }
// returns Expr[WithCapturedTree{val tree: java.lang.String}](returnTree("foobar"))
// I.e. the result of "foo"+"bar" has already been evaluated!


Is there a way to get this approach to work? Is there a better appraoch for this problem?










share|improve this question


















  • 1





    Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

    – SergGr
    Jan 4 at 1:34











  • @SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

    – ChoppyTheLumberjack
    Jan 4 at 2:41











  • Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

    – SergGr
    Jan 4 at 4:26








  • 1





    Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

    – Dmytro Mitin
    Jan 4 at 18:29














0












0








0








Say I have an expression whose tree I want to capture (without evaluating it!) and then pass it into a another macro sometime later:



// capture a tree from some arbitrary expression
val capturedTree = captureMacro( some(expression(tree())) )

// plug the tree into here
val result = processMacro(capturedTree, otherStuff)


Is there any way to do this? I've tried making capturedTree a refined type hoping that this would preserve the initial tree but that did not happen:



trait WithCapturedTree { def tree:Any }
class ReturnTreeMacro(val c:MacroContext) {
import c.universe._

def run(expression: Tree) =
q"new WithCapturedTree {val tree = ${expression}}"
}
def returnTree(expression:String):WithCapturedTree = macro ReturnTreeMacro.run

reify { returnTree("foo"+"bar") }
// returns Expr[WithCapturedTree{val tree: java.lang.String}](returnTree("foobar"))
// I.e. the result of "foo"+"bar" has already been evaluated!


Is there a way to get this approach to work? Is there a better appraoch for this problem?










share|improve this question














Say I have an expression whose tree I want to capture (without evaluating it!) and then pass it into a another macro sometime later:



// capture a tree from some arbitrary expression
val capturedTree = captureMacro( some(expression(tree())) )

// plug the tree into here
val result = processMacro(capturedTree, otherStuff)


Is there any way to do this? I've tried making capturedTree a refined type hoping that this would preserve the initial tree but that did not happen:



trait WithCapturedTree { def tree:Any }
class ReturnTreeMacro(val c:MacroContext) {
import c.universe._

def run(expression: Tree) =
q"new WithCapturedTree {val tree = ${expression}}"
}
def returnTree(expression:String):WithCapturedTree = macro ReturnTreeMacro.run

reify { returnTree("foo"+"bar") }
// returns Expr[WithCapturedTree{val tree: java.lang.String}](returnTree("foobar"))
// I.e. the result of "foo"+"bar" has already been evaluated!


Is there a way to get this approach to work? Is there a better appraoch for this problem?







scala macros abstract-syntax-tree






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 22:07









ChoppyTheLumberjackChoppyTheLumberjack

327311




327311








  • 1





    Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

    – SergGr
    Jan 4 at 1:34











  • @SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

    – ChoppyTheLumberjack
    Jan 4 at 2:41











  • Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

    – SergGr
    Jan 4 at 4:26








  • 1





    Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

    – Dmytro Mitin
    Jan 4 at 18:29














  • 1





    Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

    – SergGr
    Jan 4 at 1:34











  • @SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

    – ChoppyTheLumberjack
    Jan 4 at 2:41











  • Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

    – SergGr
    Jan 4 at 4:26








  • 1





    Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

    – Dmytro Mitin
    Jan 4 at 18:29








1




1





Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

– SergGr
Jan 4 at 1:34





Could you describe your real high-level problem of why do you need this at all? Macro can contain main functions that call each other inside one macro invocation. Why do you need to pass some data between macro invocations?

– SergGr
Jan 4 at 1:34













@SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

– ChoppyTheLumberjack
Jan 4 at 2:41





@SergGr The 'capturedTree' is a tree that I want to search inside of 'otherStuff' in processMacro. Actually I want to define multiple capturedTrees which to search for inside of 'otherStuff' and replace in some situations.I don't want my users to be forced to define all the capturedTrees directly inside of the processMacro function, because there will be multiple processMacro invocations with different 'otherStuff' in each one.

– ChoppyTheLumberjack
Jan 4 at 2:41













Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

– SergGr
Jan 4 at 4:26







Sorry, your description is still not clear to me. If the user can pass some capturedTree to your processMacro macro, why he can't pass the data used to build it and let the processMacro macro build the same capturedTree inside using the same call? Or is it just an attempt of a performance optimization?

– SergGr
Jan 4 at 4:26






1




1





Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

– Dmytro Mitin
Jan 4 at 18:29





Reusing code in different macros is normally done by macro bundles docs.scala-lang.org/overviews/macros/bundles.html

– Dmytro Mitin
Jan 4 at 18:29












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030458%2fcan-i-store-a-tree-produced-in-one-scala-def-macro-and-then-pass-it-into-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030458%2fcan-i-store-a-tree-produced-in-one-scala-def-macro-and-then-pass-it-into-another%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Angular Downloading a file using contenturl with Basic Authentication

Monofisismo

Olmecas