Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules #74

Open
Tracked by #5
liquidev opened this issue Jun 8, 2022 · 0 comments
Open
Tracked by #5

Modules #74

liquidev opened this issue Jun 8, 2022 · 0 comments
Labels
enhancement New feature or request mod: corelib wishlist @liquidev's feature wishlist

Comments

@liquidev
Copy link
Member

liquidev commented Jun 8, 2022

One thing that's nice about Lua is it's very simplistic module system. Basically, each file is treated like a function, and can return any arbitrary value as a result:

-- my_module.lua
local my_module = {}

function my_module.hello()
  print("yo!")
end

return my_module

Then, one can import the module by using the require function, which searches for modules relative to the main module.

-- main.lua
local my_module = require "my_module"
my_module.hello()

However, I see a few problems with this. The most important one is that libraries need to implement weird hacks to get relative requires, which is not ideal. Ruby solves this by having a separate require_relative, but I think it looks quite ugly.

The basics: require in Mica

require would work the same way it does in Lua. The value returned by the module is also returned by require.

# my_module.mi
impl struct MyModule # see #54
  func hello()
    print("yo!")
  end
end

MyModule
# main.mi
MyModule = require("./my_module")
MyModule.hello()

Unlike Lua however, require works more like in Node.js, where paths use / as a path separator, and if a path doesn't start with . or .., a system module is loaded. System modules are provided by the standard library and host application; for instance, NetCanv could register a module netcanv.

netcanv = require("netcanv")  # require a system module
brush = require("netcanv/brush")  # system modules can have submodules
util = require("./util")  # require a local module
@liquidev liquidev added enhancement New feature or request mod: corelib labels Jun 8, 2022
@liquidev liquidev mentioned this issue Jun 8, 2022
11 tasks
@liquidev liquidev moved this to 📦 Backlog in Mica 1.0 Sep 10, 2022
@liquidev liquidev modified the milestone: 0.6.0 Sep 10, 2022
@liquidev liquidev added the wishlist @liquidev's feature wishlist label Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mod: corelib wishlist @liquidev's feature wishlist
Projects
Status: 📦 Backlog
Development

No branches or pull requests

1 participant