+
+Rosetta Example: Abstract type
+
+
+
+
+For details see http://rosettacode.org/wiki/Abstract_type
+
+
+
package req nx + +nx::Class create AbstractQueue { + + :public method enqueue {item} {error "not implemented"} + :public method dequeue {} {error "not implemented"} + + :public class method create {args} { + error "Cannot instantiate abstract class [self]" + } +}
Create a concrete queue:
+
+
+
nx::Class create ListQueue -superclass AbstractQueue { + :variable list {} + :public method enqueue {item} { + lappend :list $item + } + :public method dequeue {} { + set item [lindex ${:list} 0] + set :list [lrange ${:list} 1 end] + return $item + } +}
+
+
+
+
+Demonstrating the behavior in a shell:
+Trying to create an instance of the AbstraceQueue returns an error message:
+
+
+
% AbstractQueue new +Cannot instantiate abstract class ::AbstractQueue
Create an instance of the concrete queue:
+
+
+
% set q [ListQueue new]
Enqueue and dequeue items
+
+
+
% $q enqueue 100 +100 +% $q enqueue 101 +100 101 +% $q dequeue +100