June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,13 @@
(defprotocol Thing
(thing [_]))
(defprotocol Operation
(operation [_]))
(defrecord Delegator [delegate]
Operation
(operation [_] (try (thing delegate) (catch IllegalArgumentException e "default implementation"))))
(defrecord Delegate []
Thing
(thing [_] "delegate implementation"))

View file

@ -0,0 +1,15 @@
module Delegates
export Delegator, Delegate
struct Delegator{T}
delegate::T
end
struct Delegate end
operation(x::Delegator) = thing(x.delegate)
thing(::Any) = "default implementation"
thing(::Delegate) = "delegate implementation"
end # module Delegates

View file

@ -0,0 +1,11 @@
using .Delegates
a = Delegator(nothing)
b = Delegator("string")
d = Delegate()
c = Delegator(d)
@show Delegates.operation(a)
@show Delegates.operation(b)
@show Delegates.operation(c)

View file

@ -0,0 +1,25 @@
// version 1.1.51
interface Thingable {
fun thing(): String?
}
class Delegate(val responds: Boolean) : Thingable {
override fun thing() = if (responds) "delegate implementation" else null
}
class Delegator(d: Delegate) : Thingable by d {
fun operation() = thing() ?: "default implementation"
}
fun main(args: Array<String>) {
// delegate doesn't respond to 'thing'
val d = Delegate(false)
val dd = Delegator(d)
println(dd.operation())
// delegate responds to 'thing'
val d2 = Delegate(true)
val dd2 = Delegator(d2)
println(dd2.operation())
}

View file

@ -0,0 +1,25 @@
class NonDelegate { }
class Delegate {
method thing {
return "delegate implementation"
}
}
class Delegator (delegate = null) {
method operation {
if (delegate.respond_to(:thing)) {
return delegate.thing
}
return "default implementation"
}
}
var d = Delegator()
say "empty: #{d.operation}"
d.delegate = NonDelegate()
say "NonDelegate: #{d.operation}"
d.delegate = Delegate()
say "Delegate: #{d.operation}"