No registered users in community xowiki
in last 10 minutes
in last 10 minutes
Re: [Xotcl] assign proc - how to execute assign from superclass?
From: Gustaf Neumann <neumann_at_wu-wien.ac.at>
Date: Wed, 05 Jan 2011 23:08:25 +0100
Dear Krzysztof,
Good example, no easy answer!
When you create an object b1
B create b1 -foo 10
your goal is most probably to chain the assign methods of
the slots foo of the classes A and B along the precedence
path of the object b1.
The simple but unsatisfying approach is to call the "right"
assign method manually:
===========================================
Class create B -superclass A -slots {
Attribute create foo -proc assign {domain var value} {
if {$value < 0} {
error "Value cannot be less than 0"
}
::A::slot::foo assign $domain $var $value
}
}
===========================================
This crude approach will work, but is unsatisfying, since
the "assign" method of class B has to know that the
superclass of B is A and it has to know, that
the same-named slot of class A exists and has
as well an assign method.
In the approach below this information is obtained
via introspection by a method named "continue",
which is supposed to be quite generic.
===========================================
Class create B -superclass A -slots {
Attribute create foo -proc assign {domain var value} {
if {$value < 0} {
error "Value cannot be less than 0"
}
my continue $domain $var $value
}
}
===========================================
Below you will find the implementation of method
"continue". Note that there will be some changes in
this are in XOTcl 2 and NX.
hope this helps
-gustaf neumann
===========================================
::xotcl::Attribute instproc continue {domain var value} {
set order [$domain info precedence]
set current [[my info parent] info parent]
#
# find the next class from the precedence order
#
for {set order [lassign $order c]} \
{$c ne $current && $c ne ""} \
{set order [lassign $order c]} {continue}
#
# iterate on the remaining order until we find next
# assign method
foreach c $order {
set slot ${c}::slot::$var
if {[info command $slot] eq ""} continue
if {[$slot info methods assign] ne ""} {
#
# invoke it
#
return [$slot assign $domain $var $value]
}
}
}
===========================================
On 05.01.11 15:50, Krzysztof Frukacz wrote:
> Hello,
>
> I have a question for which I haven't been able to find
> the answer.
> I have two classes:
>
> Class create A -slots {
> Attribute foo -proc assign {domain var value} {
> if {$var > 5} {
> error "Value cannot be bigger than 5"
> }
> $domain set $var $value
> }
> }
>
> Class create B -superclass A -slots {
> Attribute foo -proc assign {domain var value} {
> if {$var < 0} {
> error "Value cannot be less than 0"
> }
> #I don't know how to call assign for foo in class
> A here
> }
> }
>
> What I want to to is for assign proc from B to call assign
> proc from A too check both conditions.
> Thank you for your help.
>
Date: Wed, 05 Jan 2011 23:08:25 +0100
Dear Krzysztof,
Good example, no easy answer!
When you create an object b1
B create b1 -foo 10
your goal is most probably to chain the assign methods of
the slots foo of the classes A and B along the precedence
path of the object b1.
The simple but unsatisfying approach is to call the "right"
assign method manually:
===========================================
Class create B -superclass A -slots {
Attribute create foo -proc assign {domain var value} {
if {$value < 0} {
error "Value cannot be less than 0"
}
::A::slot::foo assign $domain $var $value
}
}
===========================================
This crude approach will work, but is unsatisfying, since
the "assign" method of class B has to know that the
superclass of B is A and it has to know, that
the same-named slot of class A exists and has
as well an assign method.
In the approach below this information is obtained
via introspection by a method named "continue",
which is supposed to be quite generic.
===========================================
Class create B -superclass A -slots {
Attribute create foo -proc assign {domain var value} {
if {$value < 0} {
error "Value cannot be less than 0"
}
my continue $domain $var $value
}
}
===========================================
Below you will find the implementation of method
"continue". Note that there will be some changes in
this are in XOTcl 2 and NX.
hope this helps
-gustaf neumann
===========================================
::xotcl::Attribute instproc continue {domain var value} {
set order [$domain info precedence]
set current [[my info parent] info parent]
#
# find the next class from the precedence order
#
for {set order [lassign $order c]} \
{$c ne $current && $c ne ""} \
{set order [lassign $order c]} {continue}
#
# iterate on the remaining order until we find next
# assign method
foreach c $order {
set slot ${c}::slot::$var
if {[info command $slot] eq ""} continue
if {[$slot info methods assign] ne ""} {
#
# invoke it
#
return [$slot assign $domain $var $value]
}
}
}
===========================================
On 05.01.11 15:50, Krzysztof Frukacz wrote:
> Hello,
>
> I have a question for which I haven't been able to find
> the answer.
> I have two classes:
>
> Class create A -slots {
> Attribute foo -proc assign {domain var value} {
> if {$var > 5} {
> error "Value cannot be bigger than 5"
> }
> $domain set $var $value
> }
> }
>
> Class create B -superclass A -slots {
> Attribute foo -proc assign {domain var value} {
> if {$var < 0} {
> error "Value cannot be less than 0"
> }
> #I don't know how to call assign for foo in class
> A here
> }
> }
>
> What I want to to is for assign proc from B to call assign
> proc from A too check both conditions.
> Thank you for your help.
>