Comments (6)
Actually, "forward declaration" might not even be enough, classes might need to become functions again that get hoisted... b/c of the way that SystemJS / System register modules declaration work...
from lively.modules.
Also note that when changing the order of imports in test from a.js, b.js to b.js, a.js there isn't a problem b/c dependent modules get run first, so a.js defines Foo before reaching the definition of Bar.
from lively.modules.
I have played around with various solutions like falling back to normal function declarations for classes but this breaks the expected class behavior / es6 class specification. It looks like this is a real conceptual issue with es6 and others have observed that as well [1] and I was able to reproduce it with a vanilla systemjs / babeljs setup.
As stated in [1], the availability of class definitions depends on the module import order... reloading a cuple of times until it works... it can't get much more ugly than that. Anyway, I will close this issue for now as I'm not able to find a solution that would be compatible with es6 classes :/
from lively.modules.
And down the rabbit hole... This issue kept bothering me b/c it would prohibit pretty basic usage of modules and top-level classes. For an example consider the BoundsMarker
in lively.morphic/markers.js. It is a morph that inherits from the Morph
class, yet the module is itself imported by the morph.js module b/c it defines additions (show function) that should be available for morphs in general. The issue caused BoundsMarker to be incorrectly initialized (it inherited from "undefined" = Object instead of Morph), breaking its behavior.
I can easily imagine lots of similar scenarios in packages that would repeatedly cause very hard to track issues. For that reason I decided to implement around the es6 / System.register specification. When using Lively es6 class definitions that get generated from generic es6 class syntax, we pass in the module context the class is defined in + statically available information about the superclass (it's name / reference). With this information we are able to subscribe to changes in the internal module runtime state. This allows us to update the class definition with new superclasses from other modules when the superclass binding changes. Practically "extending the reach" of module bindings. Thus, the problem mentioned above is solved. Downside: Lot's of machinery is necessary to make this work...
from lively.modules.
It seems that this solution would work for now. However, I think there are two disadvantages. There could be another solution but I don't know yet whether this works.
- Storing additional meta-information to class definitions in Lively could be useful for debugging but if we add too much functionality here, this would not work well with non-Lively class definitions. For example, you could think of using a class definition defined in an external JavaScript file and using it as superclass for a Lively class. For now we can probably rewrite all of these before loading but there could be browser built-in APIs that use classes in the future. Again, storing a reference to the module for debugging is still useful but relying on the fact that all classes are always Lively es6 classes could bite us back in the future.
- The approach only subscribes to changes to top level definitions. I wonder whether this could cause problems if the class definition is somewhat indirectly given. So, in the following example, if you change
a.js
(or load it for the first time), does this also change (or initialize) the superclass ofB
?
// a.js
import {B} from "b.js";
export class A { m() { return new B(); } }
// geta.js
import {A} from "a.js";
export function getA(n) { return A; }
// b.js
import {getA} from "geta.js";
const A = getA();
export class B extends A {}
Idea: This is just an idea and it may not work in practice but I don't yet see why it would be impossible to determine the right loading order just before loading module with classes and importet superclasses. As you pointed out, loading 'b.js' and then 'a.js' works fine. Maybe this is too much work or does not work for other reasons but maybe the "right" order could be determined simply by parsing the module and swapping the order before loading.
from lively.modules.
-
So far this only affects the runtime and we need to instrument that anyway, otherwise a modification of class would always result in a new class instance and not modify the existing one.
-
No, metadata is attached to all classes, even anonymous, so this mechanism should work just fine.
from lively.modules.
Related Issues (20)
- persistent module state HOT 2
- imported modules are not updated in workspace modules HOT 2
- Dealing with new exports of module HOT 1
- Lazy definitions HOT 3
- Including lively modules bundle as test dependency HOT 1
- Update instances when class definition changes after module reload HOT 2
- System notifier for module and package events HOT 1
- how can packages be used? HOT 2
- package.json is not checked for validity HOT 2
- manual import via doit doesn't work for non-packages
- new module creation + import issue HOT 1
- Remove methods from class prototype when changing class definition
- Improving compile performance HOT 3
- update module dependencies in code with side effects HOT 2
- Add a Windows CI integration HOT 1
- constructor without super call not removed
- Module state recorder does not allow to (re)define certain globals
- Transform fails for `export default 3;` HOT 1
- export default async function foo() does work any more in a module HOT 2
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 lively.modules.