Skip to content

feature request: module, namespace or similar for grouping related classes and functions #881

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

Closed
erisdev opened this issue Nov 28, 2010 · 6 comments
Labels

Comments

@erisdev
Copy link

erisdev commented Nov 28, 2010

Something kind of like Ruby's module or C++'s namespace would be pretty useful for larger codebases. module Foo should be roughly equivalent to Foo ?= { } (so that multiple occurrences don't clobber previous classes and such) and declarations of classes within the module would follow as property assignments in the compiled JavaScript code.

I'm not sure, but it might be advantageous to stick the module in the global namespace too. I suppose that could be left up to the developer with a compiler switch, though.

@TrevorBurnham
Copy link
Collaborator

Could you give an example of exactly what you're proposing, in terms of either JavaScript output or the existing CoffeeScript this new keyword would replace?

@erisdev
Copy link
Author

erisdev commented Nov 29, 2010

Sure thing. I should have done this already, but I was in a bit of a hurry.
This code using the proposed addition:

module Foo

    ARBITRARY = 37

    class Bar
        constructor: () ->

would be approximately equivalent to this code using the current syntax:

Foo = (->
    Foo ?= { } 
    Foo.ARBITRARY = 37
    Foo.Bar = class
      constructor: () ->

    Foo
)()

and would therefore compile to something like this:

Foo = (function() {
    typeof Foo != "undefined" && Foo !== null ? Foo : Foo = {};
    Foo.ARBITRARY = 37
    Foo.Bar = function() {
        function Bar() { }
        return Bar;
    }();
    return Foo;
})()

Hope that's clear. And not too ugly. I'm not sure if maybe the @Property
syntax should be used here instead (@ARBIRARY = 37), but I guess that's up
to whomever implements it.

@michaelficarra
Copy link
Collaborator

For a recent, similar discussion, see #821.

@erisdev
Copy link
Author

erisdev commented Nov 29, 2010

Fair deuce; I'll roll my own for what I need. :)

@TrevorBurnham
Copy link
Collaborator

It's also worth noting that the class keyword will do exactly what you want (thanks to executable class bodies added in 0.9.5):

class Foo

  @ARBITRARY = 37

  class @Bar
      constructor: () ->

# Foo.ARBITRARY is 37 and Foo.Bar is a class.

It's also worth mentioning that module can't be used as a keyword by CoffeeScript because it has a particular meaning in Node.js. namespace would be OK.

Here's a good roll-your-own approach:

namespace = (func) -> func.call(func)

namespace Foo = ->
  @ARBITRARY = 37

Not too bad, right?

@erisdev
Copy link
Author

erisdev commented Nov 29, 2010

Oh wow, I completely missed the addition of executable class bodies in my excitement over default arguments.

As for roll-my-own, here's what I did (targeted at the web, of course):

namespace = (name, body) ->
    ns = window[name] ?= { }
    body.apply ns
    ns

That way my namespaces/modules automatically get put into the global namespace (web browser of course), which is what I want in this case—and, if I declare the same namespace more than once, the new declaration is added to the old one instead of replacing it entirely. Yours looks prettier, though, and I might rework mine.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants