[OE] Suggestion for OpenEuphoria 'Next' | Class/Interface/Contract
- Posted by mitgedanken Feb 27, 2021
- 1243 views
Forked from Class/Interface/Contract -- Re: fake class in OE
I hope my english is ok.
--[ <built-in> --[+ About An Interface --- --+] interface Class --[ About A Contract -- a "contract name" is optional, -- it is not required to be the same name as the routine or variable. -- could be used for error reporting. -- If a contract is not satisfied an error will occur (aka ##error:crash()##) -- example: (# is like : for namespace. I choose that to differ from namespaces -- i.e. instead of ::) -- sequence message = {Class#contract[_NAME_], Class#contract[_REQUIREMENTS_]} -- crash_message("Contract %s not satisfied!\nRequirements: %s", message) --] contract create atom class_pointer: -1 <= return >= 0 export procedure create() return class_pointer docline "-1 means error, >= 0 means 'This is my pointer and I love you so much!'" end contract --[ About @ --- @ could be a routine name and will be (auto-) called if its Requirements are not satisfied --- I think for ClassError it must be a procedure. --- And the format (fmt) of ##error:crash_message(sequence fmt)## is optional to use --- example: --- fmt could be by default: sprintf("%s is neither %d nor %d", {...}) --- fmt by user would be used like any ##sprintf## i.e. @ClassError("...", {...}) --- See: [["About A Contract"]] --] contract destroy integer code return >= 1 @ClassError -- should it be @ClassError() ?? or return <= 0 @ClassError --- export function destroy() return code docline "<= 0 means no error and >= 1 means an error" end contract --- More (explicit) contracts if needed ... --[ About 'magic routines' -- It's magic if no function was defined -- Like in other languages (Java, etc.) --] export function equal() return boolean export function hash() return sequence export function serialize() return sequence export procedure deserialize() [...] end interface --] interface Animal: Class -- ": Class" is optional, any class/interface/contract/etc. will be inherited from Class contract name sequence name = "Animal" end contract -- Return name of Animal contract export function get_name(): string return name: sequence end contract --- override in an interface is a 'silent' contract: this must be overridden! override function equal() -- should a type-hint be required ?? override function hash() --- end interface class Cow: Animal sequence name = "Cow" export function get_name(): string return name end function export function equal(@Class seq) return equal(seq#name, name) end function export function hash() return name end function end class class Goat: Cow override sequence name = "Goat" [...] end class
Use (strict)
Goat#get_name()
Use (compat)
class_pointer goat_ = getclass("Goat") -- or get_class("Goat") -- is case-insensitive method_return goat_name = getmethod(goat_, "get_name", {}) -- same here
class_pointer is an atom
method_return is a sequence; {atom class_pointer, sequence method_name, object return_value}
But I prefer
class_pointer goat_ = class:new("Goat") method_return goat_name = class:method(goat_, "get_name", {}) -- or class:call(...) -- or class:func(..) / class:proc(...)
I know this is a big thing to implement.
It could be implemented step by step.
The point is to use interfaces and contracts.
Better documentation to read by an IDE/Editor and any human.
The first step could be to ignore 'interface ... end interface'.
It's than a stub. Used for documentation, only.
It could be implemented step by step.
The point is to use interfaces and contracts.
Better documentation to read by an IDE/Editor and any human.
The first step could be to ignore 'interface ... end interface'.
It's than a stub. Used for documentation, only.
--[ About "doc...end doc" and "docline" --- I think it's good practice to say which comment is more than a hint for the code owner. --]