20 ms·
Understanding null safety in Dart
- mindvirus 6y agoI'm very happy about this - lack of null safety and implicit downcasts are two of the biggest problems with Dart/Flutter, which I otherwise really enjoy working in. I've gotten in the habit of adding "assert(x != null)" in all of my methods.
- cbolton 6y ago> I've gotten in the habit of adding "assert(x != null)" in all of my methods Is this not useless most of the time, since you'll usually get an exception (with stack trace) when the value is used?
- mindvirus 6y agoYeah, but at least this catches it early - i.e. if a branch/if-statement uses the field, it might not always be hit when testing things out. Unit tests _should_ catch these things, but so should the type system, and for my side projects I'm kinda fast and loose with testing for better or for worse.
- hardwaregeek 6y agoNull safety is absolutely essential for me. It's 2020, why are we still having NullPointerExceptions? However I'm curious about the next frontier of safety. We've slowly added a lot of nice features around safe use of values and safe memory. I wonder what's next. Personally I've been thinking about safe use of arrays. Arrays are such an important data structure and yet they're horribly unsafe. Every time I do index math I get that same nervousness that I used to get accessing fields in nullable data structures. I'd love for a way to verify array indices that doesn't involve too much dependent typing.
- bigyikes 6y agoI think twice about writing for loops. Functional style APIs make these kinds of mistakes way less common
- catalogia 6y agoPersonally I'm a fan of list comprehension, particularly as implemented by rackets for/list forms. I certainly don't use them for everything, sometimes a plain old map fits the circumstance better, but I find myself using for/list fairly often. And I don't think it's error prone in the same way that a traditional C-style for loop would be. https://docs.racket-lang.org/reference/for.html?q=for%2Flist#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._for%2Flist%29%29 https://docs.racket-lang.org/reference/for.html?q=for%2Flist... > (for/list ([i '(1 2 3)] [j "abc"] #:when (odd? i) [k #(#t #f)]) (list i j k)) '((1 #\a #t) (1 #\a #f) (3 #\c #t) (3 #\c #f))
- foolfoolz 6y agocollections apis have solved this. i haven’t used an array in many years
- riffraff 6y agoBut some things at the lower level still need indexes, don't they? Think of in place sort routines.
- hardwaregeek 6y agoYeah, maybe it's just my niche but I've used a lot of indices recently in my compiler. I have a bunch of tables for types, names, etc. and indices play a big role with them
- gameswithgo 6y agomap filter fold etc.
- 6y ago
- jitl 6y agoDoes Dart2 offer any compelling features as a language that aren’t also present in Kotlin, Typescript, Swift, or Haxe? I know many users are interested in Flutter - but I’m curious more about the language itself. Whenever I see Flutter examples I’m disappointed I can’t use it with Kotlin (which already has many specialized backends) or some other language with more widespread support outside the Google ecosystem.
- hamstercat 6y agoI come from a TypeScript background, so I can compare it to that. The biggest difference to me was the soundness of the type system, where you're ensured (from a runtime perspective) that objects are correctly typed. This cuts both way: it was great not having to think about typings all the time around input/output, but it was also cumbersome when implementing the internal implementation. I'm a bit biased because I've come to think in term of structural typing much more. Typing in Dart reminded me more of my time with C#. Null safety was a huge missing piece though, glad to see it being added.
- smichel17 6y agoIs there any language that does both? I've really enjoyed js/ts's structural typing for prototyping, but there are certain places I miss nominal typing, particularly with input validation (and other "primitive type, plus restrictions") scenarios. For example, an integer that's used to index into an array, so it must be positive. Or a string that's a valid street address. You can maintain that info by packing it into a unique structure (ie, wrap in an object, using a unique key), but that's awkward to access. Or you can always pass around the parent object (eg, House), which has a unique structure, but then you're introducing unnecessarily tight coupling which is a disaster to maintain.
- emptysea 6y agoIf I understand correctly you want "new types" in typescript, which can be done either via a library or by hand rolling it: - https://github.com/gcanti/newtype-ts https://github.com/gcanti/newtype-ts - https://github.com/Microsoft/TypeScript/issues/4895#issuecomment-401067935 https://github.com/Microsoft/TypeScript/issues/4895#issuecom... but it isn't as nice as say Python's `typing.NewType` or Haskell's `newtype`
- chubot 6y agoI used flow typing in MyPy and it's awesome. TypeScript has the same thing (although I would be curious if their rules are significantly different). I think you can't practically have null safety without flow typing, at least in an imperative language, otherwise you end up with casts everywhere. MyPy has null safety, though it takes some effort to turn it on for most codebases.
- millimeterman 6y agoNot at all - that kind of dataflow analysis is basically just a hack to patch over the fundamentally broken design of having everything be nullable. It's totally unnecessary in a language with proper algebraic data types where there's no all-pervasive concept of "nullability". The only imperative language I know of that has this is Rust, but it originates from ML languages like OCaml and Haskell. It's one of those features where once you get used to it, it's hard to fathom how anyone programs without it. It's such a simple yet expressive feature that's incredibly useful when writing normal, ordinary code. Nothing fancy or theoretical.
- blandflakes 6y agoI'm mostly agreeing with both you and the sibling - flow-sensitive typing is great. I think it's awesome in Kotlin, and removes the Java-style casting or manual unwrapping inside of if guards you always end up with. As sibling said, ML-style languages get away with this a different way - chained "container" operations in Scala, for example, are "right-biased", so the only get called if the result of the preceding operation was a success or not null. I've heard it called "Railway Oriented Programming": https://fsharpforfunandprofit.com/rop/ https://fsharpforfunandprofit.com/rop/
- Abishek_Muthian 6y agoNot a Dart programmer, but looking at it for flutter. I see from Jetbrains Developer Survey[1] that Dart has grown to 9% from 6% last year[2] which I think is because of flutter, which has grown its share to 39% in cross-platform development. But doesn't it seem reasonable to use TypeScript for flutter now? If you are not a Googler, Where do you use Dart (outside flutter) in production for a purpose where it is better than TypeScript? [1]https://www.jetbrains.com/lp/devecosystem-2020/ https://www.jetbrains.com/lp/devecosystem-2020/ [2]https://www.jetbrains.com/lp/devecosystem-2019/ https://www.jetbrains.com/lp/devecosystem-2019/
- latchkey 6y agoI use Dart for my Flutter ios/android app and Typescript for my backend for the app. I don't think I'd even consider reversing that. Why? Libraries.
- sebe 6y ago>But doesn't it seem reasonable to use TypeScript for flutter now? Flutter started as an experiment from chrome team, so their first language was JavaScript, when they move to dart they decided to write the whole framework layer in dart, the engine is in C/C++, so it seems there is no going back to a JS world. https://flutter.dev/docs/resources/technical-overview#layer-cakes-are-delicious https://flutter.dev/docs/resources/technical-overview#layer-... With the Dart, they got a VM, GC and other tools and a language team that could grow Dart to suit flutter. They also got stateful hotreload, a Dart idea, which was something that the Flutter team didn't know they wanted.
- syspec 6y agoI say this as a huge IntelliJ fan, ask anyone who knows me. My friends joke that I must be collecting commission checks, for as much as I nudge people toward IntelliJs awesome IDEs. Take this survey with a grain of salt, because the results come from IntelliJ users, and IntelliJs most important and popular product is IntelliJ IDEA, a Java IDE (and other things) So the results will skew in that direction for that userbase.
- 6y ago
- ElliotH 6y agoI'm really hoping this will make AngularDart a lot nicer to write. Inputs might have a null value and it's very easy to forget to account for it, will be great to catch that at compile time.
- yagodragon 6y agoI believe Dart is a more exciting language than Go. It's the most well designed scripting language out there, that can also be AOT compiled into native binaries. It's a better JS and a better Java at the same time. It would be also a killer language for backends and simple websites. Just imagine a better node.js built around Dart's excellent tooling. What made Go more successful than Dart was a mix of decisions, luck, and momentum. On an alternate reality, Dart would swap places with Go. Dartium VM would have replaced Javascript on the Web and everyone would have been happy. Go would be seen as an obscure language with missing features that doesn't really do anything better than java/C#/C++
- skocznymroczny 6y agoDart is one of my favourite languages right now. I know it gets a lot of criticism for not reinventing the wheel and being heavily OOP based, but it's simple enough, standard library works well, IDEs work great. Also it actually has some unique features which I now miss from other OOP languages, such as named constructors - new Angle.fromRadians(...), new Angle.fromDegrees(...) - feels much cleaner and more self-documenting than the static factory method other languages have you use. Optional typing I never really cared about, because even in languages with var I find myself writing the types whenever possible.
- olodus 6y agoLooks to be very close to the solution Zig also went for. I like the solution and it seems to fit both languages very well. I should look a bit further into Dart some time I think.