YAPC::EU 2018 Glasgow Update!
This commit is contained in:
parent
22f33d4004
commit
4e2d22a71d
1170 changed files with 15042 additions and 3047 deletions
44
Task/Delegates/Phix/delegates-1.phix
Normal file
44
Task/Delegates/Phix/delegates-1.phix
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
enum OTHER, OPERATION
|
||||
|
||||
function operation(object o)
|
||||
integer rid = o[OPERATION]
|
||||
if rid!=NULL then
|
||||
return call_func(rid,{})
|
||||
end if
|
||||
return "no implementation"
|
||||
end function
|
||||
|
||||
function xthing()
|
||||
return "default implementation"
|
||||
end function
|
||||
|
||||
function newX()
|
||||
return {1,routine_id("xthing"),2}
|
||||
end function
|
||||
|
||||
function newY()
|
||||
object res = newX()
|
||||
res[OTHER] = "something else"
|
||||
-- remove delegate:
|
||||
res[OPERATION] = NULL
|
||||
return res
|
||||
end function
|
||||
|
||||
function zthing()
|
||||
return "delegate implementation"
|
||||
end function
|
||||
|
||||
function newZ()
|
||||
object res = newX()
|
||||
-- replace delegate:
|
||||
res[OPERATION] = routine_id("zthing")
|
||||
return res
|
||||
end function
|
||||
|
||||
object x = newX(),
|
||||
y = newY(),
|
||||
z = newZ()
|
||||
|
||||
?operation(x)
|
||||
?operation(y)
|
||||
?operation(z)
|
||||
7
Task/Delegates/Phix/delegates-2.phix
Normal file
7
Task/Delegates/Phix/delegates-2.phix
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
?operation(x)
|
||||
try -- (since rid=NULL check commented out)
|
||||
?operation(y)
|
||||
catch e
|
||||
?"oops, no implementation"
|
||||
end try
|
||||
?operation(z)
|
||||
32
Task/Delegates/Scala/delegates.scala
Normal file
32
Task/Delegates/Scala/delegates.scala
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
trait Thingable {
|
||||
def thing: String
|
||||
}
|
||||
|
||||
class Delegator {
|
||||
var delegate: Thingable = _
|
||||
|
||||
def operation: String = if (delegate == null) "default implementation"
|
||||
else delegate.thing
|
||||
}
|
||||
|
||||
class Delegate extends Thingable {
|
||||
override def thing = "delegate implementation"
|
||||
}
|
||||
|
||||
// Example usage
|
||||
// Memory management ignored for simplification
|
||||
object DelegateExample extends App {
|
||||
|
||||
val a = new Delegator
|
||||
assert(a.operation == "default implementation")
|
||||
// With a delegate:
|
||||
val d = new Delegate
|
||||
a.delegate = d
|
||||
assert(a.operation == "delegate implementation")
|
||||
// Same as the above, but with an anonymous class:
|
||||
a.delegate = new Thingable() {
|
||||
override def thing = "anonymous delegate implementation"
|
||||
}
|
||||
assert(a.operation == "anonymous delegate implementation")
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue