Re: namespace and switch problems
- Posted by jeremy (admin) Dec 28, 2010
- 1068 views
pls cud someone explain namespace and visibilty of public and expprt identifiers with practical examples. i also need someone to explain the switch statement practically.the truth is i have read the manual but as they say an example is better than 10,000 words of explanaton.am still confused despite the examples given there.i almost drove myself to the wire when trying to practise with my own examples just to understand.thanks guys
export limits visibility to the including file only. public allows one to use public include to pass on the public methods to the parent.
support.e
public procedure say_hello() puts(1, "Hello") end procedure export procedure say_goodbye() puts(1, "Goodbye") end procedure
mylib.e
public include support.e -- many other files included there that make up your entire lib say_hello() -- Hello say_goodbye() -- Goodbye
myapp.ex
include mylib.e say_hello() -- Hello say_goodbye() -- ERROR, say_goodbye is not visible. It's for internal use in mylib.e only.
Now, switch. Say you have this:
if color = 1 then puts(1, "Red") elsif color = 2 then puts(1, "Green") else puts(1, "Blue") end if
This would be better written (less code and higher performance) as a switch statement. A switch statement makes the decision once and jumps to the appropriate case statement.
switch color do case 1 then puts(1, "Red") case 2 then puts(1, "Green") case else puts(1, "Blue") end switch
Jeremy