This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,5 @@
(defun augment-instance-with-slots (instance slots)
(change-class instance
(make-instance 'standard-class
:direct-superclasses (list (class-of instance))
:direct-slots slots)))

View file

@ -0,0 +1,12 @@
CL-USER> (let* ((instance (make-instance 'foo :bar 42 :baz 69))
(new-slots '((:name xenu :initargs (:xenu)))))
(augment-instance-with-slots instance new-slots)
(reinitialize-instance instance :xenu 666)
(describe instance))
#<#<STANDARD-CLASS NIL {1003AEE2C1}> {1003AEE271}>
[standard-object]
Slots with :INSTANCE allocation:
BAR = 42
BAZ = 69
XENU = 666

View file

@ -0,0 +1,31 @@
struct Dynamic(T) {
private T[string] vars;
@property T opDispatch(string key)() pure nothrow {
return vars[key];
}
@property void opDispatch(string key, U)(U value)/*pure*/ nothrow {
vars[key] = value;
}
}
void main() {
import std.variant, std.stdio;
// If the type of the attributes is known at compile-time:
auto d1 = Dynamic!double();
d1.first = 10.5;
d1.second = 20.2;
writeln(d1.first, " ", d1.second);
// If the type of the attributes is mixed:
auto d2 = Dynamic!Variant();
d2.a = "Hello";
d2.b = 11;
d2.c = ['x':2, 'y':4];
d2.d = (int x) => x ^^ 3;
writeln(d2.a, " ", d2.b, " ", d2.c);
immutable int x = d2.b.get!int;
}

View file

@ -0,0 +1,13 @@
import std.stdio, std.variant, std.conv;
struct Dyn {
Variant[string] data;
alias data this;
}
void main(string[] args) {
Dyn d;
const attribute_name = text("attribute_", args.length);
d[attribute_name] = "something";
writeln(d[attribute_name]);
}

View file

@ -0,0 +1,27 @@
#subject foo.
#class FieldContainer
{
#field theValue.
#method foo'set : anObject
[
theValue := anObject.
]
#method foo'get = theValue.
}
#symbol Program =
[
#var anObject := 234.
// adding a field
anObject := anObject &= FieldContainer.
anObject foo'set:"bar".
'program'Output << anObject << ".foo=" << anObject foo.
'program'input get.
].