I have the following freemarker template:
[#local snippet ][#noparse]
[#assign out ]value: ${v}
[/#assign]
[/#noparse]
[/#local]
[#assign hook = snippet?interpret ]
...
[#macro trigger ]
[#hook /]
[/#macro]
...
[#list values as v ]
[#trigger ]
[/#list]
${out}
What it essentially does is defining a hooks to execute at a certain later moment and a macro to trigger it's execution.
When I try to render this template, i get the following error:
The following has evaluated to null or missing:
==> v [in template "xxx.ftl->anonymous_interpreted" at line 1, column 17]
It might be interesting to note that the following:
[#list values as v ]
[#hook ]
[/#list]
is just working, i.e. rendering the template as I would expect, printing value: xxx for each value in the list.
Does anybody know what is going wrong here?
Is there any way to make this work as expected?
EDIT
I just discovered that the error doesn't occur if instead of an interpreted snippet I pass a regular macro:
[#assign hook = myMacro ]
...
but not if the macro is defined in another namespace.
A loop variable (v) is basically a local variable of the #list block. So, with Java-ish pseudo-code what you try to do is like:
void main() {
for (int v : values) {
trigger();
}
}
void trigger() {
print(v); // Error, v is not visible here!
}
As of calling a directive defined with ?interpret, that's special, as it tries to behave as if you have copy-pasted the code snippet at the place of invocation. That is, the directive that you call with #hook doesn't create its own local context. So that's why it sees v if you call it directly from the loop. But if you call it form trigger, then it will live in the local context of the trigger macro, which do have its own local context.
As of making it work... one possibility is assigning v to a namespace-scope variable via <#assign value = v>, and then refer to value in the interpreted fragment. Another solution is of course getting rid of the trigger indirection, if you can. Yet another solution is to define trigger with ?interpret instead of with #macro, because then trigger will see v, and so hook will too.
Related
I have 2 print statements below. first print statement is working fine but the second print is returning a null value. what could be the reason.
Thanks in advance.
void setup()
{
setSize(800);
GUI g=new GUI();
Println(g); // this prints fine
}
void draw()
{
Println(g); //this becomes null
}
First off, please post the actual code you're running. The println() function starts with a lower-case letter, so the code you've posted won't compile. Please post a MCVE.
Secondly, please understand how scope works. Variables you create in one function are not available in other functions. So the g variable you create in the setup() function is not the same as the g variable in the draw() function!
All of that being said, your problem is caused by the fact that Processing contains a g variable that you aren't really supposed to mess with. Change the name of the variable to avoid this problem. Of course, this will give you a compiler error because you're trying to use a variable outside of its scope. Fix that by declaring variables you want to use in multiple functions at the top of your sketch.
Does Groovy have something similar to bang methods on Ruby?
From this blog post:
In Ruby, you can write methods whose names end in ! (exclamation point or “bang”). There’s a lot of confusion surrounding the matter of when, and why, you would want to do so.
The ! in method names that end with ! means, “This method is dangerous”—or, more precisely, this method is the “dangerous” version of an otherwise equivalent method, with the same name minus the !. “Danger” is relative; the ! doesn’t mean anything at all unless the method name it’s in corresponds to a similar but bang-less method name.*
And this site:
You'll find a number of pairs of methods, one with the bang and one without. Those without the bang perform an action and return a freshly minted object, reflecting the results of the action (capitalizing a string, sorting an array, and so on). The bang versions of the same methods perform the action, but they do so in place: Instead of creating a new object, they transform the original object.
This is not a convention in Groovy like it is in Ruby. However you can write methods with names that contain characters like ! with the limitation that it must always be quoted like a string:
// define method with quoted name
def 'dangerous!'() {
// do something dangerous
}
// invoke method with quoted name
'dangerous!'()
No, groovy (currently as of v2.1.4) doesn't have anything like this
To add to your options, another solution that would be more Groovy-like or Java-like would be to include an optional parameter that enabled in-place (a.k.a. dangerous) modification, like so:
def processFoo(Foo item, mutate = false) {
if(!mutate) {
Foo temp = new Foo()
// copy item properties
item = temp
}
item.bar = 'blah blah'
// process item here
return item
}
processFoo(myFoo) // makes a copy
processFoo(myFoo, true) // modifies original
This pattern is used — albeit in the opposite manner — with the sort method on collections. Calling sort(false) on Lists prevents changing the original array. Calling sort() or sort(true) will modify it directly.
Could someone explain this with an example of how it is useful.
Specifically I am having an issue with how you accumulate something (an array would be nice) when you change scope into a method..
e.g.
def modify(value)
...code ....
end
an_array.inject(:modify)
How can I get an accumulator (above), or something that is passing a message along. The value returned is the last value to come out of modify. But modify only gets each value in the array. It doesn't get the message. (below) passing message to the .... code ...
an_array.inject(0) { |message,element| .... code .... }
Using inject with only a symbol:
[*1..5].inject(:+) #=> 15
The above translates to:
[*1..5].inject { |sum, num| sum + num }
This is what the docs have to say on the subject (emphasis added):
Combines all elements of enum by applying a binary operation,
specified by a block or a symbol that names a method or operator
I'm happy to update my answer once you clarify the second part of your question a bit (e.g. what do you mean by "changing scope" in this context?).
In Lua I've created a pretty printer for my tables/objects. However, when a function is displayed, it's shown as a pointer.
I've been reading up on Lua Introspection but when I introspect the function with debug.getinfo() it won't return the function's name. This seems to be due to a scoping issue but I'm not sure how to get around it.
What is the best way to get a function's name using its pointer? (I understand functions are first class citizens in Lua and that they can be created anonymously, that's fine)
when you create the functions, register them in a table, using them as keys.
local tableNames = {}
function registerFunction(f, name)
tableNames[f] = name
end
function getFunctionName(f)
return tableNames[f]
end
...
function foo()
..
end
registerFunction(foo, "foo")
...
getFunctionName(foo) -- will return "foo"
For some reason, it seems to work only with number parameters (that is, active functions on the stack).
The script
function a() return debug.getinfo(1,'n') end
function prettyinfo(info)
for k,v in pairs(info) do print(k,v) end
end
prettyinfo(a())
prints
name a
namewhat global
but if I change the last line to
prettyinfo(debug.getinfo(a, 'n'))
it gives me only an empty string:
namewhat
I'm binding multiple functions to a single widget using the same sequence (in this case the '<Button>' sequence) in Tkinter. To do this I'm using the add argument. Is it possible to get all the functions bound to a particular sequence?
snippet :
wid.bind('<Button>', func0)
wid.bind('<Button>', func1, add=True)
If I understand the question correctly, you can create one callback function:
def cb(event):
func0()
func1()
# ...
wid.bind('<Button>', cb)
Also, you can replace add=True with '+':
wid.bind('<Button>, func1, '+')