Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question about Snit

2 views
Skip to first unread message

Arjen Markus

unread,
Oct 16, 2008, 4:25:26 PM10/16/08
to
Hello,

I am experimenting a bit with Snit, to see how it could be used to
wrap certain numerical/mathematical/...
functions and provide a more convenient interface.

Two things I am wondering about are:
- Is it possible to define new methods for a particular Snit object
(after it has been created)?
- Is it possible to redefine an existing method?

The reason I ask, is that it would be very convenient to have, say, a
type that offers
one or more integration options and then define the function to be
integrated per object.
Something like:

snit::type numeric {
method integral {a b} {
}
method f {x} { puts "This is a dummy!" }
}

numeric create func
func define f {expr {$x*exp(-$x)}}

puts "Integral: [func integral 1 3]"

Making a new class/type for each function you want to integrate would
be inconvenient.

Regards,

Arjen

Helmut Giese

unread,
Oct 16, 2008, 4:54:40 PM10/16/08
to
Hi Arjen.
I don't know anything about Snit but XOTcl's mixins would come in
handy here: You can "augment" any function of an object by 'mixing in'
additional functionality. And since you can do it on a 'per object
base' a scenario like this is easy:
- object a is dumb: no integration - no mixin
- objects b and c are different but their integration method is the
same - they get mixin1
- object d is different again and has an integration method of its
own: it gets the special mixin2
- etc.

HTH
Helmut Giese

Arjen Markus

unread,
Oct 17, 2008, 5:14:48 AM10/17/08
to
> >Arjen- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

Aha, so that is what a "mixin" is - I knew the term from the
discussion on the various OO systems, but I am fairly unfamiliar
with OO as such.

Well, it is useful to see how XOTcl would deal with this. Thanks
for the tip.

Regards,

Arjen

Neil Madden

unread,
Oct 18, 2008, 7:34:31 AM10/18/08
to
Arjen Markus wrote:
> Hello,
>
> I am experimenting a bit with Snit, to see how it could be used to
> wrap certain numerical/mathematical/...
> functions and provide a more convenient interface.
>
> Two things I am wondering about are:
> - Is it possible to define new methods for a particular Snit object
> (after it has been created)?

You technically can, but it's not something Snit supports well (see below).

> - Is it possible to redefine an existing method?
>
> The reason I ask, is that it would be very convenient to have, say, a
> type that offers
> one or more integration options and then define the function to be
> integrated per object.
> Something like:
>
> snit::type numeric {
> method integral {a b} {
> }
> method f {x} { puts "This is a dummy!" }
> }
>
> numeric create func
> func define f {expr {$x*exp(-$x)}}
>
> puts "Integral: [func integral 1 3]"
>
> Making a new class/type for each function you want to integrate would
> be inconvenient.

You can define procs in the relevant namespace to achieve this. (You can
also use lambdas).

package require Tcl 8.5 ;# required
package require snit 2.2 ;# may work with earlier
# A function environment
snit::type funcenv {
constructor {} {
# Make things nice for expr
namespace eval $selfns\::tcl::mathfunc \
[list namespace path $selfns]
}
# Define a function:
# env define f x y ... = expression
method define {name args} {
set body [list expr [lindex $args end]]
set params [lrange $args 0 end-2]
proc $selfns\::tcl::mathfunc::$name $params $body
return $name
}
method apply {f args} {
$selfns\::tcl::mathfunc::$f {*}$args
}
}

snit::type numeric {
component env
delegate method define to env
delegate method * to env using "%c apply %M"
constructor {} {
install env using funcenv $self.env
}
method interal {a b} { ... }
}
numeric create func
func define f x = {$x*exp(-$x)}
func f 1
func define g x = {$x-f($x)}
func define pi = acos(-1)
...

In many ways this is a nice solution -- the funcenv is a nice general
abstraction for function symbols. On the other hand, the delegation is
rather magical and I suspect the rewrite required involves some
performance impact. I will wikify this shortly.

-- Neil

GN

unread,
Oct 19, 2008, 4:56:38 AM10/19/08
to
On 17 Okt., 11:14, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
> Aha, so that is what a "mixin" is - I knew the term from the
> discussion on the various OO systems, but I am fairly unfamiliar
> with OO as such.
>
> Well, it is useful to see how XOTcl would deal with this. Thanks
> for the tip.

Actually, for the given example, mixins are not needed. In XOTcl
allows to define object specific methods via "proc".

Class numeric


numeric method integral {a b} {
}

numeric create func
func proc f {x} {expr {$x*exp(-$x)}}

Mixins can be defined in XOTcl per-class (for all instances of a
class) or per-object. Mixins are used to add/remove dynamically a set
of methods to classes or objects. It is certainly possible to define
mixins with only one method ("f" in the example). Mixins are in
particular useful when the same set of methods should be modeled
together and added to several e.g. objects without the need to
duplicate code.

G. Neumann, U. Zdun: Enhancing Object-Based System Composition
through Per-Object Mixins, in: Proceedings of Asia-Pacific Software
Engineering Conference (APSEC), Takamatsu, Japan, December, 1999
http://nm.wu-wien.ac.at/research/publications/xotcl-mixin.pdf

G. Neumann, U. Zdun: Implementing Object-Specific Design Patterns
Using Per-Object Mixins, in: Proceedings of NOSA`99, Second Nordic
Workshop on Software Architecture, Ronneby, Sweden, August, 1999 .
http://nm.wu-wien.ac.at/research/publications/xotcl-objpattern.pdf

Concerning your first questions:
- In XOTcl it is possible to define new methods on the object and
class level at any time (i.e. after creation)
- in XOTcl it is possible to redefine existing methods both on the
object or on the class level at any time.

Arjen Markus

unread,
Oct 23, 2008, 5:45:07 AM10/23/08
to
On 19 okt, 10:56, GN <neum...@wu-wien.ac.at> wrote:
> On 17 Okt., 11:14, Arjen Markus <arjen.mar...@wldelft.nl> wrote:
>
> > Aha, so that is what a "mixin" is - I knew the term from the
> > discussion on the various OO systems, but I am fairly unfamiliar
> > with OO as such.
>
> > Well, it is useful to see how XOTcl would deal with this. Thanks
> > for the tip.
>
> Actually, for the given example, mixins are not needed. In XOTcl
> allows to define object specific methods via "proc".
>
>   Class numeric
>   numeric method integral {a b} {
>   }
>
>   numeric create func
>   func proc f {x} {expr {$x*exp(-$x)}}
>
> Mixins can be defined in XOTcl per-class (for all instances of a
> class) or per-object. Mixins are used to add/remove dynamically a set
> of methods to classes or objects. It is certainly possible to define
> mixins with only one method ("f" in the example). Mixins are in
> particular useful when the same set of methods should be modeled
> together and added to several e.g. objects without the need to
> duplicate code.
>
> G. Neumann, U. Zdun:  Enhancing Object-Based System Composition
> through Per-Object Mixins, in: Proceedings of Asia-Pacific Software
> Engineering Conference (APSEC), Takamatsu, Japan, December, 1999http://nm.wu-wien.ac.at/research/publications/xotcl-mixin.pdf

>
> G. Neumann, U. Zdun:  Implementing Object-Specific Design Patterns
> Using Per-Object Mixins, in: Proceedings of NOSA`99, Second Nordic
> Workshop on Software Architecture, Ronneby, Sweden, August, 1999 .http://nm.wu-wien.ac.at/research/publications/xotcl-objpattern.pdf

>
> Concerning your first questions:
> - In XOTcl it is possible to define new methods on the object and
> class level at any time (i.e. after creation)
> - in XOTcl it is possible to redefine existing methods both on the
> object or on the class level at any time.

Just tested your solution in a small and very simple example and it
works nicely.

Thanks (also for the two references)

Regards,

Arjen

0 new messages