-- Objects.russell
open <
write: proc[characterString.val->[]] ~ lambda s: characterString.val in
putString <stream~stdout, string~s>
ni ,
newLine: proc[[]->[]] ~ lambda x:[] in
putString <stream~stdout, string~"\n">
ni ,
stringVal ~ characterString.val
> in
open <*
Object: type ~ prod [
setName: proc[ stringVal -> [] ],
getName: proc[ [] -> stringVal ],
copy: proc[ [] -> Object ]
] ,
MkObject: func[ stringVal -> Object ] ~
lambda originalName: stringVal in
open ref[ stringVal ] in
open < myName ~ new<> > in
assign <lhs~myName, rhs~originalName> ;
<*
setName ~ lambda newName: stringVal in
assign <lhs~myName, rhs~newName>
ni ,
getName ~ lambda x:[] in
valOf myName
ni ,
copy ~ lambda x:[] in
MkObject(getName<>)
ni
*>
ni
ni
ni
*> in
open < obj1 ~ MkObject "This is wrong" > in
open < obj1Alias ~ obj1, obj2 ~ obj1.copy<> > in
obj1Alias.setName "This is object 1" ;
obj2.setName "This is object 2" ;
write "Name of obj 1: "; write (obj1.getName<>) ; newLine<> ;
write "Name of obj 1 alias: "; write (obj1Alias.getName<>) ; newLine<> ;
write "Name of obj 2: "; write (obj2.getName<>) ; newLine<>
ni
ni
ni
ni