Menu

Concept of modules

Each class and each function or method have its own namespace, so general functions defined somewhere "outside" aren't easily accessible, you have to move up through the self keyword (for example: self.self.self.math.sin) or it must be available through the global namespace (global.math.sin), which doesn't look much better, either. So I added a concept of modules to SilverScript. Modules are just classes in the global namespace, but usually you declare that you are using a certain module in the beginning of a function. I added the keywords use, as and module to the language.
I also added the @-operator to SilverScript. If you prepend it to an identifier, the interpreter searches the namespaces up to the first set variable of the given name. It's useful if you want to create a class instance from another class.

Examples:

use math # the same as: math = global.math
foobar = math.sin(4)

use math as m # the same as: m = global.math
foobar = m.sin(4)

####################

class shot
  ...
end

class player
  x = 300
  y = 50

  function shoot
    @shot.new(self.x, self.y) # instead of: self.self.shot.new(...)
  end
end

I'm currently working on a complete, formal specification (PDF document) of SilverScript.

Posted by Tenry 2012-12-16 Labels: module

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.