1. [OE] Suggestion for OpenEuphoria 'Next' | Module
- Posted by mitgedanken Feb 25, 2021
- 1013 views
- Last edited Nov 07, 2022
Forked from Alternative: Module -- Re: fake class in OE
An alternative to "class"
Draft for OpenEuphoria += Next
Version 2022-11-07
Version 2022-11-07
An explicit defined module must be instanciated to be accessible.
Not like a file with/without a namespace.
Not like a file with/without a namespace.
For short
- a module is like a namespace and like a routine
- but module must be instanciated
- and needs a procedure "new"
- "Module.routine(..)" and "Module.var" if it's export/public or have a "get_var" or "set_var" (both or one of them)
- only anything above "module" will be usable if only namespace is used
Accessing routines and public/exported variables and constants
Internal
- routine => self.routinename() ( or routinename() / .routinename() )
- var/const => self#varname / self#constname ( or #varname / #constname )
- constants alternative => self##constname ( optional )
External
- routine => modulename.routinename
- var/const => modulename#varname / modulename#constname (or modulename##constname)
namespace module_animal -- still optional -- "export module" is equal to "export function/procedure" export module Animal is Class -- or as above, but I think, that's better then Animal : Class (it seems like a namespace) -- a module does NOT inheritance from Class automaticly! -- Class is more like a struct/type -- anything like above
Use as above ...
but includes ONLY anything above module definition
but includes ONLY anything above module definition
-- "public module" is equal to "public function/procedure" public module Cow is Animal use mymods/Animal.em -- .em Euphoria Module -- a Module could be self executable but I think it must not. -- 'use' could be before 'module' -- […]
The Horns Example (simple)
namespace module_cow public module Cow is Animal use mymods/Animal.em integer horns sequence name -- procedure new() is required for any (instancible) module! export procedure new(sequence name, integer horns = 2) #name = name #horns = horns export procedure export procedure rename(sequence name) #name = name end procedure export function get_horns() return #horns end function export procedure set_horns(integer amount) #horns = amount end procedure
use mymods/Cow.em my_cow = Cow() ? my_cow#name -- "Luisa" ? my_cow#horns -- 2 your_cow = Cow("Priceless", 3) ? your_cow#name -- "Priceless" ? your_cow#horns -- 3 your_cow#horns = 0 your_cow.rename("Princess") ? your_cow#horns -- 0 ? your:cow#name -- "Princess"
my_cow = Cow() your_cow = Cow() ? equal(my_cow, your_cow) -- 0
Comparing modules
Rules
Equality
- It is the same class/module.
- An .equal function is defined and it return 1 (true)
Example 1
instance classA = new ClassA() instance classB = new ClassB()
equal(classA, classB) => 0 / false
equal(classA, classA) => 1 / true
equal(classA, classA) => 1 / true
Example 2
export module ClassC is class integer ii = 1 export new() … end new export equal(instance q, instance p) return equal(q.ii, p.ii) end equal …
export module ClassD is class integer ii = 1 export new() … end new export equal(instance q, instance p) return equal(q.ii, p.ii) end equal …
equal(classC, classD) => true