Send an unknown method call: Difference between revisions

+J
(+J)
Line 156:
 
For more information on this see [[Respond_to_an_unknown_method_call#Icon_and_Unicon|Respond to an unknown method call]].
=={{header|J}}==
 
''Solution'': There are multiple ways to evoke code at runtime. The most common is <tt>". y</tt> (''eval''uate the code in the string y, producing a noun), but there's also <tt>''name''~ </tt> (which will modify J's stack by replacing the two tokens <tt>''name''</tt> and <tt>~</tt> with the named object) as well as <tt>x 128!:2 y</tt> (''apply'' the verb described by <tt>x</tt> to the noun <tt>y</tt>. There are other methods as well, e.g.
 
'''Example''':<lang j> sum =: +/
prod =: */
count =: #
 
nameToDispatch =: 'sum' NB. pick a name already defined
 
". nameToDispatch,' 1 2 3'
6
nameToDispatch~ 1 2 3
6
nameToDispatch (128!:2) 1 2 3
6
 
nameToDispatch =: 'count' NB. pick another name
 
". nameToDispatch,' 1 2 3'
3
nameToDispatch~ 1 2 3
3
nameToDispatch (128!:2) 1 2 3
3</lang>
 
 
 
 
=={{header|Java}}==
Anonymous user