-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Namespace keyword #2159
Comments
Is this a keyword change only? For example, would clodules still be supported? (clamespaces?) |
It's an additional keyword that does the same thing. To the best of my understanding from current discussions, "clamespaces", "funspaces", and other exciting possibilities of names are just a nice bonus. 😄 |
I like this suggestion. We could make this change more potent by accompanying it with the math.ts namespace math {
export function add(x, y) { ... }
} foo.ts
This shouldn't be a breaking change because the error is only raised when the At the moment we are forced to keep our namespaces relatively flat in order to avoid problems with accessing deeply nested types: var foo = foo.bar.baz.bazooka.add(10,3); |
I strongly support this. The number of times I've had to explain is insane. Thanks! |
👍 |
A crucial expectation for namespaces is to span multiple declarations.
Here both Red and Green are heavily implied to be in the same namespace, as opposed to the second one extending the first. Obviously, in JS/TS namespaces/internal modules actually create closures instead, see arguing about it on issue #447. This closure (and potentially an actual behaviour logic) doesn't quite fit with what 'namespace' implies. 'Module' is actually better.
However 'module' can become confusing after ES6 gains wider adoption. How about 'namespace module' instead?
That way it looks clearer to me. It still is havily hinting towards 'namespace modules' having behaviour, not just scope. But at the same time it's also clear those are some special modules, not your normal ES6 modules. |
@mihailik Not sure exactly what you mean. Multiple namespaces would let you span multiple declarations, just as internal modules do today. The codegen extends the existing module if it's there, rather than create a new closure each time. The linked issue seems to be more about the codegen being suboptimal in some cases. Unlike modules, which are based around a file and aren't extendable, namespaces (nee internal modules) are extendable. The only caveat is that closed-over non-exported symbols can't be seen across each namespace being merged, but I'd argue that's a good thing rather than a bad thing. |
@jbondc external modules are called "Modules" in ECMAScript 6, so it makes sense to stick with calling them "Modules" in TypeScript. |
@jonathandturner the question is not whether it's a good or bad thing -- the question is whether it's a naturally expected thing. I'd say that 'namespace' suggests it's a syntactic construct, whereas 'module' suggests lifetime and behaviour. |
The objections to using |
I support this simply because of the possibility of funspaces. |
Great summary, @jbondc ! To me keeping 'module', but adding a qualifier makes most sense: clearer English, and greater backward and forward compatibility. Few more: scope module Colors {
export var Red = 'red8';
}
closure module Colors {
export var Red = 'red9';
}
extend module Colors {
export var Red = 'red10';
} Beware the familiarity of 'namespace' appealing to C#-aware folks! That's not a wise move to gain a wider foothold in JS universe. |
I'm not quite following the arguments. So we don't want to introduce a new keyword, and we don't want to do C#-aware stuff, but nobody complained about |
It's a fine line with C# and marketing. There's only so much of C# a layman JS developer is ready to take. Say, German cars are fantastic. But if they come with German shops, German pedestrians and German road signs -- I'd have hard time adapting. Not ready to learn another language, me. A bit of C# feels great. But sticking it everywhere, shoving it on the first line of every file -- that could feel just too foreign to a JS person. |
Makes perfect sense when the argument is couched in terms of Germans! 😄 I don't think it's absolutely correct to argue that since modules are emitted as JavaScript closures that one should call them that, because a closure is very much a JavaScript construct (read small code-base), and the code we write is more about managing related components spanning tens of thousands of LOC (for which JavaScript was not originally designed). I would like to see I don't really care much about what the keyword is called (so long as it keeps the Germans happy!) |
"namespace" is the right way to go. It's not just about C#, but JAVA and other languages too using that concept. Javascript is the only thing, I know of, that use module. I noticed people everywhere are moving toward higher level language that write Javascript for us so we don't have to know of more dirty, more lengthy & more complicated scripts. C# use "using", JAVA use "import", other language use "??" etc. that we don't have to write declaration stuff. |
👍 |
OK, but Java does not have a namespace keyword in the first place. The crucial problem with using 'namespace' is that it already HAS a very specific meaning in languages like C# and C++. It specifically is a name space, a space for names. It's a naming/addressing thing -- not a behaviour thing. Internal modules in TypeScript are emphatically not merely a naming tweak. Fundamentally those modules have lifetime and behaviour. Those are not just bolted-on aspects, the lifetime and behaviour of modules need to be actively managed. They cause their share of bugs, sometimes require careful re-ordering or renaming to ensure initialisers are executed correctly. The difference between TS 'internal module' and C# 'namespace' is like a difference between Class and Record in Turbo Pascal, if you know what I mean ;-) It does make sense to have a separate word for 'internal modules', but not necessarily a first borrowed idea from C# is the right one. |
I dont see in my comment that I say JAVA do have "namespace" keyword. Just saying it in layman term for programmers that understand the concept of it. |
If you would like the emit for namespaces to be different then that's just another suggestion. It doesn't really say anything about the need for wanting to have namespaces in TypeScript. |
As long as the project overlords at Microsoft are content with the existing TS adoption, reusing the C# 'namespace' keyword is the reasonable way to go. Conquering the minds of the JS folk is not necessarily the goal, right? ;-) |
I've read this interesting discussion. Both clever arguments on both side. |
It seems many agree that "external/internal modules" is now sub-optimal language and often confusing to explain. I would add that it's also confusing while coding, because the words While I was learning TypeScript modules, I was initially under the impression that I could write an internal module, then simply add a compiler flag to magically produce an external module. It took time to realize things like:
Overall, TypeScript's module terminology meant that, once I was comfortable with internal modules and started to create external modules, I had to "unlearn" assumptions I had made. |
Re: the "namespace and C#" concern, I say this with love: the world does not revolve around C#. |
In ES4, http://www.ecma-international.org/activities/Languages/Language%20overview.pdf
ES4 namespaces always seemed a bit esoteric. I'd say few AS3/Tamarin developers ever made their own namespaces, whereas everyone created packages. Explanations of ES4 namespaces: That being said, ES4's definitions of |
Closed in #2923. |
Now that we have ES6 modules in TypeScript, we should probably move towards a cleaner separation in the module types.
In truth, "internal modules" have been a bit of confusion for developers new to TypeScript. They're much closer to what most people would call a namespace.
Likewise, "external modules" in JS speak really just are modules now.
Let's move to a simpler explanation and have namespaces and modules be more separate. We'd still support the previous syntax, but this would mean introducing a new keyword called 'namespace' that was a bit easier to read and doesn't conflict with ES6 terminology.
Before:
After:
The text was updated successfully, but these errors were encountered: