Send an unknown method call: Difference between revisions
Content deleted Content added
m →{{header|Wren}}: Changed to Wren S/H |
add Ecstasy example |
||
Line 153: | Line 153: | ||
E.call(example, name, []) |
E.call(example, name, []) |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header|Ecstasy}}== |
|||
Here's a simple example of a test module using its runtime type to search for methods by some name (specified on the the command line), grabbing the one by that name that requires no parameters, and dynamically invoking it: |
|||
<syntaxhighlight lang="ecstasy"> |
|||
module test { |
|||
@Inject Console console; |
|||
void run(String[] args) { |
|||
String name = args.empty ? "foo" : args[0]; |
|||
if (val mm := &this.actualType.multimethods.get(name), |
|||
val m := mm.methods.any(m -> m.ParamTypes.size == 0)) { |
|||
m.invoke(this, Tuple:()); |
|||
} else { |
|||
console.print($"No such 0-parameter method: {name.quoted()}"); |
|||
} |
|||
} |
|||
void foo() { |
|||
console.print("this is the foo() method"); |
|||
} |
|||
void bar() { |
|||
console.print("this is the bar() method"); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
x$ xec test foo |
|||
this is the foo() method |
|||
x$ xec test bar |
|||
this is the bar() method |
|||
x$ xec test baz |
|||
No such 0-parameter method: "baz" |
|||
</pre> |
|||
=={{header|Elena}}== |
=={{header|Elena}}== |