Static module detection is unnecessary

One of my original concerns for the “simple modules” proposal for ECMAScript was that we should support a simple, static form of what you might call “module detection.” I’ve since realized that this feature is probably unnecessary.

Since different browsers often provide similar functionality in different ways, programmers use object detection to query the host environment for functionality. This lets them make use of different libraries provided under different names (and with variations in behavior) to implement the same functionality.

Now imagine that we’re in a world with statically-compiled modules, and browsers provide modules with similar functionality but under different names. How can programmers conveniently query for modules and their bindings — without causing a compile-time error by referring to a module or binding that doesn’t exist on a given platform? They can always use the dynamic loading forms to try loading a module from several alternative sources:

try {
    m = loadModule("@mozilla/widget")
} catch (e1) {
    try {
        m = loadModule("@webkit/foo")
    } catch (e2) {
        // etc...

This could of course be made more convenient with some more abstractions (say, a library function that takes an array of module resource locator strings and tries each in turn), but my initial thinking was that we should support a static form of this pattern so that programmers don’t have to resort to dynamic loading when they have a fixed set of alternative sources.

The thing is, object detection is never just a simple matter of looking for the same thing under different names. Different libraries provide similar functionality with wildly different API’s. The “module detection” idea naively assumes that each environment will provide identical modules, just under different names.

In general, I think people will continue to need to use object detection and can create wrapper modules that do the necessary dynamic querying of the environment. Then the clients of these wrapper modules can simply use them statically.

Comments are closed.