In order to use Polymorphism (and dynamic dispatch in particular) with templates you should be able to define templates on entities.

Currently we can do something polymorphic with functions only, so:

entity Base {
}

define output(b: Base) {
// show simple output
}

entity Sub : Base {
function foo() {
// do something
}
}

define output(s: Sub) {
// show custom output for sub
}

define page doSomething(b: Base) {
init {
b.foo(); // this works
}
main {
output(b) // this does not call output(s: Sub)
}
}

An option would be to define a function on an entity, such as:

extend entity Sub {
define show() {
// show custom output for sub
}
}

And then in doSomething we can call b.show() instead of output(b).

Submitted by Tim on 18 March 2010 at 11:25

On 18 March 2010 at 12:30 Danny Groenewegen commented:

I’ve also proposed this a while ago, another advantage is that the plugin could then provide relevant templatecall completions once you type b. in the body of a template. It would also be easy to implement since it just reuses Java dispatch like we do now with the entity functions.

A recurring discussion related to this is whether it is worth the effort to implement multiple dispatch for templates (and functions) instead of relying on the Java dispatch semantics.

Log in to post comments