Comments (8)
This probably will have an effect on how modules are translated
from elixirscript.
Still thinking this through. I haven't come up with a plan yet, but I have thought about some options.
One would be to check for inner modules, translate them into a function and export them with the outer module's export statement. The problem here is when aliasing, etc, it would currently break with the way they are translated into imports.
Another would be to take the module and make it into it's own file. This would remove the issues laid out above. I would require some updates to the translation of modules, but nothing too bad.
I'm not sold on either of those and would love any thoughts, feedback, or ideas.
from elixirscript.
Ok, so I think I've come up with a plan for modules.
Elixir modules don't really match up with ES6 modules as well as one would like. For instance, you could create a module inside of a function if you wanted to. So my proposal for updating modules is to do the following:
- Modules would no longer be ES6 Modules and module creation will be a function closure the returns an object with public functions. All of the object would be in the global scope.
defmodule Hello do
@some_attr "hello"
defp private_function() do
end
def public_function() do
end
end
Would turn into this
const Hello = (function(){
const __MODULE__ = Atom("Hello")
const some_attr = "hello"
function private_function(){
return null;
}
function public_function(){
return null;
}
return { public_function: public_function }
})();
inner modules would work similar:
defmodule Hello do
@some_attr "hello"
defmodule World do
end
defp private_function() do
end
def public_function() do
end
end
Would turn into this
const Hello = (function(){
const __MODULE__ = Atom("Hello")
const some_attr = "hello"
let World = (function(){
const __MODULE__ = Atom("World")
})();
function private_function(){
return null;
}
function public_function(){
return null;
}
return { public_function: public_function, World: World }
})();
An added benefit to this would be that one would no longer have to explicitly use aliases for modules. Aliases would just end up as const declarations to pointing to the specific module.
For using other JS libraries, one could use them globally or a macro would be added specifically for importing js libraries. An added benefit of using the macro is that it helps keep Elixirscript as standard Elixir. The :from
clause currently added to the alias, import, and require isn't standard Elixir so removing that would good.
This may also help down the line with protocols as well. An added benefit is th
from elixirscript.
Option 2 would be making them into classes. I just don't like ES6 classes that much (personal preference).
from elixirscript.
What about dependencies and module loading? What happens with that stuff?
from elixirscript.
They will either be reference by their full names are can be aliased. For example:
defmodule Hello do
alias Another.Module.World
js_import JQuery
def something() do
Some.Other.Module.do_it()
World.do_it()
end
end
would be translated to
const Hello = (function(){
const __MODULE__ = Atom("Hello")
const World = Another.Module.World
import JQuery from 'jquery'
function something(){
Some.Other.Module.do_it();
World.do_it();
}
return { something: something }
})();
In this example, since the modules will be in the same scope, they can be accessed without having to import them in. This works more in line with Elixir. Also, you saw an example of what an alias would do.
Lastly I added an example of how an ES6 module could be added. A macro (in the example js_import
) could be used to import JavaScript ES6 modules. Libraries in the global scope would not have to be imported explicitly either.
The only issue for importing ES6 modules would be that import statements are hoisted to the top. That would mean that a module that imported an ES6 module could have that ES6 module used by another module here. That doesn't cause to much of a concern though..
from elixirscript.
@togakangaroo here is the link the the defmodule documentation for Elixir. Maybe it would give you some ideas as to how I could do this a better way. I like your idea of using something such as System.define, but not sure if it would work out well.
http://elixir-lang.org/docs/stable/elixir/Kernel.html#defmodule/2
from elixirscript.
So I ended up reverting back to making the modules into ES6 modules. I'm pulling out the inner modules and they are being placed into their own files. I'm also automatically creating an import statement for them in the modules containing them. The solution should resolve this issue.
from elixirscript.
Related Issues (20)
- Track the modules a module uses
- Build failure with macOS 10.13 and Xcode 9 HOT 2
- ElixirScript Test Assertions HOT 1
- Create compiler manifest HOT 2
- Create manifests function in Mix compiler
- Create clean function in mix compiler HOT 1
- Use manifest to see if a module should be compiled or not HOT 1
- Regex functions throw errors HOT 3
- Is it possible to compile quoted or string? HOT 3
- Enum.map does not work in 0.32 HOT 2
- comprehensions/generators HOT 1
- in-browser Elixir parser / interpreter HOT 3
- Beginner Tutorial HOT 2
- FFI - Support npm modules directly
- Elixir + Wasm HOT 9
- Pragmatic question: working with JS Object HOT 3
- Elixir Script as ClientLibrary Framework to Interface with Elixir/Phoenix
- Still alive? HOT 2
- String.slice(str, 0, -1) raises FunctionClauseError HOT 1
- Inactive project HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from elixirscript.