-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Remove the 'new' keyword for invoking constructors #5680
Comments
Another data point to consider: Lasse recently moved parseInt and parseDouble into the int and double classes, respectively. So it's int.parse() and double.parse(). He made them static methods. If we actually like the new keyword, shouldn't those be named constructors? |
I thought hard about int/double.parse, but in the end, a constructor just didn't feel right. Even if we have constructors in other places, we don't have them on int or double, and you don't think of the parse function as creating a new number, but more like finding an already existing number object. Dart is designed to be "familiar", which is one reason for having constructors and the "new" keyword. The only real difference between factory constructors and static methods is that the former can take type parameters and has a fixed return type. Removed Type-Defect label. |
This comment was originally written by sigma...@gmail.com -1 for this proposal, as I did express on the mailing list, too. The expression Alternatively, we could make `.new()' the default generative constructor. That will at least solve Justin's syntax highlighting problem. It is a big change though! |
This comment was originally written by chiahau...@gmail.com I think, avoiding using new to call the constructor will enable people to change between a constructor call and a normal function without changing code. Thus reducing problems with dependencies. But there would still be a problem with named constructors. |
This comment was originally written by sig...@gmail.com chiahau200 : I find this argument ... interesting. How often have you required such a change? How much is the corresponding gain? In the current scenario, one can simply grep the source tree for ` new ' to identify all allocation locations, across all classes. The proposed modification eliminates that simple felicity. |
This comment was originally written by chiaha...@gmail.com to be honest not much. Because we have factories. I believe the gain here is that, we can eliminate or at least lessen the need for factory constructors. Thus making the language simpler. or at least give people an option to choose between using a function or a factory constructor. Its true that you can use "new" to identify where "new" is used and thats about it. It doesn't represent allocation locations as calling new doesn't always create a new instance of a class. You could just be pulling something from cache. |
Thanks for taking a look Gilad. I understand the familiarity argument, but personally it isn't so strong to me without some data to back it up. For usability and adoption, which parts of the language need to be familiar? How familiar? How can we tell? Do Python users count? I do see that trying to explain when type parameters can appear on what looks like a function call could be confusing, however I would argue that that's another good reason for generic functions. |
This comment was originally written by raku...@gmail.com I would like to see 'new' go away, or at least be optional. Annotations are top-level implicitly const but you have to put 'const' on all the subexpressions. class S { @s(const S(const [1])) int foo; // can't say const at the top level and must say const deeper. If const was infectious, I could write: @s(S([1])) int foo; This makes for a much cleaner embedded compile-time annotation language. Now if I can omit 'const', I should be able to omit 'new' too. |
This comment was originally written by lasc...@gmail.com I think google is one of the few companies that has the ability to create innovative things that are accepted by the mere fact of coming from google. I agree that Dart has to be a familiar language but not go so far as to copy Java / Javascript. I do not think removing "new" do less familiar language. I vote for removal. Sorry for my English |
This comment was originally written by @davidB +1 for remove usage of 'new' in caller side. vs it's "familiar" (for java audience) : * I'm using Java since 1996. But I learn Python for a project no 'new' keyword and no duplicate of the name in the constructor was something I quickly appreciate. Idem about 'case class' in scala : it was a selling point for me (quick, readable and DRY way to create structure like).
vs it's an indicator of 'allocation': * wrong, case of factory that can provide a singleton or use cache, pool,...
vs tools take care of rename : * only heavy ide
complementary discussion at To copy the proposal from discussion (I'm not fan of all point, but seems a better way) class Point0 { class Point1 { Sorry for my long comment in French-glish |
This comment was originally written by @davidB I forgot a point (sorry) I'm working on a dart game, where context (level, entities) is defined via "declaration", removing 'new' will be more 'homogeneous' : extract : Entity newChronometer(int millis, timeout) => _newEntity([ Entity newCamera(music) => _newEntity([ Entity newLight(vec3 pos, vec3 lookAt) => _newEntity([ Entity newAmbientLight(color) => _newEntity([ |
This comment was originally written by gp789...@gmail.com I see no benefit in using "new" for constructor calls either. There is no reason to replace static methods, .i.e. int.parse(), by constructors. One can easily discover and detect constructor calls (= class name) without new. Programming in Dart is already so much different from programming Javascript and Java. |
This comment was originally written by kevinc184...@gmail.com The 'familiar' argument is sound. No smalltalk/haskell syntactic innovations. It's a stone in the shoe. Who wants to a new pair of shoes with a 'new' stone glued beneath the sole. Also, I think there's a language where: The sweet spot for syntax is succinct - but non-magical - and reflects the core Dart semantics cleanly. Which will appeal to pioneers/early adopters. And enough familiarity for jobbing programmers to pick it up easily. |
Added WontFix label. |
One thing I really like about Dart is that it does a fantastic job in pleasing both worlds in other cases such as If You like using |
This comment was originally written by @seaneagan You will want to star issue #18241 (Make const/new optional) |
I keep wishing that Dart didn't have new keyword when I'm writing code. Especially when using simple value classes like Uri or Point, 'new' is noise that I just don't want to type or look at, and it may even discourage people from using nicely typed values over strings, lists and maps because those literal values are so much shorter.
Consider calling a function that takes points, with and without 'new':
drawRect(new Point(0, 0), new Point(100, 200));
drawRect([0, 0], [100, 200]);
drawRect(Point(0, 0), Point(100, 200));
(1) is just terribly noisy to me, especially with syntax highlighting. (2) is the cleanest looking, and common in JavaScript, but we should discourage that because having a mixture of Points and lists floating around various APIs would be a messy pain. (3) isn't that bad really, much better than with 'new', in my opinion.
The text was updated successfully, but these errors were encountered: