Polymorphic Delegates

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



Delegates with descendant types are assignment compatible, providing support for so called "Polymorpic Delegates".

Consider the following two delegate definitions:

type
  delegate Foo(sender: object; data: FooData);
  delegate FooEx(sender: object; data: FooDataEx);

You can assign a FooEx to something that expects a Foo; basically FooEx behaves as if it were a descendant of Foo.

This feature is especially relevant for writing WPF applications, as WPFs event routing system uses shared methods to register event handlers, expect a specific delegate. These methods expect a base delegate.

Compatibility Rules

A parameter of a delegate is compatible with the corresponding parameter of a method if the type of the delegate parameter(s) is more restrictive than the type of the method parameters. This guarantees that an argument passed to the delegate can be passed safely to the method.

Similarly, the return type of a delegate is compatible with the return type of a method if the return type of the method is more restrictive than the return type of the delegate, because this guarantees that the return value of the method can be cast safely to the return type of the delegate.

See Also

Delegates


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox