No registered users in community xowiki
in last 10 minutes
in last 10 minutes
[Xotcl] Thoughts on dealing with class parameters
From: Kristoffer Lawson <setok_at_scred.com>
Date: Thu, 5 Aug 2010 14:48:47 +0300
I resolved my own situation with sending args with "-" at the start by replacing stuff for Class. I then changed that to use a mixin, as per Gustaf's suggestion. I wanted to do this for everything without having to bring in the mixin to every class, or creating a new meta-class, but of course a couple of external libs don't work as they use the dash parameters. This got me thinking. I do see that it is a handy notion to have sometimes, so played around with several ideas on how to do that.
I'm thinking aloud here, so excuse me for rambling.
Anyway my idea is to use a different method, instead of [new], for the cases where pre-configuration is desired. Here are some of the options:
IrcConnection preconf {-nick $IrcNick -channel $IrcChannel} $IrcServer
IrcConnection -new {-nick $IrcNick -channel $IrcChannel} $IrcServer $IrcChannel
IrcConnection -new $IrcServer $IrcChannel {-nick $IrcNick -channel $IrcChannel}
IrcConnection -new $IrcServer $IrcChannel {
-nick $IrcNick
-channel $IrcChannel
}
IrcConnection -new $IrcServer $IrcChannel {
nick $IrcNick
channel $IrcChannel
}
Of these I actually like the last one most. It's almost as if you have a Tcl script passed in. In fact, why not implement exactly that way, but with implicit access to the object's methods in-scope. It also solves the problem of passing values beginning with dash as arguments to the parameters. To use without configuration, just use the normal [new] method, or have an empty configuration script.
One option would also be to have the last option, but with the normal [new] command. It would work so that any mandatory arguments to the constructor are always picked up first (and only the last argument is configuration). Naturally this doesn't make those cases safe where 'args' or optional arguments are used, so would not be 100% safe.
Date: Thu, 5 Aug 2010 14:48:47 +0300
I resolved my own situation with sending args with "-" at the start by replacing stuff for Class. I then changed that to use a mixin, as per Gustaf's suggestion. I wanted to do this for everything without having to bring in the mixin to every class, or creating a new meta-class, but of course a couple of external libs don't work as they use the dash parameters. This got me thinking. I do see that it is a handy notion to have sometimes, so played around with several ideas on how to do that.
I'm thinking aloud here, so excuse me for rambling.
Anyway my idea is to use a different method, instead of [new], for the cases where pre-configuration is desired. Here are some of the options:
IrcConnection preconf {-nick $IrcNick -channel $IrcChannel} $IrcServer
IrcConnection -new {-nick $IrcNick -channel $IrcChannel} $IrcServer $IrcChannel
IrcConnection -new $IrcServer $IrcChannel {-nick $IrcNick -channel $IrcChannel}
IrcConnection -new $IrcServer $IrcChannel {
-nick $IrcNick
-channel $IrcChannel
}
IrcConnection -new $IrcServer $IrcChannel {
nick $IrcNick
channel $IrcChannel
}
Of these I actually like the last one most. It's almost as if you have a Tcl script passed in. In fact, why not implement exactly that way, but with implicit access to the object's methods in-scope. It also solves the problem of passing values beginning with dash as arguments to the parameters. To use without configuration, just use the normal [new] method, or have an empty configuration script.
One option would also be to have the last option, but with the normal [new] command. It would work so that any mandatory arguments to the constructor are always picked up first (and only the last argument is configuration). Naturally this doesn't make those cases safe where 'args' or optional arguments are used, so would not be 100% safe.
-- Kristoffer Lawson, Co-Founder, Scred // http://www.scred.com/