[Xotcl] Suggestion needed
Victor Mayevski
vitick at gmail.com
Thu Jan 22 09:29:06 CET 2015
Based on your input, I have created a proc to simplify the entire process:
#Create a proc ::nx::clone in order to put the NX OO system under a private
#namespace, so that the ::nx namespace doesn't get polluted with custom
methods.
#It also allows further unlimited cloning into other namespaces, where the
custom
#methods are inherited down the line.
###############################################################
proc ::nx::clone {ns {nsbody ""}} {
set cns [namespace current];
if {![regexp ^:: $ns]} {
set ns ::$ns;
};
namespace eval $ns $nsbody;
${cns}::Class create ${ns}::Object -superclasses ${cns}::Object;
${cns}::Class create ${ns}::Class -superclasses ${cns}::Class {
upvar cns cns
upvar ns ns
::nx::slotObj [self];
::nx::ObjectParameterSlot create [self]::slot::superclasses
\
-methodname "::nsf::methods::class::superclass" \
-elementtype class -multiplicity 1..n \
-default ${ns}::Object
};
proc ${ns}::clone {ns {nsbody ""}} [info body ::nx::clone];
namespace eval $ns {namespace export Object Class};
return $ns;
};
##################################################################
::nx::clone ::my {puts "you could put your custom package code here"};
::my::Object public method mymethod {} {puts "hello from mymethod in [:],
it will be inherited by the next clone"};
::my::Class create ::MyClass;
::MyClass create myobject;
::myobject mymethod;
::my::clone ::your;
::your::Class create YourClass;
::your::Object create yourobject;
::YourClass create yourobject1;
::yourobject mymethod;
::yourobject1 mymethod;
On Tue, Jan 20, 2015 at 12:21 PM, Gustaf Neumann <neumann at wu.ac.at> wrote:
> Dear Victor,
>
> below is a small improvement for the working suggestion:
>
> - the first suggestion used the constructor to fix the superclass
>
> - it is better, the change the default of the "superclasses" slot
> (inherited from nx::Class) from "nx::Object" to "nx::Class"
>
> - this is a slot, that is not linked to a variable, but to a method
> altering the class. Therefore the definition of this slot is slightly
> more magic.
>
> - This solution has the advantage, that one has not to care
> that further specializations of my::Class call in their
> constructor always "next". Furthermore, it should be
> slightly faster.
>
> all the best
> -gn
>
> namespace eval my {
>
> Class create Object -superclasses nx::Object
>
> Class create Class -superclasses nx::Class {
>
> # create a slot container ...
> ::nx::slotObj [self]
>
> # ... and redefine slot "superclasses" with a different default
> # value, such we can write
> #
> # my::Class create Foo
> #
> # instead of
> #
> # my::Class create Foo -superclasses ::my::Object
> #
> ::nx::ObjectParameterSlot create [self]::slot::superclasses \
> -methodname "::nsf::methods::class::superclass" \
> -elementtype class -multiplicity 1..n \
> -default [namespace current]::Object
> }
>
> namespace export Object Class
> }
>
> #
> # show parameter definition of "superclasses" with changed default
> #
> puts "parameter definition for '-superclasses': [my::Class info lookup parameters create super*]"
>
>
>
> Am 20.01.15 um 11:28 schrieb Victor Mayevski:
>
> That's beautiful! Thank you!
>
> On Tue, Jan 20, 2015 at 1:11 AM, Stefan Sobernig <
> stefan.sobernig at wu.ac.at> wrote:
>
>> A more complete solution (preserving any pre-existing superclass lists)
>> is as follows:
>>
>> package req nx
>>
>> namespace eval ::vitick {
>>
>> Class create Object -superclasses nx::Object
>>
>> Class create Class -superclasses nx::Class {
>> :method init {} {
>> ##
>> ## Rewire the default superclass
>> ##
>> if {[:info superclasses] eq "::nx::Object"} {
>> :configure -superclasses [namespace current]::Object
>> }
>>
>> }
>> }
>>
>> namespace export Object Class
>> }
>>
>> namespace import -force ::vitick::*
>>
>> puts [[Class create MyClass] info superclasses]; # default is:
>> ::vitick::Object
>> puts [[Class create AnotherClass -superclasses MyClass] info
>> superclasses]; # ::MyClass
>> puts [[Class create AnotherClass -superclasses MyClass] info superclasses
>> -closure]; # ::MyClass ::vitick::Object ::nx::Object
>>
>> //stefan
>>
>> Hi Victor,
>>>
>>> > Any suggestions?
>>>
>>> As always, there are several options. One way of nursing our custom
>>> Object/Class is to use proper specializations of nx::Object/nx::Class:
>>>
>>> package req nx
>>>
>>> namespace eval ::vitick {
>>>
>>> Class create Object -superclass nx::Object
>>>
>>> Class create Class -superclass nx::Class {
>>> :method init {} {
>>> :configure -superclasses [namespace current]::Object
>>> }
>>> }
>>>
>>> namespace export Object Class
>>> }
>>>
>>> namespace import -force ::vitick::*
>>>
>>> This is less invasive and you have the benefit of monitoring any
>>> changes/additions to nx::Object/nx::Class for free.
>>>
>>> Cheers,
>>> Stefan
>>> _______________________________________________
>>
>>
>
More information about the Xotcl
mailing list