Delegates: Difference between revisions

1,168 bytes added ,  1 month ago
Added FreeBASIC
(→‎{{header|TXR}}: New section.)
(Added FreeBASIC)
 
(5 intermediate revisions by 2 users not shown)
Line 797:
 
=={{header|Elena}}==
ELENA 56.0x :
 
Using multi methods:
Line 805:
interface IOperable
{
abstract operate() {};
}
Line 839:
var delegator := new Delegator();
new object[]{nil, new Object(), new Operable()}.forEach::(o)
{
delegator.Delegate := o;
Line 860:
class Delegator
{
prop object Delegate : prop;
constructor()
Line 869:
operate()
{
// if the object does not support "get&operableOperable" message - returns nil
var operable := Delegate.Operable \ back:(nil);
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="ecmascriptwren">class Thingable {
thing { }
}
2,122

edits