Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Send-an-unknown-method-call/00-META.yaml
Normal file
3
Task/Send-an-unknown-method-call/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Send_an_unknown_method_call
|
||||
note: Object oriented
|
||||
9
Task/Send-an-unknown-method-call/00-TASK.txt
Normal file
9
Task/Send-an-unknown-method-call/00-TASK.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
;Task:
|
||||
Invoke an object method where the name of the method to be invoked can be generated at run time.
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Respond to an unknown method call]].
|
||||
* [[Runtime evaluation]]
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
obj := {mA: Func("mA"), mB: Func("mB"), mC: Func("mC")}
|
||||
InputBox, methodToCall, , Which method should I call?
|
||||
obj[methodToCall].()
|
||||
|
||||
mA(){
|
||||
MsgBox Method A
|
||||
}
|
||||
mB(){
|
||||
MsgBox Method B
|
||||
}
|
||||
mC(){
|
||||
MsgBox Method C
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(task=
|
||||
( oracle
|
||||
= (predicate="is made of green cheese")
|
||||
(generateTruth=.str$(!arg " " !(its.predicate) "."))
|
||||
(generateLie=.str$(!arg " " !(its.predicate) "!"))
|
||||
)
|
||||
& new$oracle:?SourceOfKnowledge
|
||||
& put
|
||||
$ "You may ask the Source of Eternal Wisdom ONE thing.
|
||||
Enter \"Truth\" or \"Lie\" on the next line and press the <Enter> key.
|
||||
"
|
||||
& whl
|
||||
' ( get':?trueorlie:~Truth:~Lie
|
||||
& put$"Try again\n"
|
||||
)
|
||||
& put$(str$("You want a " !trueorlie ". About what?" \n))
|
||||
& get'(,STR):?something
|
||||
& (SourceOfKnowledge..str$(generate !trueorlie))$!something
|
||||
);
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
using System;
|
||||
|
||||
class Example
|
||||
{
|
||||
public int foo(int x)
|
||||
{
|
||||
return 42 + x;
|
||||
}
|
||||
}
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var example = new Example();
|
||||
var method = "foo";
|
||||
|
||||
var result = (int)example.GetType().GetMethod(method).Invoke(example, new object[]{ 5 });
|
||||
Console.WriteLine("{0}(5) = {1}", method, result);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(import '[java.util Date])
|
||||
(import '[clojure.lang Reflector])
|
||||
|
||||
(def date1 (Date.))
|
||||
(def date2 (Date.))
|
||||
(def method "equals")
|
||||
|
||||
;; Two ways of invoking method "equals" on object date1
|
||||
;; using date2 as argument
|
||||
|
||||
;; Way 1 - Using Reflector class
|
||||
;; NOTE: The argument date2 is passed inside an array
|
||||
(Reflector/invokeMethod date1 method (object-array [date2]))
|
||||
|
||||
;; Way 2 - Using eval
|
||||
;; Eval runs any piece of valid Clojure code
|
||||
;; So first we construct a piece of code to do what we want (where method name is inserted dynamically),
|
||||
;; then we run the code using eval
|
||||
(eval `(. date1 ~(symbol method) date2))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(funcall (intern "SOME-METHOD") my-object a few arguments)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for name in ["foo", "bar"] {
|
||||
E.call(example, name, [])
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import extensions;
|
||||
|
||||
class Example
|
||||
{
|
||||
foo(x)
|
||||
= x + 42;
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var example := new Example();
|
||||
var methodSignature := "foo";
|
||||
|
||||
var invoker := new MessageName(methodSignature);
|
||||
var result := invoker(example,5);
|
||||
|
||||
console.printLine(methodSignature,"(",5,") = ",result)
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
USING: accessors kernel math prettyprint sequences words ;
|
||||
IN: rosetta-code.unknown-method-call
|
||||
|
||||
TUPLE: foo num ;
|
||||
C: <foo> foo
|
||||
GENERIC: add5 ( x -- y )
|
||||
M: foo add5 num>> 5 + ;
|
||||
|
||||
42 <foo> ! construct a foo
|
||||
"add" "5" append ! construct a word name
|
||||
! must specify vocab to look up a word
|
||||
"rosetta-code.unknown-method-call"
|
||||
lookup-word execute . ! 47
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
include FMS-SI.f
|
||||
include FMS-SILib.f
|
||||
|
||||
var x \ instantiate a class var object named x
|
||||
|
||||
\ Use a standard Forth string and evaluate it.
|
||||
\ This is equivalent to sending the !: message to object x
|
||||
42 x s" !:" evaluate
|
||||
|
||||
x p: 42 \ send the print message ( p: ) to x to verify the contents
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Type Example
|
||||
foo As Integer Ptr
|
||||
Declare Constructor (x As Integer)
|
||||
End Type
|
||||
|
||||
Constructor Example(x As Integer)
|
||||
This.foo = New Integer
|
||||
*This.foo = 42 + x
|
||||
End Constructor
|
||||
|
||||
Dim As Example result = 5
|
||||
Print *result.foo
|
||||
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type example struct{}
|
||||
|
||||
// the method must be exported to be accessed through reflection.
|
||||
func (example) Foo() int {
|
||||
return 42
|
||||
}
|
||||
|
||||
func main() {
|
||||
// create an object with a method
|
||||
var e example
|
||||
// get the method by name
|
||||
m := reflect.ValueOf(e).MethodByName("Foo")
|
||||
// call the method with no argments
|
||||
r := m.Call(nil)
|
||||
// interpret first return value as int
|
||||
fmt.Println(r[0].Int()) // => 42
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Example {
|
||||
def foo(value) {
|
||||
"Invoked with '$value'"
|
||||
}
|
||||
}
|
||||
|
||||
def example = new Example()
|
||||
def method = "foo"
|
||||
def arg = "test value"
|
||||
|
||||
assert "Invoked with 'test value'" == example."$method"(arg)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
procedure main()
|
||||
x := foo() # create object
|
||||
x.m1() # static call of m1 method
|
||||
# two examples where the method string can be dynamically constructed ...
|
||||
"foo_m1"(x) # ... need to know class name and method name to construct name
|
||||
x.__m["m1"] # ... general method (better)
|
||||
end
|
||||
|
||||
class foo(a,b,c) # define object
|
||||
method m1(x)
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Example := Object clone
|
||||
Example foo := method(x, 42+x)
|
||||
|
||||
name := "foo"
|
||||
Example clone perform(name,5) println // prints "47"
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
import java.lang.reflect.Method;
|
||||
|
||||
class Example {
|
||||
public int foo(int x) {
|
||||
return 42 + x;
|
||||
}
|
||||
}
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Object example = new Example();
|
||||
String name = "foo";
|
||||
Class<?> clazz = example.getClass();
|
||||
Method meth = clazz.getMethod(name, int.class);
|
||||
Object result = meth.invoke(example, 5); // result is int wrapped in an object (Integer)
|
||||
System.out.println(result); // prints "47"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
example = new Object;
|
||||
example.foo = function(x) {
|
||||
return 42 + x;
|
||||
};
|
||||
|
||||
name = "foo";
|
||||
example[name](5) # => 47
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
const functions = Dict{String,Function}(
|
||||
"foo" => x -> 42 + x,
|
||||
"bar" => x -> 42 * x)
|
||||
|
||||
@show functions["foo"](3)
|
||||
@show functions["bar"](3)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
// Kotlin JS version 1.1.4-3
|
||||
|
||||
class C {
|
||||
fun foo() {
|
||||
println("foo called")
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val c = C()
|
||||
val f = "c.foo"
|
||||
js(f)() // invokes c.foo dynamically
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
define mytype => type {
|
||||
public foo() => {
|
||||
return 'foo was called'
|
||||
}
|
||||
public bar() => {
|
||||
return 'this time is was bar'
|
||||
}
|
||||
}
|
||||
local(obj = mytype, methodname = tag('foo'), methodname2 = tag('bar'))
|
||||
#obj->\#methodname->invoke
|
||||
#obj->\#methodname2->invoke
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
obj = script("MyClass").new()
|
||||
-- ...
|
||||
method = #foo
|
||||
arg1 = 23
|
||||
res = call(method, obj, arg1)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
:- object(foo).
|
||||
|
||||
:- public(bar/1).
|
||||
bar(42).
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
:- object(query_foo).
|
||||
|
||||
:- public(query/0).
|
||||
query :-
|
||||
write('Message: '),
|
||||
read(Message),
|
||||
foo::Message.
|
||||
write('Reply: '),
|
||||
write(Message), nl.
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
local example = { }
|
||||
function example:foo (x) return 42 + x end
|
||||
|
||||
local name = "foo"
|
||||
example[name](example, 5) --> 47
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
funName = 'foo'; % generate function name
|
||||
feval (funNAME, ...) % evaluation function with optional parameters
|
||||
|
||||
funName = 'a=atan(pi)'; % generate function name
|
||||
eval (funName, 'printf(''Error\n'')')
|
||||
|
|
@ -0,0 +1 @@
|
|||
ToExpression[Input["function? E.g. Sin",]][Input["value? E.g. 0.4123"]]
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface Example : NSObject
|
||||
- (NSNumber *)foo;
|
||||
@end
|
||||
|
||||
@implementation Example
|
||||
- (NSNumber *)foo {
|
||||
return @42;
|
||||
}
|
||||
@end
|
||||
|
||||
int main (int argc, const char *argv[]) {
|
||||
@autoreleasepool {
|
||||
|
||||
id example = [[Example alloc] init];
|
||||
SEL selector = @selector(foo); // or = NSSelectorFromString(@"foo");
|
||||
NSLog(@"%@", [example performSelector:selector]);
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
16 "sqrt" asMethod perform
|
||||
|
|
@ -0,0 +1 @@
|
|||
16 "sqrt" Word find perform
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
foo()=5;
|
||||
eval(Str("foo","()"))
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
class Example {
|
||||
function foo($x) {
|
||||
return 42 + $x;
|
||||
}
|
||||
}
|
||||
|
||||
$example = new Example();
|
||||
|
||||
$name = 'foo';
|
||||
echo $example->$name(5), "\n"; // prints "47"
|
||||
|
||||
// alternately:
|
||||
echo call_user_func(array($example, $name), 5), "\n";
|
||||
?>
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
program Test;
|
||||
{$mode objfpc}{$h+}
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
type
|
||||
TProc = procedure of object;
|
||||
|
||||
{$push}{$m+}
|
||||
TMyObj = class
|
||||
strict private
|
||||
FName: string;
|
||||
public
|
||||
constructor Create(const aName: string);
|
||||
property Name: string read FName;
|
||||
published
|
||||
procedure Foo;
|
||||
procedure Bar;
|
||||
end;
|
||||
{$pop}
|
||||
|
||||
constructor TMyObj.Create(const aName: string);
|
||||
begin
|
||||
FName := aName;
|
||||
end;
|
||||
|
||||
procedure TMyObj.Foo;
|
||||
begin
|
||||
WriteLn(Format('This is %s.Foo()', [Name]));
|
||||
end;
|
||||
|
||||
procedure TMyObj.Bar;
|
||||
begin
|
||||
WriteLn(Format('This is %s.Bar()', [Name]));
|
||||
end;
|
||||
|
||||
procedure CallByName(o: TMyObj; const aName: string);
|
||||
var
|
||||
m: TMethod;
|
||||
begin
|
||||
m.Code := o.MethodAddress(aName);
|
||||
if m.Code <> nil then begin
|
||||
m.Data := o;
|
||||
TProc(m)();
|
||||
end else
|
||||
WriteLn(Format('Unknown method(%s)', [aName]));
|
||||
end;
|
||||
|
||||
var
|
||||
o: TMyObj;
|
||||
|
||||
begin
|
||||
o := TMyObj.Create('Obj');
|
||||
CallByName(o, 'Bar');
|
||||
CallByName(o, 'Foo');
|
||||
CallByName(o, 'Baz');
|
||||
o.Free;
|
||||
end.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package Example;
|
||||
sub new {
|
||||
bless {}
|
||||
}
|
||||
sub foo {
|
||||
my ($self, $x) = @_;
|
||||
return 42 + $x;
|
||||
}
|
||||
|
||||
package main;
|
||||
my $name = "foo";
|
||||
print Example->new->$name(5), "\n"; # prints "47"
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">Hello</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #008000;">"Hello"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">erm</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"Hemmm"</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #000000;">5</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">erm</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]+=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">3</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
|
||||
<span style="color: #7060A8;">call_proc</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #000000;">erm</span><span style="color: #0000FF;">),{})</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
go =>
|
||||
println("Function: Use apply/n"),
|
||||
Fun = "fib",
|
||||
A = 10,
|
||||
% Convert F to an atom
|
||||
println(apply(to_atom(Fun),A)),
|
||||
nl,
|
||||
|
||||
println("Predicate: use call/n"),
|
||||
Pred = "pyth",
|
||||
call(Pred.to_atom,3,4,Z),
|
||||
println(z=Z),
|
||||
|
||||
% Pred2 is an atom so it can be used directly with call/n.
|
||||
Pred2 = pyth,
|
||||
call(Pred.to_atom,13,14,Z2),
|
||||
println(z2=Z2),
|
||||
|
||||
nl.
|
||||
|
||||
% A function
|
||||
fib(1) = 1.
|
||||
fib(2) = 1.
|
||||
fib(N) = fib(N-1) + fib(N-2).
|
||||
|
||||
% A predicate
|
||||
pyth(X,Y,Z) =>
|
||||
Z = X**2 + Y**2.
|
||||
|
|
@ -0,0 +1 @@
|
|||
(send (expression) Obj arg1 arg2)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
string unknown = "format_nice";
|
||||
object now = Calendar.now();
|
||||
now[unknown]();
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
$method = ([Math] | Get-Member -MemberType Method -Static | Where-Object {$_.Definition.Split(',').Count -eq 1} | Get-Random).Name
|
||||
$number = (1..9 | Get-Random) / 10
|
||||
$result = [Math]::$method($number)
|
||||
$output = [PSCustomObject]@{
|
||||
Method = $method
|
||||
Number = $number
|
||||
Result = $result
|
||||
}
|
||||
|
||||
$output | Format-List
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
class Example(object):
|
||||
def foo(self, x):
|
||||
return 42 + x
|
||||
|
||||
name = "foo"
|
||||
getattr(Example(), name)(5) # => 47
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
(define foo -> 5)
|
||||
|
||||
(define execute-function
|
||||
Name -> (eval [(INTERN Name)]))
|
||||
|
||||
(execute-function "foo")
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
#lang racket
|
||||
(define greeter
|
||||
(new (class object% (super-new)
|
||||
(define/public (hello name)
|
||||
(displayln (~a "Hello " name "."))))))
|
||||
|
||||
; normal method call
|
||||
(send greeter hello "World")
|
||||
|
||||
; sending an unknown method
|
||||
(define unknown 'hello)
|
||||
(dynamic-send greeter unknown "World")
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
my $object = 42 but role { method add-me($x) { self + $x } }
|
||||
my $name = 'add-me';
|
||||
say $object."$name"(5); # 47
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
class Example
|
||||
def foo
|
||||
42
|
||||
end
|
||||
def bar(arg1, arg2, &block)
|
||||
block.call arg1, arg2
|
||||
end
|
||||
end
|
||||
|
||||
symbol = :foo
|
||||
Example.new.send symbol # => 42
|
||||
Example.new.send( :bar, 1, 2 ) { |x,y| x+y } # => 3
|
||||
args = [1, 2]
|
||||
Example.new.send( "bar", *args ) { |x,y| x+y } # => 3
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Example
|
||||
private
|
||||
def privacy; "secret"; end
|
||||
public
|
||||
def publicity; "hi"; end
|
||||
end
|
||||
|
||||
e = Example.new
|
||||
e.public_send :publicity # => "hi"
|
||||
e.public_send :privacy # raises NoMethodError
|
||||
e.send :privacy # => "secret"
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
class Example {
|
||||
def foo(x: Int): Int = 42 + x
|
||||
}
|
||||
|
||||
object Main extends App {
|
||||
val example = new Example
|
||||
|
||||
val meth = example.getClass.getMethod("foo", classOf[Int])
|
||||
|
||||
assert(meth.invoke(example, 5.asInstanceOf[AnyRef]) == 47.asInstanceOf[AnyRef], "Not confirm expectation.")
|
||||
println(s"Successfully completed without errors. [total ${scala.compat.Platform.currentTime - executionStart} ms]")
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Example {
|
||||
method foo(x) {
|
||||
42 + x
|
||||
}
|
||||
}
|
||||
|
||||
var name = 'foo'
|
||||
var obj = Example()
|
||||
|
||||
say obj.(name)(5) # prints: 47
|
||||
say obj.method(name)(5) # =//=
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Object subclass: #Example.
|
||||
|
||||
Example extend [
|
||||
foo: x [
|
||||
^ 42 + x ] ].
|
||||
|
||||
symbol := 'foo:' asSymbol. " same as symbol := #foo: "
|
||||
|
||||
Example new perform: symbol with: 5. " returns 47 "
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
import Foundation
|
||||
|
||||
class MyUglyClass: NSObject {
|
||||
@objc
|
||||
func myUglyFunction() {
|
||||
print("called myUglyFunction")
|
||||
}
|
||||
}
|
||||
|
||||
let someObject: NSObject = MyUglyClass()
|
||||
|
||||
someObject.perform(NSSelectorFromString("myUglyFunction"))
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
@dynamicCallable
|
||||
protocol FunDynamics {
|
||||
var parent: MyDynamicThing { get }
|
||||
|
||||
func dynamicallyCall(withArguments args: [Int]) -> MyDynamicThing
|
||||
func dynamicallyCall(withKeywordArguments args: [String: Int]) -> MyDynamicThing
|
||||
}
|
||||
|
||||
extension FunDynamics {
|
||||
func dynamicallyCall(withKeywordArguments args: [String: Int]) -> MyDynamicThing {
|
||||
if let add = args["adding"] {
|
||||
parent.n += add
|
||||
}
|
||||
|
||||
if let sub = args["subtracting"] {
|
||||
parent.n -= sub
|
||||
}
|
||||
|
||||
return parent
|
||||
}
|
||||
}
|
||||
|
||||
@dynamicMemberLookup
|
||||
class MyDynamicThing {
|
||||
var n: Int
|
||||
|
||||
init(n: Int) {
|
||||
self.n = n
|
||||
}
|
||||
|
||||
subscript(dynamicMember member: String) -> FunDynamics {
|
||||
switch member {
|
||||
case "subtract":
|
||||
return Subtracter(parent: self)
|
||||
case "add":
|
||||
return Adder(parent: self)
|
||||
case _:
|
||||
return Nuller(parent: self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Nuller: FunDynamics {
|
||||
var parent: MyDynamicThing
|
||||
|
||||
func dynamicallyCall(withArguments args: [Int]) -> MyDynamicThing { parent }
|
||||
}
|
||||
|
||||
struct Subtracter: FunDynamics {
|
||||
var parent: MyDynamicThing
|
||||
|
||||
func dynamicallyCall(withArguments args: [Int]) -> MyDynamicThing {
|
||||
switch args.count {
|
||||
case 1:
|
||||
parent.n -= args[0]
|
||||
case _:
|
||||
print("Unknown call")
|
||||
}
|
||||
|
||||
return parent
|
||||
}
|
||||
}
|
||||
|
||||
struct Adder: FunDynamics {
|
||||
var parent: MyDynamicThing
|
||||
|
||||
func dynamicallyCall(withArguments arg: [Int]) -> MyDynamicThing {
|
||||
switch arg.count {
|
||||
case 1:
|
||||
parent.n += arg[0]
|
||||
case _:
|
||||
print("Unknown call")
|
||||
}
|
||||
|
||||
return parent
|
||||
}
|
||||
}
|
||||
|
||||
let thing =
|
||||
MyDynamicThing(n: 0)
|
||||
.add(20)
|
||||
.divide(2) // Unhandled call, do nothing
|
||||
.subtract(adding: 10, subtracting: 14)
|
||||
|
||||
print(thing.n)
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package require Tcl 8.6
|
||||
oo::class create Example {
|
||||
method foo {} {return 42}
|
||||
method 1 {s} {puts "fee$s"}
|
||||
method 2 {s} {puts "fie$s"}
|
||||
method 3 {s} {puts "foe$s"}
|
||||
method 4 {s} {puts "fum$s"}
|
||||
}
|
||||
set eg [Example new]
|
||||
set mthd [format "%c%c%c" 102 111 111]; # A "foo" by any other means would smell as sweet
|
||||
puts [$eg $mthd]
|
||||
for {set i 1} {$i <= 4} {incr i} {
|
||||
$eg $i ...
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
import "meta" for Meta
|
||||
|
||||
class Test {
|
||||
construct new() {}
|
||||
|
||||
foo() { System.print("Foo called.") }
|
||||
|
||||
bar() { System.print("Bar called.") }
|
||||
}
|
||||
|
||||
var test = Test.new()
|
||||
for (method in ["foo", "bar"]) {
|
||||
Meta.eval("test.%(method)()")
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
name:="len"; "this is a test".resolve(name)() //-->14
|
||||
Loading…
Add table
Add a link
Reference in a new issue