Abstract type: Difference between revisions
Content added Content deleted
m (→{{header|Go}}: Trim method name by guideline at http://golang.org/doc/effective_go.html#Getters) |
(Added zkl) |
||
Line 1,887: | Line 1,887: | ||
Sub Method_Must_Be_Implemented() |
Sub Method_Must_Be_Implemented() |
||
End Interface</lang> |
End Interface</lang> |
||
=={{header|zkl}}== |
|||
In zkl, nothing is ever abstract, objects are always runnable. However, it is easy to define "meta" objects, objects that define an interface/api for a "class" of objects. For example, it is desirable for "stream" objects (such as File, List, etc) to share semantics so that code doesn't need to know what the source object really is. |
|||
<lang zkl>class Stream{ // Mostly virtural base class |
|||
var [proxy protected] |
|||
isBroken = fcn { _broken.isSet() }, |
|||
isClosed = fcn { return(_closed.isSet() or _broken.isSet()); }; |
|||
fcn init{ |
|||
var [protected] |
|||
_closed = Atomic.Bool(True), |
|||
_broken = Atomic.Bool(False), |
|||
whyBroken = Void; |
|||
} |
|||
fcn clear { _closed.clear(); _broken.clear(); return(self.topdog); } |
|||
fcn open { return(topdog.init(vm.pasteArgs())); } |
|||
fcn toStream { return(self); } |
|||
fcn close { _closed.set(); return(self.topdog); } |
|||
fcn flush { return(self.topdog); } |
|||
fcn read { throw(Exception.TheEnd); } // destructive or advance |
|||
fcn readln { throw(Exception.TheEnd); } |
|||
fcn write(x) { return(self.topdog); } |
|||
fcn writeln(x) { return(self.topdog); } |
|||
fcn walker { return((0).walker(*,wap((self.topdog.read.fpM(""))))); } |
|||
}</lang> |
|||
*The topdog property is the "youngest" child in the inheritance tree (root if you view the tree upside down), it allows a "parent" (or super) to access or pass control to, the actual instance. |
|||
*If you wish to "force" method implementation, you can have a meta method throw an NotImplementedError. This is run time thing, not compile time. |
|||
And now for a "real" object: |
|||
<lang zkl>class DevNull(Stream){ |
|||
var [const] fileName = "DevNull"; // compatibility with File |
|||
fcn init { Stream.init() } |
|||
fcn write(x) { return(0); } |
|||
}</lang> |
|||
{{omit from|Applesoft BASIC}} |
{{omit from|Applesoft BASIC}} |