September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -1,54 +1,46 @@
import extensions.
import system'routines.
import extensions;
import system'routines;
class IOperable
interface IOperable
{
operate
[
NotSupportedException new; raise.
]
abstract operate() {}
}
class Operable :: IOperable
class Operable : IOperable
{
operate
= "delegate implementation".
constructor() {}
operate()
= "delegate implementation";
}
class Delegator
{
object theDelegate.
object theDelegate;
constructor new
[
theDelegate := nil.
]
set Delegate(object)
{
theDelegate := object
}
set Delegate:obj
[
if ($nil == obj)
[ theDelegate := $nil. ];
[ theDelegate := obj. ].
]
internal operate(operable)
= "default implementation";
operate(Object operable)
= "default implementation".
internal operate(IOperable operable)
= operable.operate();
operate(IOperable operable)
= operable operate.
operate
<= operate(theDelegate).
operate()
<= operate(theDelegate);
}
program =
[
var delegator := Delegator new.
public program()
{
var delegator := new Delegator();
(nil, Object new, Operable new) forEach(:o)
[
delegator Delegate := o.
new object[]{nil, new Object(), new Operable()}.forEach:(o)
{
delegator.Delegate := o;
console printLine(delegator operate).
].
].
console.printLine(delegator.operate())
}
}

View file

@ -1,53 +1,47 @@
import extensions.
import system'routines.
import extensions;
import system'routines;
class Operable
{
operable = $self.
Operable = self;
operate
= "delegate implementation".
operate()
= "delegate implementation";
}
class Delegator
{
object theDelegate.
prop object Delegate;
constructor new
[
theDelegate := nil.
]
constructor()
{
Delegate := nil
}
set Delegate:obj
[
if ($nil == obj)
[ theDelegate := $nil. ];
[ theDelegate := obj. ].
]
operate
[
operate()
{
// if the object does not support "get&operable" message - returns nil
var anOperable := theDelegate operable \ back:$nil.
var operable := Delegate.Operable \ back:nil;
if ($nil == anOperable)
[
^ "default implementation".
];
[
^ anOperable operate.
].
]
if (nil == operable)
{
^ "default implementation"
}
else
{
^ operable.operate()
}
}
}
program =
[
var delegator := Delegator new.
public program()
{
var delegator := new Delegator();
(nil, Object new, Operable new) forEach(:o)
[
delegator Delegate := o.
new object[]{nil, new Object(), new Operable()}.forEach:(o)
{
delegator.Delegate := o;
console printLine(delegator operate).
].
].
console.printLine(delegator.operate())
}
}

View file

@ -0,0 +1,54 @@
interface Thingable {
method : virtual : public : Thing() ~ String;
}
class Delegator {
@delegate : Thingable;
New() {
}
method : public : SetDelegate(delegate : Thingable) ~ Nil {
@delegate := delegate;
}
method : public : Operation() ~ String {
if(@delegate = Nil) {
return "default implementation";
}
else {
return @delegate->Thing();
};
}
}
class Delegate implements Thingable {
New() {
}
method : public : Thing() ~ String {
return "delegate implementation";
}
}
class Example {
function : Main(args : String[]) ~ Nil {
# Without a delegate:
a := Delegator->New();
Runtime->Assert(a->Operation()->Equals("default implementation"));
# With a delegate:
d := Delegate->New();
a->SetDelegate(d);
Runtime->Assert(a->Operation()->Equals("delegate implementation"));
# Same as the above, but with an anonymous class:
a->SetDelegate(Base->New() implements Thingable {
method : public : Thing() ~ String {
return "anonymous delegate implementation";
}
});
Runtime->Assert(a->Operation()->Equals("anonymous delegate implementation"));
}
}

View file

@ -0,0 +1,27 @@
trait Thingable {
fn thing(&self) -> &str;
}
struct Delegator<T>(Option<T>);
struct Delegate {}
impl Thingable for Delegate {
fn thing(&self) -> &'static str {
"Delegate implementation"
}
}
impl<T: Thingable> Thingable for Delegator<T> {
fn thing(&self) -> &str {
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
}
}
fn main() {
let d: Delegator<Delegate> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
println!("{}", d.thing());
}

View file

@ -0,0 +1,41 @@
#![feature(specialization)]
trait Thingable {
fn thing(&self) -> &str;
}
struct Delegator<T>(Option<T>);
struct Delegate {}
impl Thingable for Delegate {
fn thing(&self) -> &'static str {
"Delegate implementation"
}
}
impl<T> Thingable for Delegator<T> {
default fn thing(&self) -> &str {
"Default implementation"
}
}
impl<T: Thingable> Thingable for Delegator<T> {
fn thing(&self) -> &str {
self.0.as_ref().map(|d| d.thing()).unwrap_or("Default implmementation")
}
}
fn main() {
let d: Delegator<i32> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<i32> = Delegator(Some(42));
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(None);
println!("{}", d.thing());
let d: Delegator<Delegate> = Delegator(Some(Delegate {}));
println!("{}", d.thing());
}

View file

@ -0,0 +1,6 @@
Object
subclass:#Thingy
instanceVariableNames:''
thing
^ 'thingy implementation'

View file

@ -0,0 +1,10 @@
Object
subclass:#Delegator
instanceVariableNames:'delegate'
delegate:something
delegate := something
operation
^ delegate
perform:#thing ifNotUnderstood:'default implementation'.

View file

@ -0,0 +1,8 @@
|d|
d := Delegator new.
d operation.
-> 'default implementation'
d delegate:(Thingy new).
d operation.
-> 'thingy implementation'