16
SFML projects / SilverScript: Concept of Modules
« on: October 02, 2013, 03:53:14 pm »
Like in any other programming and scripting language there shall also be the possibility in SilverScript to put the code in several files for better overview and maintenance. So I am thinking about the concept of so called modules.
Let's say, you have got your main script file main.sil, and you define a class for creating a window in window.sil.
window.sil (1):
class window
# attributes and class methods
end
main.sil:
use window # the interpreter looks for window.sil and loads it into the "window" namespace/class
@my_window = new window.window()
Note that you can give modules, you import, any alias you want:
use window as w
@my_window = new w.window()
There are also cases, where you want to load a module directly into the current namespace. This is supported through the keyword import:
import window
@my_window = new window()
If your module only has got a single class, you can also trick there a little bit and treat the module as a class:
window.sil (2):
@window_attribute1 = 0
@window_attribute2 = 0
# ...
function new() # constructor
# initialize a new instance
end
# more class members...
main.sil:
use window
use window as foobar
@first_window = new window()
@second_window = new foobar()
Please tell me your oppinions and suggestions about these plans. Do you have any improvements?
It is not clear yet, where the module files (when using/importing a module the first time) are searched and in which order, but they shall be searched at least in the directory where the requesting script file lies.
Let's say, you have got your main script file main.sil, and you define a class for creating a window in window.sil.
window.sil (1):
class window
# attributes and class methods
end
main.sil:
use window # the interpreter looks for window.sil and loads it into the "window" namespace/class
@my_window = new window.window()
Note that you can give modules, you import, any alias you want:
use window as w
@my_window = new w.window()
There are also cases, where you want to load a module directly into the current namespace. This is supported through the keyword import:
import window
@my_window = new window()
If your module only has got a single class, you can also trick there a little bit and treat the module as a class:
window.sil (2):
@window_attribute1 = 0
@window_attribute2 = 0
# ...
function new() # constructor
# initialize a new instance
end
# more class members...
main.sil:
use window
use window as foobar
@first_window = new window()
@second_window = new foobar()
Please tell me your oppinions and suggestions about these plans. Do you have any improvements?
It is not clear yet, where the module files (when using/importing a module the first time) are searched and in which order, but they shall be searched at least in the directory where the requesting script file lies.