Context and Multiple Dispatching
PWB has a mechanism for multiple dispatching.
Some background:
In an Object Oriented language, when you send a message to an object, which method is executed is defined by the class of the receiver.
class ObjectA{function a(){echo 'a';}}
class ObjectB{function a(){echo 'b';}}
$a = new ObjectA();
$a->a();//echoes 'a';
$b = new ObjectB();
$b->a(); //echoes 'b';
As you can see, same message + different class -> different method. This is single dispatching.
PWB provides a way to use Multiple Dispatching. That is, to choose the method based on the types of the parameters to the function. It provides the macro defmdf ("define multiple dispatch function"), and the function mdcall.
#@defmdf assign($user:User, $area:Area){
$user->addArea($area);
}@#
#@defmdf assign($user:User, $group:Group){
$user->addGroup($group);
}@#
#@defmdf assign($file:File, $group:Group){
$file->addGroup($group);$group->attachFile($file);
}@#
And then, to use them:
mdcall('assign', array($user, $group));
Context dispatching, is a similar concept, but based on the types of the context instead of the type of the parameters:
#@defmdf showNews [NewsList] ($news:News){
echo $news->title; echo $news->author->name;
}@#
#@defmdf showNews [NewsView] ($news:News){
echo $news->title; echo $news->title; echo $news->body;
}@#
#@defmdf showNews [AuthorView<-NewsList] ($news:News){
echo $news->title;
}@#
In this case, we show news.
Showing a news article, when you are showing a list of news, is just showing the title, and the author.
Showing a news article, when you are showing only a news article, is showing the title, the author, and the body.
Showing a news article, when you are showing a list of news, in the context of "showing an author", is just showing the title (we already know the author's name from the context).
So, we would call the function:
mdcompcall('showNews',array($context, $news));
the first parameter to showNews (the first element in the array) is the context to use.
This techniques are tholoughly used in YATTAA!
