11 ms·
Dart/Flutter now has macros/metaprogramming
- mikenew 3y agoThis is the actual spec: https://github.com/dart-lang/language/blob/main/working/macros/feature-specification.md https://github.com/dart-lang/language/blob/main/working/macr... Not really sure what the linked repo is about.
- millsteed 3y agoThanks for linking the spec. It has been a living document for a long time. I discovered that the implementation had made it into the SDK's master branch. Since it's very early stage and lacking documentation, I made this repo which demonstrates creating a macro that implements some of the most popular codegen functionality Flutter developers use.
- mikenew 3y agoAh, gotcha, very cool. Sorry I just wasn't sure what I was looking at with the repo.
- chimen 3y ago[flagged]
- millsteed 3y agoYeah, I get that. I think they were burned by having to do https://google.github.io/styleguide/cppguide.html https://google.github.io/styleguide/cppguide.html and https://google.github.io/styleguide/javaguide.html https://google.github.io/styleguide/javaguide.html.
- sakesun 3y agoI myself much prefer well-defined "X spaces" over tab usage, especially when tab mean 8 spaces visually.
- spiderice 3y agoTab means however many spaces visually you want. This is literally the argument for tabs over spaces.
- ivanjermakov 3y agoLooks very similar to the Reflection API in Java.
- cogman10 3y agoIt's more akin to Java's annotation processor. It's generating code at compile time and not runtime. Dart has runtime reflection and annotations that predate this. The benefit of making this compile time are mainly performance.
- millsteed 3y agoDon't discount the developer experience too. With the codegen annotations, you either have to check in your .g.dart files to git which could be stale and break compilation in CI, or have them generate in CI, which is unnecessarily slow and eats up CI minutes. Added bonus is the macro API is less complex than the codegen API.
- mdasen 3y ago> or have them generate in CI, which is unnecessarily slow and eats up CI minutes. Maybe I'm missing something since I'm not that familiar with Dart, but wouldn't Macros use the same amount of time to run and compile? Either you're processing my_thing.dart and writing out my_thing.g.dart and then compiling the whole thing -or- you're processing my_thing.dart and writing out the new stuff to something in memory and then compiling the whole thing. Right? The amount of time it takes to generate the methods should be the same between the two and then the time to compile the same methods with the same code should be the same as well, right? Again, I'm not that familiar with Dart and maybe the source generators have drawbacks that I'm unaware of, but it seems like the macro would be doing the same compile-time work (just without emitting the code to a file). I'd love to know what it's doing differently if that isn't how it's working.
- millsteed 3y agoYeah, it is. It inspired the reflection in Dart, which inspired the macros. You can only use Dart's reflection when running in VM mode though. Flutter apps use AOT compilation, due to tree-shaking, so you lose the reflection and fall back to having to run codegen - kind of like TypeScript -> JS. This is a nice improvement that is run in memory before compilation and allows better access to the AST.
- chipweinberger 3y agoto anyone that has not used it, dart is an excellent language * instant compilation * amazing tooling * null safety * typed I could go on.
- packetlost 3y agoI'm not really sure where it slots in in a world with a lot of programming language choices. Why would I use it over something like Rust, Go, or even JavaScript?
- jgalt212 3y agoCome for the Flutter, stay for the extras.
- millsteed 3y agoExactly. I use it for a full stack mobile app development. Backend and frontend can share code. Server can be compiled to a single machine code binary.
- g00gler 3y agoThat’s really interesting and compelling. What’s the rest of your backend stack look like? Do you get good performance from the database drivers written in dart?
- millsteed 3y agoI mainly just trust Bob Nystrom (https://news.ycombinator.com/user?id=munificent https://news.ycombinator.com/user?id=munificent) to keep this thing on the rails. He's an excellent language designer.
- paulddraper 3y ago> Rust Dart has a GC. > Go Dart has exceptions. > JavaScript Dart is statically typed. --- Plus....Dart has a first-class mobile story, unlike Rust + Go. --- Hope that helps!
- armchairhacker 3y agoI hope Dart gets more support, I really want alternatives to TypeScript for developing webapps which aren't super "bulky" (slow startup, buggy and hard to debug) and support mobile apps. Dart seems to be the best at both although I haven't used it much. It also seems reasonable (one of the reasons I don't like TypeScript is because it still has some quirks from JavaScript like `===`, also because the types are occasionally wrong even in strict mode).
- paulddraper 3y ago> webapps which aren't super "bulky" (slow startup, buggy and hard to debug) All good things. Hot take to think Dart is superior in that respect.
- vvern 3y agoAs hot of a take is that macros and meta programming are going to make startup and code bloat better
- andrewflnr 3y agoThat's clearly not what armchairhacker was claiming.
- mardifoufs 3y agoIsn't flutter the anti thesis of that? Making performant JavaScript apps is much much easier than making a performant flutter or especially flutter web app
- themerone 3y agoYou can use dart without flutter.
- mardifoufs 3y agoAbsolutely, but for webapps, is there any other options using dart? For the record, dart is one of my favorite languages. I'm just a bit sad that it is more or less tied to flutter (not by design, just because it's the main usage it gets)
- pier25 3y agoDart is great. Shame the ecosystem outside of Flutter is so poor.
- markdog12 3y agoExamples right in language repo: https://github.com/dart-lang/language/tree/main/working/macros/example https://github.com/dart-lang/language/tree/main/working/macr...
- markdog12 3y agoIt also may be getting shared multithreading and related features: https://github.com/dart-lang/language/blob/15ab39175ee00fe884e26886c7dd112055f0a0a0/working/333%20-%20shared%20memory%20multithreading/proposal.md https://github.com/dart-lang/language/blob/15ab39175ee00fe88...
- dr_kretyn 3y agoThis should be "Show HN".
- deleted 3y ago[deleted]
- fngjdflmdflg 3y agoOP, can you clarify what you mean by the tittle? You have been able to enable Macros as an experiment for a while now, right? And Macros still haven't achieved GA, so I'm not sure what has changed. Is there some point I'm missing here?
- markdog12 3y agoNot OP, but you're right, it's still an experiment.
- cageface 3y agoAfter years of working in Typescript I've been doing a Dart project for the last 6 months or so. Overall it's a great language but one of the clunky aspects is the use of code generation for so many things. If this can reduce or even eliminate that it will be a big step forward for devX.
- paulddraper 3y agoThat's a Google-ism for sure. (Go, Dart/Flutter, Angular, Bazel)
- capital_guy 3y agoWhat kind of idiomatic code generation is there in Go?
- mdasen 3y agoI'm curious what aspect of the code generation you find clunky. When I used Dart, my issue with the code generation was that it didn't "just happen". With C#, anything that's being generated just happens without any involvement from me and it happens basically instantly. With Dart, I was left running their build_runner manually. Plus common things like json_serializable then require you to add methods to wire up the generated methods (partly because you can't have a factory constructors outside of the class definition, but I don't know why they don't just wire up toJson() as an extension method). I guess I'm trying to figure out if you just dislike having generated files (where you can see the code) or if you dislike how Dart does it (where it's a bit of a clunky process).
- 38 3y agoisn't this bad? every single language I have ever seen that added macros (including Rust) has had problems with macros. unless you are VERY careful, the macro language ends up being turing complete, and/or compile times explode. also now you have two programming languages, and macro heavy code tends to be unreadable, which is fine for the API users, but makes maintenance a nightmare.
- tmdh 3y ago> A macro declaration is a user-defined Dart class that implements one or more new built-in macro interfaces. Macros in Dart are written in normal imperative Dart code. There is not a separate "macro language".
- eviks 3y agoHave you seen Zig's comptime macros? On the surface, that seems like a great approach: no second language and readability fails
- bernaferrari 3y agovery clickbait post, it is in alpha and will take many many months to stabilize.
- snarfy 3y agoFlutter is like Qt where they implement their own windows themselves. But instead of C++ and OpenGL, they use ... Dart and CSS? All the ease-of-use and ergonomics of C++, and the raw bare metal performance of Python. /s I try to give it the benefit of the doubt, so I found a demo site. Here is the first example I opened: https://flutter.github.io/samples/web/material_3_demo/ https://flutter.github.io/samples/web/material_3_demo/ I click play on the progress indicator. My CPU usage goes up 5%. This is on a Ryzen 9 7950X3D. Compare to Unreal Tournament 2004 at max settings running at 240fps, which uses 6% cpu. I just can't take Flutter seriously. I don't care how good the developer experience might be. It's absolutely terrible for the user.
- esrauch 3y agoIt can use css on the web but in general it uses skia directly which is C++ and OpenGL (or Metal or whatever).
- themerone 3y agoIt's not fair to compare a web app to a native app. A flutter desktop app will perform a lot better.
- LVB 3y agoI started some cross-platform work about 6 ago and came into Flutter with plenty of hesitation. I was fearing this much ranted about janky feeling, but so far it has not come. AFAICT from the forums and such, the Web target is definitely far, far behind the mobile targets. This isn’t really a concern for me. For the app I’m building for iOS and Android, the result has been performant, and I’ve really liked the language and framework.
- mightyham 3y agoI really love the engineering that has been put into dart/flutter and the innovation it has brought to native UI development, but I just can't justify using it for anything. The ecosystem is immature and anything I would use it for could just as easily be written in Kotlin or JavaScript (TS).
- lxgr 3y agoTo be honest, I'm glad. No development environment can be as great as to justify the horrible, juddery UX I experience as a user in the few apps that I know use it (and probably a few more where I only have a strong suspicion).
- ken47 3y agoThe Flutter team say they’ve addressed this with a release that updated the rendering engine.
- lxgr 3y agoInteresting, when? If that's really the case, I hope to be able to see the effects of that soon in Google Pay on iOS, which is even being touted as a showcase application by Flutter/Google: https://flutter.dev/showcase/google-pay https://flutter.dev/showcase/google-pay
- ken47 3y agohttps://docs.flutter.dev/perf/impeller https://docs.flutter.dev/perf/impeller
- _yy01 3y ago> The ecosystem is immature There are usually good libraries for most things today. And with Android / Jetpack ecosystem moving so fast and deprecating things, Flutter is something to consider even if you target just Android. My biggest two concerns were state management and native interop. State management is probably biggest concern - there are at least 5 approaches / libraries competing for mind-share - and none of them are particularly ergonomic. I did get an opportunity to work for a while on native interop for android / java [1]. But I haven't been able to work on that since long time. [1] - https://mahesh-hegde.github.io/posts/building_jnigen/ https://mahesh-hegde.github.io/posts/building_jnigen/
- ken47 3y agoI see a couple of comments regarding frame rates on load. The Flutter team claims they addressed that with their new rendering engine. Has anyone found this not to be the case?
- land_ho 3y agoI'm building a medium sized app in Flutter right now. Occasionally I notice animation lag in development, but in production it is really smooth.
- teho 3y agoI'm working on an app that is running on the latest version of Flutter with Impeller enabled. The performance seems good as long as you do not embed platform views inside a list. However, when you drop even a single platform view (web view, video player, google ads...), the performance drops to unacceptable levels even on the latest iOS devices. This is related to another issue with the Flutter ecosystem, it has close to zero support from third parties and therefore you end up having a lot of these platform views. Even things like Google Ads have no native Flutter support and therefore adding a single ad on a list ruins the app. Reading Flutter issues about the performance related issues is sad as the same issues have been there since the beginning and there is no end in sight. However, if your app can do without any platform views, the performance seems pretty good.
- satvikpendem 3y agoYou might be interested in Compose Multiplatform, it's from JetBrains and is a combination of Jetpack Compose and Kotlin Multiplatform. You can transparently use both platform views and CM views together in a way that I haven't seen in Flutter. Here is a video about this where the JetBrains employees go over a demo of building a cross platform app in 100% Kotlin, just as would be the case with Flutter [0]. Check out the timestamp 41:37 where they talk about the above interoperability. They have an example of a messenger app where the messages are in CM while the text input box is in Swift, so that this ensures that you are not reimplementing the native text editing controls as Flutter does, so I see that as a clear advantage for specific things like that. Also see timestamp 51:23 where they talk about graceful decomposition where if you decide to remove CM, you are left with a regular Android app as CM is backwards compatible with Jetpack Compose. I use Flutter quite a bit but it seems like Compose Multiplatform might be the future, since it has a number of clear advantages due to how the architecture of CM is set up. [0] https://www.youtube.com/live/IGuVIRZzVTk https://www.youtube.com/live/IGuVIRZzVTk
- holografix 3y agoDart is to Flutter what Ruby is to Rails. Less actually. Is Flutter still just rendering everything to a <canvas> as an image? Why not a “Plutter” framework using Python instead of Dart and outputting a WASM app that uses Flutter for all the layout?
- Alifatisk 3y ago> Is Flutter still just rendering everything to a <canvas> as an image? No, it can spit out html instead.
- Alifatisk 3y agoWould it be wiser if the Dart team and the Flutter team split up and let the Dart language grow outside the Flutter ecosystem? Because the only use case I see with Dart is UI development, nothing else.
- pjmlp 3y agoFlutter is what rescued Dart from certain death, after Chrome stop caring and AdWords took over from what was left in Dart 1.0, as most well know language designers on the team like Gilad Bracha and Kasper Lund left Google.
- mdhb 3y agoThat information is about a decade out of date. In practice a bunch of new stuff is using it from the as yet unreleased Fuchsia OS and major products are being rewritten to take advantage of it (Google Earth comes to mind).
- pjmlp 3y agoFuchsia OS is going the way of the dodo, unfortunately. Which has been released onto smart displays, which you apparently missed. Since when has Google Earth lost its C++/WebAssembly/WebGL roots?
- mdhb 3y agoI’m not sure why you’re pretending to have some kind of secret insight on Fuchsia but you’re talking out of your ass here. And I don’t know what you think Flutter is built with but it’s C++ / WebAssembly and WebGL. https://x.com/FlutterDev/status/1708908434068779376 https://x.com/FlutterDev/status/1708908434068779376
- pjmlp 3y agoIt's public knowledge, https://news.ycombinator.com/item?id=39005470 https://news.ycombinator.com/item?id=39005470 https://news.ycombinator.com/item?id=36871673 https://news.ycombinator.com/item?id=36871673 https://news.ycombinator.com/item?id=34515277 https://news.ycombinator.com/item?id=34515277 You didn't even knew about its use in Nest Hub Smart Displays. I know how Flutter is implemented, thank you very much. Most likely is a very thin layer over existing Google Earth C++, unless you have some proof the C++ code was fully rewritten into Dart, beyond a simple tweet.