Referencing value assigned to variable in statement builder within another statement
Given a statement that does something like:
I would like to use the value assigned to
propertyValue
elsewhere, but of course, I cannot use AppendExpression with it since the context isn't aware it's been assigned. How might I go about referencing this variable value as an expression?
Thanks!10 Replies
I think that
ExpressionFactory.Parse("propertyValue")
should work for you. (It's effectively the same as using ExpressionBuilder
and then calling ToExpression()
on it.)May you're looking for IFieldOrPropertyBuilder.InitializerExpression. https://doc.metalama.net/api/metalama_framework_code_ifieldorproperty_initializerexpression#Metalama_Framework_Code_IFieldOrProperty_InitializerExpression
Well, let me put it another way. Say I use the statement builder with the following:
When I insert the statement via
meta.InsertStatement
, this doesn't give me a variable I can reference in other parts of the template. The use of ExpressionFactory.Parse
on the next line does give me abc
to use in the template, but this suddenly can't be used in a compile-time loop as it outputs the following:
I suppose I could do something like:
And then this yields (outside of the apparently bug reported at https://github.com/postsharp/Metalama/issues/202):
Again, since the first variable is more than sufficient for my purposes but is simply inaccessible since it's created by string, might you consider updating the StatementBuilder to include an out variable or the like that I can use to reference the variable on the class instead of this hacky approach resulting in two lines in the output for each assignment (especially since the ExpressionFactory.Parse().Value
approach itself yields a unique variable name each time?
Thanks!GitHub
Issues · postsharp/Metalama
Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions. - Issues · postsharp/Metalama
OK I got it. Didn't read your first request properly it seems
However generally we want to keep StatementBuilder and ExpressionBuilder as minimalistic as possible.
So you want an API that defines a local variable and takes care of the unique naming?
If you want to reference the variable you declared, you can do it by declaring
var abc = ExpressionFactory.Parse("abc");
and then using abc.Value
where you used just abc
previously.
Alternatively, I wonder if using Builder
just for the expression and letting Metalama manage the variable would work better. Let me check.
Yeah, I think that would work better for you.
For example, this code:
generates:
I'll see if I can't use that - I suspect I'm going to run into issues along the lines of https://github.com/postsharp/Metalama/issues/202 since this is within several
if
and foreach
loops both run-time and compile-timeGitHub
Bug: Metalama not incrementing in compile-time loop as expected · I...
I'm trying to solve a variable reference issue. Put simply, I have a loop inside a template that enumerates several compile-time values to write several run-time statements. As these are writte...
I think you shouldn't, since you wouldn't be modifying any compile-time variables.
Yeah, unfortunately I think my toy sample there doesn't quite work with what I actually have in place - can't see how to fit that in there.
I've got a foreach going through a compile-time dictionary, a foreach inside that going through the values of that kv pair, then a null check on each of those values (compile-time), then a run-time null check (per issue #202 up there) and then inside all that I've got one more equality check and the statement builder. Right now I'm incrementing the meta.CompileTime after the run-time null check but at the bottom of the inner foreach.
If not for all my loops already there, I think that'd be a clever fix
To your question @the.metalama.guy Yes, that'd be great - an API that lets me simply provide the right side of a statement after the "=" and places its own unique variable name in there so I might trivially reference it anywhere else.
Like what I get with expression builder - don't much care what the auto-generated variable name is, so long as it's unique and I can trivially reference it in the rest of the template
I think it's rather going to be a method like:
IExpression DefineLocalVariable( string nameHint, IType type, IExpression? assignedValue );
Please file a feature request on GitHub because we don't track tickets here -- just for ephemeral discussions
Absolutely - I've been thinking about what I'd like it to do, but I'll file a ticket about it in a few
Filed under #205