Reputation: 759
I am building an status bar for Linux. I already made a module for when a different keyboard is plugged:
set initialTeck [eval exec cat [glob /sys/bus/usb/devices/*/product]]
set initialTeck [string match *Truly* $initialTeck]
set initialKB [exec xkb-switch]
dict set table samy 0 samy
dict set table samy 1 temy
dict set table temy 0 samy
dict set table temy 1 temy
dict set table gb 0 samy
dict set table gb 1 temy
proc init {} {
variable initialTeck
variable initialKB
set currentTeck [eval exec cat [glob /sys/bus/usb/devices/*/product]]
set currentTeck [string match *Truly* $currentTeck]
set currentKB [exec xkb-switch]
if {$initialTeck != $currentTeck} {
set initialTeck $currentTeck
nextKB $currentKB $initialTeck
}
}
proc nextKB { currentKB teck } {
variable table
exec [dict get $table $currentKB $teck]
}
while 1 {
init
after 1000
}
Temy is an .xkb layout file I made for a the "Truly Ergonomic Keyboard", samy a custom .xkb layout for a standard GB keyboard. Teck stores the state of the USB port and a dict
for the hash table.
Obviously there are many more modules for the status bar, I already use namespaces but as the project grows larger, I decided to go for OOP. At the same time I need to translate the project into Python code to share among developers.
Currently tcl have many extensions for OOP; TclOO, Snit, Stooop, Incr-Tcl, Xotcl and many more. So as Incr-Tcl is similar to C++, is there a Tcl extension which is similar to Python?
Upvotes: 1
Views: 91
Reputation: 137567
None of the Tcl OO systems are all that similar in flavour to Python; the languages are different and favour different ways of thinking about problems. Part of this is syntactic (most Tcl programmers don't like _
characters that much!) but a much larger part of this is that Tcl OO systems don't work in the same way:
The net result is that Tcl's OO systems all make objects behave like Tcl commands (which allows a great deal of flexibility, of course) and not like Tcl values (which tend to be pretty simple transparent numbers, strings, lists and dictionaries). This makes for a very different flavour of OO to that which you get in Python.
You can pretend at the simplest level I suppose, but as soon as you start doing anything complicated you'll hit the differences. To say more than that though, we need to pick a more concrete example for us to dig into.
Here's some Python code:
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
self.description = "This shape has not been described yet"
self.author = "Nobody has claimed to make this shape yet"
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
def describe(self, text):
self.description = text
def authorName(self, text):
self.author = text
def scaleSize(self, scale):
self.x = self.x * scale
self.y = self.y * scale
Here's the equivalent class definition in TclOO (my favourite):
oo::class create Shape {
variable _x _y _description _author
constructor {x y} {
set _x $x
set _y $y
set _description "This shape has not been described yet"
set _author "Nobody has claimed to make this shape yet"
}
method area {} {
return [expr {$_x * $_y}]
}
method perimeter {} {
return [expr {2 * $_x + 2 * $_y}]
}
method describe {text} {
set _description $text
}
method authorName {text} {
set _author $text
}
method scaleSize {scale} {
set _x [expr {$_x * $scale}]
set _y [expr {$_y * $scale}]
}
}
Apart from the obvious syntax differences (e.g., Tcl likes braces and puts expressions inside the expr
command) the main things to notice are that variables should be declared in the class definition, and that there's no need to pass a self
in (it's automatically present as a command, [self]
).
Upvotes: 2