View · Search · Index
No registered users in community xowiki
in last 10 minutes

Re: [Xotcl] For anyone who wants their XOTcl instantiation safer

From: Gustaf Neumann <neumann_at_wu.ac.at>
Date: Thu, 05 Aug 2010 19:59:30 +0200

Hi Kristoffer,

i just saw a buch of mails from you, i guess, you have answered by now
most of your questions.
Your example is perfectly fine. The reader should be aware, that you
traded in "new"
object parameterization against arguments to init, which is (in my
opinion) bad.

In the example with just "new"
> Class SafeInit
> SafeInit instproc new {args} {
> return [next [concat [list -init] $args]]
> }
>
> # Mix safer [new] and [new-with] into every class
> Class instmixin SafeInit
>
> # Test:
> Class Foo -parameter {
> {animal horse}
> }
>
you have lost all means to pass a value for "animal", except when you
pass it to init (i know "new-with" is different). One can can certainly
say, the first argument passed to init is the "animal", then default
handling is ugly (in the general case) and you have to deal with ugly
positional arguments. This technique does not scale:
What, if one inherits additional parameters from a superclass
(of Foo), or when the superclass is extended? If every argument
to init corresponds to a parameter, one has to extend the signature
of the involved init methods. Adding arguments is not really an
option when the parameters are provided via mixins, or when
the object-class and class-class relationships can changed dynamically.
Another issue is passing arguments of init to superclasses via next.
If the inits of the superclasses have different signatures, the
code becomes error prone.

My hypothesis is, that arguments for init are the poor men's
approach for object parameterization, if one has nothing better.
Arguments to contructors come more often into discussion
when porting c++ style programs to xotcl, rather than
from intrinsics needs when coding in xotcl.

Guess, you will like the next xotcl release. The new object system
has scripted initialization (quite similar to your "new-with" example)
while preserving parameterization (highly orthogonal object and
method parameterization, same code for scripted methods and
c-implemented methods).

Below is an example to define a classical stack in the new syntax...

all the best
-gustaf neumann

*Class create* Stack {

    *:method init* {} {
      *set* :things ""
    }

    *:method* push {thing} {
       *set* :things [*linsert* ${:things} 0 $thing]
       *return* $thing
    }

    *:method* pop {} {
       *set* top [*lindex* ${:things} 0]
       *set* :things [*lrange* ${:things} 1 end]
       *return* $top
    }
}



-- 
Univ.Prof. Dr. Gustaf Neumann
Institute of Information Systems and New Media
WU Vienna
Augasse 2-6, A-1090 Vienna, AUSTRIA