1. [OE] Suggestion for OpenEuphoria 'Next' | Module

Forked from Alternative: Module -- Re: fake class in OE

An alternative to "class"

Draft for OpenEuphoria += Next
Version 2022-11-07

An explicit defined module must be instanciated to be accessible.
Not like a file with/without a namespace.

For short

  1. a module is like a namespace and like a routine
    1. but module must be instanciated
    2. and needs a procedure "new"
    3. "Module.routine(..)" and "Module.var" if it's export/public or have a "get_var" or "set_var" (both or one of them)
  2. only anything above "module" will be usable if only namespace is used


Accessing routines and public/exported variables and constants

Internal

  1. routine => self.routinename() ( or routinename() / .routinename() )
  2. var/const => self#varname / self#constname ( or #varname / #constname )
  3. constants alternative => self##constname ( optional )


External

  1. routine => modulename.routinename
  2. 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

-- "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

  1. It is the same class/module.
  2. 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

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


new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu