-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Accepts some statements of C++; typedef, friend, override and final #9465
Comments
Hey @samchon! Glad to see the enthusiasm here. One thing I'll mention is that there are existing issues for some of what you've brought up here:
I think one thing that you've mentioned is that you'd like to be able to have type alias declarations in classes, but from the use-case you're giving, I think you could perform class-namespace merging to achieve the same goal: class Foo {
x: Foo.Bar;
}
namespace Foo {
export type Bar = string;
}
var abc: Foo.Bar You can try it out in our playground. So would it be fair to say the other issues, as well as the suggestion here, cover what you've brought up? |
Wow, most of what I mentioned is almost have argued. Class inline type defitnition, there was an indirect method defining same named namespace. The code seems something weird, but there's a solution. namespace std
{
namespace List
{
export type iterator<T> = std.ListIterator<T>;
}
}
let list: std.List<number>;
let it: std.List.iterator<number> = list.begin();
// IN C++, std::list<number>::iterator it = list.begni(); @DanielRosenwasser Thanks for your replying. |
The type alias definition in classes was initially proposed in #7061. |
looks like all the proposals are already covered in other issues. closing to limit noise. |
Accepts some statements of C++
I have made a STL (Standard Template Library, a standard libraries with containers, functions and algorithms of C++) for TypeScript, named TypeScript-STL. Well, because I've tried to migrate C++ code to TypeScript, I strongly aspire TypeScript to accept some grammers and conceptions of C++, typedef, friend, override and final.
TypeDef
Well, I don't mean to accept typedef statement of C++, literally. TpyeScript already has similar conception with type statement. The type statement can be used in root of document, in a namespace and even in a function body. However, the type statement only can't be used in a class level.
I want the type statement can be used in a class level such below:
type can be declared in a namespace or function.
I want the type can be declared in a class elvel.
Friend
TypeScript is a superset language of JavaScript. Member variables and methods with
private
andprotected
accessors are only effective in TypeScript. When compiled to JavaScript, those accessors are all detached. Thus, TypeScript also can access the private and protected members by dynamic accessment like such below:Those dynamic accessing are not recommended, but one thing clear is, it is possible in TypeScript. As anyone knows, those implementation using string brackets (
obj["member_name"]
) are not safe. Those implementation can't take advantage of type inspection, the best benefit of TypeScript.If TypeScript supports the friend statement, then the dangerous method without type-inspection can be solved. Accessing private and protected members with friend statement in C++ sometimes beging criticized, however, it's sometimes useful especially for implementing libraries. Furthermore, it can solve the dangerous dynamic accessing problem.
Below is the real code what I need the friend statement:
Override
To enhance grammer and stability, I want TypeScript to concept override statement.
Unlike Java, TypeScript doesn't have the conception overriding. TypeScript only can do over-writing. By the over-writing of parent's function, parameters or return type of the function can be changed. It's not a matter if the changing is intended, however, it's by a mistake, then there's no way to identify the mistake until runtime.
To avoid the mistake, I want TypeScript to adapt override statement. If programmer intends to overriding, then adds override statement on the tail of the overriden function. TypeScript compiler inspects and detects mistake the overriden functions by override statement.
Sample code is such below:
Final
Making open source libraries, I sometimes want to make a method unable to override (like non-virtual method in C++). Furthermore, an overriden method, already overriden method needs to prohibit re-overriding from its derived classes (virtual and overriden method, but does not permit re-overriden nomore).
To prohibit overriden by derived classes, I want TypeScript to accept the final statement. Sample codes are below:
An example code with final statement.
Real case needs the final statement.
Those are real codes what I need the final statement.
The text was updated successfully, but these errors were encountered: