Delegates: Difference between revisions
Added FreeBASIC
imported>Arakov |
(Added FreeBASIC) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 839:
var delegator := new Delegator();
new object[]{nil, new Object(), new Operable()}.forEach::(o)
{
delegator.Delegate := o;
Line 860:
class Delegator
{
constructor()
Line 869:
operate()
{
// if the object does not support "
var operable := Delegate.Operable \ back
if (nil == operable)
Line 887:
var delegator := new Delegator();
new object[]{nil, new Object(), new Operable()}.forEach::(o)
{
delegator.Delegate := o;
Line 900:
delegate implementation
</pre>
=={{header|FreeBASIC}}==
{{trans|Phix}}
FreeBASIC does not directly support objects or delegation functions. However, you can achieve similar functionality using user-defined types and function pointers.
<syntaxhighlight lang="vbnet">
Type Objeto
operation As Function() As String
other As String
End Type
Function xthing() As String
Return "default implementation"
End Function
Function newX() As Objeto
Dim As Objeto o
o.operation = @xthing
Return o
End Function
Function newY() As Objeto
Dim As Objeto o = newX()
o.other = "something else"
o.operation = 0 ' remove delegate
Return o
End Function
Function zthing() As String
Return "delegate implementation"
End Function
Function newZ() As Objeto
Dim As Objeto o = newX()
o.operation = @zthing ' replace delegate
Return o
End Function
Function operation(o As Objeto) As String
Return Iif(o.operation <> 0, o.operation(), "no implementation")
End Function
Dim As Objeto x = newX()
Dim As Objeto y = newY()
Dim As Objeto z = newZ()
Print operation(x)
Print operation(y)
Print operation(z)
Sleep</syntaxhighlight>
{{out}}
<pre>Similar as Phix entry.</pre>
=={{header|F_Sharp|F#}}==
Line 2,915 ⟶ 2,967:
=={{header|Wren}}==
Wren is dynamically typed so we can plug any kind of delegate into the Delegator.
<syntaxhighlight lang="
thing { }
}
|