[Xotcl] Suggestion needed

Gustaf Neumann neumann at wu.ac.at
Tue Jan 20 21:21:28 CET 2015


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 <mailto: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