xp-framework/rfc — Issue #195: Type-dispatch methods
Author: thekid
Created: 2011-10-17 16:05:08+00:00
State: open
Labels: draft, framework

Scope of Change
====
Methods will be allowed to have variants of themselves with different
type signatures.

Rationale
====
Code simplification.

Functionality
====
Instead of having one method with a big if / else block inside, we will
allow declaring separate methods, each with their type literals embedded.

Declaration
----
Current:

```php

```

New:

```php

```

Dispatch
----
Dispatch will be performed at runtime, except if a compiler / code
preprocessor can determine exactly.

Here's an example of a generated dispatcher method:

```php
array(
'setXml·Tree' => array('!Tree'),
'setXml·þstring' => array('!string'),
),
);

$args= func_get_args();
foreach (@$overloads[sizeof($args)] as $method => $signature) {
foreach ($signature as $i => $literal) {
if (NULL === $args[$i]) {
if ('!' === $literal{0} || !strstr($literal, '.')) continue 2;
} else {
if (!Type::forName(substr($literal, 1))->isInstance($args[$i])) continue 2;
}
}
return call_user_func_array(array($this, $method), $args);
}

raise(
'lang.MethodNotImplementedException',
'No overload for ['.implode(', ', array_map(array('xp', 'typeOf'), $args)).']',
'setXml'
);
}
?>
```

Reflection
----
Reflection will list one method, "setXml" in this case. A reflective
invocation will also apply dispatch logic.

Security considerations
====
n/a

Speed impact
====
TODO

Dependencies
====
xp-framework/rfc #197

Related documents
====
* http://xp-framework.net/rfc/contrib/rfc0195.diff
Implementing patch
* http://experiments.xp-forge.net/xml/browse?arena,rfc0195
Experiment