Getting the name of base class object in XOTCL 
Author Message
 Getting the name of base class object in XOTCL

Hi there,

Am I able to explicitly (no next) call base class init instproc in the
init instproc of a derived class ?

Like...

Class Mother
Mother instproc init {

Quote:
} {
}

Class Daughter -superclass Mother
Daughter instproc init {
Quote:
} {

  Mother::init (???)

Quote:
}

In other OO systems like C++ if I have

class Mother
{
Mother();

Quote:
};

class Daughter : public Mother
{
Daughter();

Quote:
};

I am able to call the constructor of Mother from Daughter's
constructor.

Daughter::Daughter()
{
  // I think I can do this
  Mother();

Quote:
}



Fri, 08 Aug 2008 17:37:59 GMT  
 Getting the name of base class object in XOTCL
Hi!

No. The is not such possiblity in XOTcl. I think it is because of
internal method dispatching mechanism. Also Smalltalk (the mother of
OO) does not offer it. In my opinion is also not OO-style to do it.
Consider that in XOTcl you can build
class hieraries dynamically by using mixin. Explicit calls to same base
class whould make it very infelxible and static.

If you need to do it, you probably should spilt (refactor) the
functionality of base method
into several methods.

Sample

Class A
A instproc foo {} {
     my fooA
     my fooB

Quote:
}

Class C -superclass A
C instproc foo {} {
     my fooC
     next
Quote:
}

Class D -superclass C
D instproc foo {} {
    # I do not need fooC
    my fooA
    my fooB

Quote:
}

The example is constructed. Many problems can be solved by good naming
of
methods or building name synonims that can be used in another context
(The technique is often used in Smalltalk programms)

Class A
A instproc foo {} {
    # I am just synonim for baseFoo
     my baseFoo

Quote:
}

A instproc baseFoo {} {
    # or even more
    my veryBaseFoo
Quote:
}

You will normally overwrite method 'foo' but you can always call
'baseFoo' if you
want to jump over method call hierarchy chain directly into special
implementation.

For some speciall cases you can also build the class hierarchy
dynamically

Class A
Class B
B create ::b -mixin A

Artur



Sat, 09 Aug 2008 15:46:12 GMT  
 Getting the name of base class object in XOTCL
In addition to Arthur's reply: What are you trying to achieve? The
example having Daughter a subclass of Mother looks somewhat strange
from the oo point of view. Calling directly some other methods as you
suggested will lead to problems with interceptors (mixins, filters).

If there are really good reasons skip certain methods, you can always
use guarded methods, where you can provide a predicate (guard) to
the methods to control the execution of code blocks. The following
example skips the code block in a paren't class and executes the
block in the grandparent's class.

   Class A -instproc init {{guard 1}} {
     if {[expr $guard]} {puts "[self class] doing something"}
     next
   }
   Class B -superclass A -instproc init {{guard 1}} {
     if {[expr $guard]} {puts "[self class] doing something"}
     next
   }
   Class C -superclass B -instproc init {{guard 1}} {
     if {[expr $guard]} {puts "[self class] doing something"}
     next {[self class] eq "::A"}
   }

   C c1

the result is

   ::C doing something
   ::A doing something



Sat, 09 Aug 2008 17:40:34 GMT  
 Getting the name of base class object in XOTCL
Thank you both for your replies.

My motivation is to have a class that is derived from others and at
construction time needs to pass different arguments to the constructors
of the base classes.

Class A

Class B

Class C -superclass {A B}

C instproc init {argA1 argA2 argB1 argB2} {
  ::A::init $argA1 $argA2
  ::B::init $argB1 $argB2

Quote:
}

I guess something like you said:

next {[self class] eq "::A"}

does the switching to the desired class at next (I don't know yet how
the guard thing works).

So I guess it could be something like:

C instproc init {argA1 argA2 argB1 argB2} {
  next {[self class] eq "::A"} $argA1 $argA2
  next {[self class] eq "::A"} $argB1 $argB2

Quote:
}

Will have to try it, I guess there will be problems with passing the
args and will have to use eval as usual.


Sat, 09 Aug 2008 19:37:39 GMT  
 Getting the name of base class object in XOTCL
Constructors are used (should be used) to initialize state of objekt
(initial state) so
the better solution for XOTcl are parameters.

Class A -parameter par1
Class B -parameter par2
Class C -superclass {A B}

It is not so good idea to have multi arguments constructors in XOTcl
with differents meaning. You can even with XOTcl connect parameters to
methods (setter, getter)  if you need some initialization action.

Class C -parameter {{a -setter myset}}

On the other hand 'init' method are just normall methods in XOTcl. you
can write own
initialize mehtods for it or call them once again after normal
initialization.

Class A
A instproc init {arg1 arg2} {
    # .....

Quote:
}

A initproc initA {arg1 arg2} {
    my init $arg1 $arg2
Quote:
}

Class B
B instproc init {arg3 arg4} {
   # ....
Quote:
}

B initproc initB {arg3 arg4} {
    my init $arg3 $arg4

Quote:
}

Class C -supperclass {A B}
Class C init {arg1 arg2 arg3 arg4} {
     my initA $arg1 $arg2
     my initB $arg3 $arg4
     # no next call

Quote:
}

I think a better sollution is not to use C++ like constructors but own
init state methods.
Anyway Smalltalk has no constructors at all. Class methods are good
place
to implement own fabric methods, that generate suitable intialized
objects.

Class A
A instproc specialInit {arg1} {

Quote:
}

# Class method as instance fabric
A proc getInstanceForJobA {} {
    set inst [new]
    $inst specialInit
    return $inst

Quote:
}

Because XOTcl is not strong typed there are no such elegant way to
choose
suitable contructor at compile time dependant from arguments type, so
the
constructrors have not such power like by C++ (or C#, or Java).

Artur



Sat, 09 Aug 2008 21:04:26 GMT  
 Getting the name of base class object in XOTCL
Sorry one example whould not work. Here comes a corrected version

Class A
A instproc init {arg1 arg2} {
    my initA $arg1 $arg2

Quote:
}

A initproc initA {arg1 arg2} {
    # ....

Quote:
}

Class B
B instproc init {arg3 arg4} {
    my initB $arg3 $arg4

Quote:
}

B initproc initB {arg3 arg4} {
    # ....

Quote:
}

Class C -supperclass {A B}
Class C init {arg1 arg2 arg3 arg4} {
     my initA $arg1 $arg2
     my initB $arg3 $arg4
     # no next call
Quote:
}



Sat, 09 Aug 2008 21:10:49 GMT  
 Getting the name of base class object in XOTCL
Quote:
> I don't know yet how the guard thing works.

This is nothing special concerning xotcl, that's purely tcl. you pass
an expression and evaluate it in the appropriate context.

The simplest code is most probably

   Class A -parameter {a1 a2}
   Class B -parameter {b1 b2}
   Class C -superclass {A B}

   C c1 -a1 100 -a2 101 -b1 200 -b2 201

alternatively, you could do without much magic

  Class A -instproc init {argA1 argA2} { ..... }
  Class B -instproc init {argA1 argA2 argB1 argB2} {
     ....; next $argA1 $argA2
   }
  Class C -superclass {A B}

  C c1 100 101 200 201

Most probably, parameters are the better way to go for, since this is
easy extensible and interceptor-friendly.



Sat, 09 Aug 2008 21:47:56 GMT  
 Getting the name of base class object in XOTCL
Thanks again for your replies !

The parameter idea is a good one, but I do not go for parameters lately
(perhaps it is just that I have read they eventually this xotcl feature
will not be mimicked in the oo core extension and that influenced me
somehow :).

What I have eventually implemented looks exactly like last reply from
GN with next.

But I have come to realise what funny thing is to have more than three
or four chained superclasses and letting them pass one another the
corresponding arguments.

Perhaps the parameter option is best after all.



Sun, 10 Aug 2008 05:33:34 GMT  
 Getting the name of base class object in XOTCL

Quote:

> The parameter idea is a good one, but I do not go for parameters lately
> (perhaps it is just that I have read they eventually this xotcl feature
> will not be mimicked in the oo core extension and that influenced me
> somehow :).

If you're going with XOTcl, do use the parameter stuff. It's weird and
bizarre and unlike any other OO system I've ever encountered, but it is
also at the heart of much of the way XOTcl operates.

Donal.



Sun, 10 Aug 2008 05:54:36 GMT  
 Getting the name of base class object in XOTCL
Donal, there is nothing wierd and bizarre about parameters. The
parameter command in xotcl is a conveniant way to specify accessor
functions, very similar to a slot definition in e.g. common lisp. i am
actually not aware of any dynamic object system that is not based on
slots.

The following definitions are mostly equivalent:

  Class C -parameter c

  Class D
  D method d {args} {
    switch [llength $args] {
      0 {my set d}
      1 {my set d $args}
      default {error "use d with 1 or 0 arguments"}
    }
  }

  C c1 -c 1
  D d1 -d 2

  puts "c: [c1 c] d: [d1 d]"

btw, i would NOT regard parameters as "at the heart of much of the way
XOTcl operates". The heart of xotcl is a dynamic object system with
highly flexible composition techniques based on mixin classes and
filters.

Quote:
>From the language point of view, the parameter method is not important

at all, it is syntactic sugar. Btw, recently i developed a replacement
for parameters based on delegation, which can be much easier extended.
We are working currently on a relational abstraction layer based on
this slot mechanism, where much more properties of slots have to be
defined. This replacement is likely to find its way into the
forthcoming xotcl versions.

There are a couple of reasons against using the explicit arguments to
init and passing these around:
 - changing the signatore (actually the argument list) of functions
   makes them harder to use,
 - methods with mixin-classes have a hard time interpreting the
   argument lists, esp. if they are used on different generalization
   levels
 - long argument lists are error prone
 - the style of passing though arguments requires high maintenance,
   when e.g. the base class is to be extended
 - the argument passing style leads to a strong coupling of classes
   making dynamic class changes difficult.



Sun, 10 Aug 2008 08:32:10 GMT  
 Getting the name of base class object in XOTCL

Quote:

> btw, i would NOT regard parameters as "at the heart of much of the way
> XOTcl operates". The heart of xotcl is a dynamic object system with
> highly flexible composition techniques based on mixin classes and
> filters.

I think the things I call parameters are not the things you call
parameters. Sorry for any confusion caused.

Donal.



Mon, 11 Aug 2008 21:57:55 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Getting a class object from a string containg its name

2. Getting a class from a string containing its name

3. Getting a class from its name

4. getting class information from module name

5. Getting an instance's class name?

6. column names getting garbled in Vx-Rexx C/S database object

7. "Base not a class object" solved

8. newbie question, assigning object to class name

9. Checking an objects Class Name at run time

10. Q: Class name as parameter for object construction

11. Calling a derived class functions from base class

12. Class variables in parent and child classes with the same name

 

 
Powered by phpBB® Forum Software