-
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
Java style field initializers - please drop the C++ initialization vector (or at least don't require it) #125
Comments
This comment was originally written by [email protected] So just to recap. Make it legal to perform: class Foo { It's win-win. The javascript for this is trivial. The VM for this just needs the notion of anonymous pre-constructors. |
This comment was originally written by [email protected] Removed Type-Defect label. |
This comment was originally written by [email protected] Added Triaged label. |
I think you are missing the point here. Unlike Java, we can guarantee, for all fields you initialize via the initializer list, that they will never be seen in uninitialized state. You can redirect constructors easily so you really need not duplicate field initialization. Anyway, we're gathering feedback, so we'll take it under review. |
This comment was originally written by [email protected] I understand what you're trying at accomplish, but I viewed Java as an evolution over C++'s initialization process, and my initial impression was that dart was regressing to C++-isms, which are frustrating, error-prone, ugly, and not necessary.. I hear what you're saying that there might be something I'm missing with respect to redirection. I'll review the spec more closely with respect to constructor redirection, but my first impression was 'how is redirection different than "Foo(x) {this(x,1);} " or "Foo(x) {super(x);}"'. To re-iterate Further, C++ allows stack-like initialization for zero-arg constructors, which I often see leading to 'forgotten' initialization fields: class Foo { In Java, we'd have one of two basic models or While, as I said, I need to read more into what you're attempting with const's and redirects, I'm not currently seeing why this java-style compile-time static-code-analysis can't produce the same outcome as a const C++ structure but with fewer syntactic danger-zones. |
Well, the first example cannot happen, because the initializers cannot be dependent on each other. They cannot see "this". The second concern has more validity to it. If you add a field, you need to update the constructor that does the initialization. If it is a final field, the compiler will force you to do so (and I'd make fields final wherever possible - most fields don't change after initialization). But a non-final one could be overlooked I suppose. I'd assume you add its initialization when you add the field. I'd also try and stick to one generative constructor and have other redirect, but that may not work for everyone all the time. We're reluctant to add all the Javanese baggage of definite assignment and unassignment, but we'll see. |
This comment was originally written by [email protected] One last thing use-case I forget to mention (if you'll indulge me): // C++ Here, in order to have the benefit of a const I have to perform all my computations in a static context that does not have access to temporaries. I believe C++ allows me to make maskM1(mask - 1) (as I listed above), but then you have the fear that somebody re-arranges the variable order one day. It's hard to read, and leads to duplicate computation and bloated syntax. The above with static syntax checking in java is: // Java // compute mask inline this.maskM1 = mask - 1; // ... |
This comment was originally written by [email protected] I think one can reap the benefits of Java's field initializers and also avoid its problems using two restrictions:
The first rule prevents "this" from escaping, and therefore the initialization of the first field cannot accidentally look at the (uninitialized) second field. This is essentially the rule from C++ mentioned in comment #6. The second rule is to avoid Java's static class initialization, in which the order of initialization can be rather unpredictable. This should not add significant complexity to the analysis of final fields. I am not arguing against the initialization vector. I just wish Dart was more liberal with field initializers, as they are a lot more convenient than manual initialization in a constructor. |
Added apr30-triage label. |
Removed apr30-triage label. |
Added triage1 label. |
This comment was originally written by @tomyeh Too bad to know it was marked as WontFix. Dart is a great language and combine a dynamical language and strong typing nicely. However, the initialization (final filed or static files) is really restricted. In some degree, it forces developers not to use final since it is too inconvenient. |
You will note that for static variables, the rules have been changed in a very significant way (lazy initialization). The remaining issue is instance variables, and we have considered a number of options, none of which seems attractive at the moment. |
…rtdoc, ffi, fixnum, glob, http, http_parser, intl, json_rpc_2, lints, logging, matcher, path, protobuf, sse, string_scanner, test, typed_data, usage, webdev Revisions updated by `dart tools/rev_sdk_deps.dart`. args (https://github.com/dart-lang/args/compare/da037ac..ac0e2c8): ac0e2c8 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#229) async (https://github.com/dart-lang/async/compare/c59c7c5..de1ce93): de1ce93 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#227) characters (https://github.com/dart-lang/characters/compare/4ffccb8..fff80aa): fff80aa 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#69) cli_util (https://github.com/dart-lang/cli_util/compare/edcf1c3..5a8e8ee): 5a8e8ee 2023-01-01 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#72) collection (https://github.com/dart-lang/collection/compare/cdb11d4..85e987c): 85e987c 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#262) convert (https://github.com/dart-lang/convert/compare/4feeb10..20d136c): 20d136c 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#73) dartdoc (https://github.com/dart-lang/dartdoc/compare/1f42216..ce25524): ce255246 2023-01-02 dependabot[bot] Bump actions/cache from 3.2.1 to 3.2.2 (#3287) e4948e64 2022-12-28 Sam Rawlins Refactor search into a stateful class (#3285) 83a26b87 2022-12-28 Parker Lougheed Simplify search.html base-href handling (#3259) a622a89a 2022-12-27 dependabot[bot] Bump actions/cache from 3.0.11 to 3.2.1 (#3281) f0c7a56a 2022-12-27 dependabot[bot] Bump ossf/scorecard-action from 2.1.1 to 2.1.2 (#3282) 702a9d13 2022-12-19 dependabot[bot] Bump github/codeql-action from 2.1.36 to 2.1.37 (#3276) 2bbac76b 2022-12-19 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#3277) 195dc3da 2022-12-19 dependabot[bot] Bump ossf/scorecard-action from 2.0.6 to 2.1.1 (#3280) 4a5ed7e6 2022-12-19 Sam Rawlins Remove usage of now-removed-in-3.0 elements (#3279) ffi (https://github.com/dart-lang/ffi/compare/17a8142..2a56c2a): 2a56c2a 2023-01-02 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#172) fixnum (https://github.com/dart-lang/fixnum/compare/e4f5e97..714381c): 714381c 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#102) glob (https://github.com/dart-lang/glob/compare/7f97bf5..7adf833): 7adf833 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#67) http (https://github.com/dart-lang/http/compare/46a7708..38d5dd9): 38d5dd9 2022-12-29 Brian Quinlan Add usage instructions for runWithClient and Flutter (#846) 3fba812 2022-12-29 Nate Bosch Drop avoid_redundant_argument_values (#845) 88f6fc6 2022-12-14 Brian Quinlan Always using `package:http` for APIs and image loading. (#839) http_parser (https://github.com/dart-lang/http_parser/compare/c739675..16a4f34): 16a4f34 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#65) intl (https://github.com/dart-lang/intl/compare/6fb07f2..59e7bff): 59e7bff 2022-12-20 Copybara-Service Merge pull request #263 from sanekyy:update-ruble-sign 50f93df 2022-12-19 Copybara-Service Merge pull request #467 from samlythemanly:patch-1 548ef63 2022-12-19 Aleksandr Yurkovskiy fix duplicate_import 5dc6e8f 2022-12-16 Aleksandr Yurkovskiy Merge remote-tracking branch 'upstream/master' into update-ruble-sign 60e8cfa 2022-12-09 Moritz Merge branch 'master' into patch-1 758d086 2022-04-28 Sam Markoe Updated PEN to use the proper currency code. 83e149f 2021-03-10 Aleksandr Yurkovskiy Merge branch 'master' into update-ruble-sign edbbbce 2020-11-16 Aleksandr Yurkovskiy fix format f25b0a6 2020-11-16 Aleksandr Yurkovskiy add empty line a02de91 2020-11-16 Aleksandr Yurkovskiy fix merge issues 8a1716f 2020-11-16 Aleksandr Yurkovskiy Merge branch 'master' into update-ruble-sign d3c56e0 2020-10-05 Aleksandr Yurkovskiy update changelog ed3b5f0 2020-10-05 Aleksandr Yurkovskiy fix test d4ad715 2020-09-22 Aleksandr Yurkovskiy Merge branch 'master' into update-ruble-sign 077e653 2020-02-27 Aleksandr Yurkovskiy update version b119da3 2020-02-27 Aleksandr Yurkovskiy update ruble sign json_rpc_2 (https://github.com/dart-lang/json_rpc_2/compare/16fed53..bd9f8d9): bd9f8d9 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#87) lints (https://github.com/dart-lang/lints/compare/16bdefe..dfded5e): dfded5e 2023-01-01 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#96) logging (https://github.com/dart-lang/logging/compare/0373ef8..b525d5c): b525d5c 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#125) matcher (https://github.com/dart-lang/matcher/compare/7e6a665..deedda1): deedda1 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#202) path (https://github.com/dart-lang/path/compare/12ce876..1299791): 1299791 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#132) protobuf (https://github.com/dart-lang/protobuf/compare/2706b53..1d1c92a): 1d1c92a 2023-01-02 Kevin Moore Latest github actions (#789) sse (https://github.com/dart-lang/sse/compare/cfa93b1..2de27fe): 2de27fe 2022-12-28 dependabot[bot] Bump nanasess/setup-chromedriver from 1.0.5 to 1.1.0 (#65) string_scanner (https://github.com/dart-lang/string_scanner/compare/4a5cbc5..6ddab2c): 6ddab2c 2022-12-28 Kevin Moore Remove unnecessary parens (#51) 3e451ab 2022-12-28 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#50) test (https://github.com/dart-lang/test/compare/89a8b12..8235a25): 8235a25b 2022-12-29 Nate Bosch Add String.equalsIgnoringWhitespace condition (#1835) 6df19d2e 2022-12-29 Nate Bosch Add String.containsInOrder condition (#1836) b7f4cb54 2022-12-29 Nate Bosch Collapse core checks to fewer extensions (#1831) 101df768 2022-12-29 Nate Bosch Add Function.returnsNormally condition (#1833) f0dfdbfc 2022-12-28 Nate Bosch Tighten types of Extracted constructors (#1829) d1211748 2022-12-27 Nate Bosch Add String.equalsIgnoringCase (#1828) c14260b5 2022-12-22 Nate Bosch Add String.equals condition (#1826) 1785348f 2022-12-21 Nate Bosch Add more numeric matchers (#1823) 66d89348 2022-12-21 Nate Bosch Allow slower node test on windows (#1825) 695894ee 2022-12-21 Nate Bosch Drop dependency overrides from pkg:checks (#1824) 9be6023c 2022-12-20 Nate Bosch Add Iterable.every extension (#1822) ca412bbf 2022-12-20 Nate Bosch Add some tests with full failure messages (#1820) 6650bbb3 2022-12-20 Nate Bosch Add pairwiseCompare condition (#1821) d20eb34d 2022-12-20 Nate Bosch Add FailureDetail to allow expressive failures (#1818) 611faeac 2022-12-20 Nate Bosch Make the prefixFirst utility public (#1819) a0562585 2022-12-19 Jacob MacDonald change the url secrets for browser tests to be alphanumeric characters (#1817) typed_data (https://github.com/dart-lang/typed_data/compare/1e838b8..dbf81a7): dbf81a7 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#57) usage (https://github.com/dart-lang/usage/compare/fee1d9d..2773c7d): 2773c7d 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#186) webdev (https://github.com/dart-lang/webdev/compare/317288a..8795ca5): 8795ca5 2022-12-22 Elliott Brooks (she/her) Tests should serve`main.dart` and `index.html` from the same directory (#1856) b127883 2022-12-22 Elliott Brooks (she/her) Add test timeout of 2 minutes for all tests missing a timeout (#1854) c884705 2022-12-22 Elliott Brooks (she/her) Skip flaky events_test on Windows (#1853) 5aa3195 2022-12-22 Elliott Brooks (she/her) Add tests for interacting with the extension panels added to Chrome Devtools (#1836) 82ac652 2022-12-21 Elliott Brooks (she/her) Shard DWDS tests and fix CI test flakiness (#1851) 8e926c0 2022-12-20 Elliott Brooks (she/her) Fix flaky CI tests (#1848) 80f8671 2022-12-20 Elliott Brooks (she/her) File paths in `TestContext` are relative, not absolute (#1843) 6dedcd5 2022-12-20 Elliott Brooks (she/her) Move the `scopes` package (used in testing) to the test `fixtures` directory (#1837) db5ed71 2022-12-16 Elliott Brooks (she/her) Fix `package_uri_file_mapper_test` on Linux (#1835) 243fe42 2022-12-14 Anna Gringauze Enable weak tests for build daemon and frontend server (#1824) Change-Id: I0ef4ec924d14d88e4831c7bcb025ff19db288479 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278083 Auto-Submit: Devon Carew <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
…_launcher, clock, crypto, csslib, dartdoc, html, http, http_multi_server, intl, package_config, pool, protobuf, pub_semver, source_map_stack_trace, source_maps, source_span, sse, stack_trace, stream_channel, term_glyph, test, test_descriptor, test_process, test_reflective_loader, watcher, web_socket_channel, yaml Revisions updated by `dart tools/rev_sdk_deps.dart`. bazel_worker (https://github.com/dart-lang/bazel_worker/compare/9f21e1d..b35c25e): b35c25e 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#66) benchmark_harness (https://github.com/dart-lang/benchmark_harness/compare/ee7a253..76881df): 76881df 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#80) boolean_selector (https://github.com/dart-lang/boolean_selector/compare/5082b3d..ba7d86b): ba7d86b 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#41) browser_launcher (https://github.com/dart-lang/browser_launcher/compare/2712dda..f2f01e4): f2f01e4 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#37) clock (https://github.com/dart-lang/clock/compare/8a8231f..6b8b7bf): 6b8b7bf 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#41) crypto (https://github.com/dart-lang/crypto/compare/bf0c33b..f854f2f): f854f2f 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#138) csslib (https://github.com/dart-lang/csslib/compare/34203c0..d776535): d776535 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#156) dartdoc (https://github.com/dart-lang/dartdoc/compare/ce25524..9ed196f): 9ed196f1 2023-01-03 Sam Rawlins Move many test files to test_reflective_loader (#3284) html (https://github.com/dart-lang/html/compare/28fb8b9..3dd00b0): 3dd00b0 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#193) http (https://github.com/dart-lang/http/compare/38d5dd9..d434d42): d434d42 2023-01-03 Brian Quinlan Make it possible to use a custom CronetEngine with runWithClient (#843) http_multi_server (https://github.com/dart-lang/http_multi_server/compare/e31c698..beb40a7): beb40a7 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#47) intl (https://github.com/dart-lang/intl/compare/59e7bff..c61fdd1): c61fdd1 2023-01-04 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#528) 2a5e3a2 2023-01-04 Copybara-Service Merge pull request #527 from mateendev3:patch-2 07a5847 2022-12-31 Mateen Mehmood Update date_format.dart package_config (https://github.com/dart-lang/package_config/compare/abb4aec..2e1a8ec): 2e1a8ec 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#128) pool (https://github.com/dart-lang/pool/compare/1ea5b03..713e631): 713e631 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#59) protobuf (https://github.com/dart-lang/protobuf/compare/1d1c92a..dd04535): dd04535 2023-01-04 Mahdi K. Fard Fix avoid_renaming_method_parameters linter warning. (#783) 4b1fc34 2023-01-04 Mahdi K. Fard Removes a non-existing lint rule. (#784) pub_semver (https://github.com/dart-lang/pub_semver/compare/1723111..3946e33): 3946e33 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#76) source_map_stack_trace (https://github.com/dart-lang/source_map_stack_trace/compare/8d8078f..e5f9564): e5f9564 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#31) source_maps (https://github.com/dart-lang/source_maps/compare/b031e2c..d995912): d995912 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#70) source_span (https://github.com/dart-lang/source_span/compare/d1d47e5..72d5c55): 72d5c55 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#89) sse (https://github.com/dart-lang/sse/compare/2de27fe..3c37edb): 3c37edb 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#73) stack_trace (https://github.com/dart-lang/stack_trace/compare/cf3562e..c08ee90): c08ee90 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#125) stream_channel (https://github.com/dart-lang/stream_channel/compare/9143047..0a7800a): 0a7800a 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#83) term_glyph (https://github.com/dart-lang/term_glyph/compare/822cd5b..2bf4594): 2bf4594 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#30) test (https://github.com/dart-lang/test/compare/8235a25..3415089): 34150897 2023-01-03 dependabot[bot] Bump ossf/scorecard-action from 2.0.6 to 2.1.2 (#1838) 5f01dd97 2023-01-03 dependabot[bot] Bump github/codeql-action from 1.0.26 to 2.1.37 (#1839) test_descriptor (https://github.com/dart-lang/test_descriptor/compare/13dbc20..b73c691): b73c691 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#46) test_process (https://github.com/dart-lang/test_process/compare/1774aa7..62ea2ba): 62ea2ba 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#37) test_reflective_loader (https://github.com/dart-lang/test_reflective_loader/compare/52b6753..cf58259): cf58259 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#43) watcher (https://github.com/dart-lang/watcher/compare/3259107..2e0db71): 2e0db71 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#132) web_socket_channel (https://github.com/dart-lang/web_socket_channel/compare/a90e740..ebd0fe9): ebd0fe9 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#244) yaml (https://github.com/dart-lang/yaml/compare/f699275..02be51e): 02be51e 2023-01-03 dependabot[bot] Bump actions/checkout from 3.1.0 to 3.2.0 (#133) Change-Id: I56af76e89a75b0712b290ea154f606781183bec7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/278368 Auto-Submit: Devon Carew <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
Revisions updated by `dart tools/rev_sdk_deps.dart`. ecosystem (https://github.com/dart-lang/ecosystem/compare/e96fbdb..babf5d1): babf5d1 2023-09-13 Devon Carew add additional lints to dart_flutter_team_lints (#167) 7740bef 2023-09-13 Moritz Write comments on forks for `firehose` (#165) http (https://github.com/dart-lang/http/compare/de19214..e19094a): e19094a 2023-09-14 Brian Quinlan Use efficient operations when copying bytes between Dart and Java (#1019) d7e4375 2023-09-13 Parker Lougheed Cleanup `package:http` utils (#1011) eafbbb0 2023-09-12 Brian Quinlan Separate the cronet callbacks from the `send` method (#1017) 2cbb703 2023-09-12 Brian Quinlan Switch `cronet_http` to use jnigen (#1016) native (https://github.com/dart-lang/native/compare/bbcbc1f..7faf62c): 7faf62c 2023-09-14 Gabriel Terwesten Add `includes`, `flags`, `std`, `language`, `cppLinkStdLib` options (#125) shelf (https://github.com/dart-lang/shelf/compare/2926f76..e2a02b7): e2a02b7 2023-09-13 Kevin Moore Move to latest pkg:dart_flutter_team_lints, bump min sdk to Dart 3 (#378) tools (https://github.com/dart-lang/tools/compare/fa01f9b..1512f3d): 1512f3d 2023-09-13 Elias Yishak Add Fake Analytics instance that uses list to save events sent (#149) webdev (https://github.com/dart-lang/webdev/compare/6b21ecf..501ccc2): 501ccc28 2023-09-12 Elliott Brooks Update DCM triggers to match Dart DevTools (#2230) Change-Id: Ic3dc1924da48454a28e4c0d8705244ac1565e74a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/325967 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Devon Carew <[email protected]>
This issue was originally filed by [email protected]
I was very confused to see a hybrid of java, C++ and javascript in the constructors of Dart.. I'm not understanding the particular reasoning.
In C++ we initialize all class-fields in the constructor initialization-vector (illegal to initialize fields directly). Anything not explicitly listed has an implicit ordered item. While this allows static functions to be called that leverage field-order + constructor-parameters as it's only state, this is cludgely and a half. For non-final fields, you can perform the initialization in the constructor-body, BUT, we tend to prefer to use the initialization-vector to avoid the innefficient copy-constructor followed by operator=
class Foo {
Bar bar;
Foo(const Bar& _bar) : bar(_bar) {} // efficient copy-constructor
Foo(bool useBar, const Bar& _bar) { if (useBar) bar = _bar; } // innefficient, default-cons, then operator= called
};
If it wasn't for the possibility to avoid this duplicate initialization, I doubt the constructor-vector would have even been invented in C++.
Java fixes this in 3 ways.
class Foo {
int x = 5; // implicit anonymous pre-constructor
Map items = new HashMap(); // implicit anonymous constructor
// explicit anonymous constructor
{
items.put("one",2);
items.put("two",3);
}
static int y;
// anonymous static constructor (no corresponding named static-constructor)
static {
y = Factorial.compute(System.getProperty("fact"));
}
final int z,w;
{
z = 8;
}
Foo(bool useBool) {
if (!useBool) throw new RuntimeException("");
w = 9; // initializes final field w
}
}
Arguably 3 is really hard to accomplish for a new compiler writer, and requires that ALL code paths be in the same compilation-unit (so it can be responded to with compilation errors).
But honestly, Darts field initialization makes for nasty code and high degree of surprise for both Java and C++ people.. Namely: You CAN inline initialize numbers (like in java). You CAN'T initialize objects (which you can do in Java, and you CAN do in C++ through the use of default constructors). You can't use code-analysis to initialize final fields inside the constructor body (e.g. it's more like C++). You HAVE C++ vector initialization (which freaks out Java people who hate that Stroustrup was even born), but this means you have cludgey syntax when you need non-trivial initialization of final fields. You MUST have a long and complex initialization vector full of 'new's
// Dart
class Foo {
final List list; // can't inline initialize like Java OR C++
// can get akward that MOST initialization code must appear
// in initialization vector. How to validate prior to assignment?
Foo(x,yz,) : list = const[x,y,z] {}
}
I'm not sure if there is some latent intent to make a C / C++ version of the back-end of Dart, and thus C++ is part of the least-command-denom.. If not, this this is just madness. As a VM can trivially handled constants at runtime. Namely leverage anonymous pre and post constructors. And for all final fields, apply a post-constructor which asserts that all final fields have been assigned. In the javascript side, who cares if it was really initialized or not because javascript doesn't even have final fields.
And here's the worst part..
Let's say you have 30 fields that are complex objects (lets just assume they're all lists that need "new List()" calls).
In Java, EACH incrementally newly added field can just be
List myNewField = new List();
And then some method somewhere uses that list.
In C++ it's even easier:
std::vector myNewField; // non-heap default-constructor
But in dart, we have
List myNewField; // invalid null
Foo(): list1 = new List()
, list2 = const[1,2,3]
, myNewField = new List() // ok, I remembered to add it here
{ }
// OPS, forgot to initialize it here.. F* me.
Foo.newfactory(): list = new List(), list2 = const[1,2,3] {}
Further, as you can see, the number of fields times the number of named constructors is the number of annoyance. Yes you can create _init(); that does all this, but I tend to only look at those as a configuration-of-last-resort.. Remember, this was an incremental change/addition.. Perhaps we didn't have an _init() method already.
The text was updated successfully, but these errors were encountered: