Dart

Posts tagged with "Dart."
  • 31

    OCT
    2014

    How to Avoid Taking a Dart to the Knee

    I've been playing with Dart quite a bit lately. I really enjoy the language, but there are always snags that trip up those coming from other backgrounds. Here are the top three issues that have bit me in Dart, in the hopes of saving others some pain:

    The Truth and Nothing But the Truth… Literally!

    One of the challenges of any language is figuring out what it considers to be truthy in conditional expressions. Each system has its twists, but I find Dart to be extra strict in this case.

    Here's some code illustrating the rule:

    bool isTruthy(Object condition) {
      return !!condition;
    }
    
    void main() {
      var tests = [true, false, null, 42, 0, "", [ ], new Object()];
      for (var test in tests) {
        print("$test is ${isTruthy(test)}");
      }
    }
    

    That outputs:

    $ dart truthiness.dart
    true is true
    false is false
    null is false
    42 is false
    0 is false
     is false
    [] is false
    Instance of 'Object' is false
    

    As you can see the literal true (just that one object) is truthy in Dart and everything else is considered false. I'm in the habit of playing pretty fast and loose with truthiness from all of my time working with Ruby, so this has surprised me a few times.

    Read more…