Posted to tcl by aspect at Thu Sep 11 00:20:32 GMT 2014view raw

  1. oo::class create AbstractMixin {
  2. superclass oo::class
  3. constructor {abstractmethods} {
  4. oo::define [self] constructor {args} [format {
  5. debug log {Constructing an abstract instance [self] that needs %1$s}
  6. next {*}$args
  7. set mymethods [info class methods [self] -all]
  8. foreach m %1$s {
  9. if {$m ni $mymethods} {
  10. throw {CLASS ABSTRACTMETHOD} "abstract method $m not provided in [self]!"
  11. }
  12. }
  13. } [list $abstractmethods]]
  14. }
  15. }
  16.  
  17. # AbstractBase is a klass which provides "abstractmethod"
  18. klass create AbstractBase {
  19. variable abstractmethods
  20. constructor args {
  21. set abstractmethods {}
  22. next {*}$args
  23. oo::define [self] mixin [AbstractMixin new $abstractmethods]
  24. }
  25. method abstractmethod {name} {
  26. lappend abstractmethods $name
  27. }
  28. }
  29.  
  30.