13

I hate the fact that this is a valid line of Dart code

int val = null;

Comments
  • 2
    Why? null is completely valid when a variable still doesn't have a value. What do you prefer? 0?
  • 3
    @c3r38r170 yes, 0

    or better, treat the variable as not initialized and throw a compile time error if you try to use it before you initialize it

    the problem is that it's an integer, a type that shouldnt really be on the heap unless you actually want it there, and the boxing just makes things unnecessarily slow
  • 5
    @LotsOfCaffeine 0 may mean something.
    Sometimes you need the variable outside certain scope to be able to access it from somewhere else, but it gets its value at runtime. That's the most common use of =null.
  • 1
    Yeah, C++ has that issue too. Now they have nullptr to avoid problems with 0 being confused with NULL in code.
  • 1
    @c3r38r170 "unless you actually want it there, and the boxing just makes things unnecessarily slow"

    that's specifically the case, where you would actually do that. But only when you need it and not all the time and slow down performance
  • 1
    @Demolishun well that's an old C remnant I believe, since NULL is just a synonym for 0.
  • 5
    @c3r38r170 Then it shouldn't be null either but Option<u32> with a value of None
  • 1
    Still a better uninitialized variable handling than in JavaScript...
  • 1
    @Oktokolo I mean undefined is just the weird brother of null

    I assume that Dart went this way because besides compiling it, you can also interpret it and transpile to javascript
  • 0
    All values areto be populated with 0 when defined. Null values are pointers with value 0. There you go.
    An error for trying to dereference null pointers and voila, no values that are secretly a null value.
  • 3
    @p100sch you missed the point, an int shouldn't be a pointer to begin with
  • 1
    @LotsOfCaffeine I agree, I should have added that pointers should be seperat values that need to be deliberately created or dereference instead of implied. If you need a pointer you define the parameter as a pointer.
  • 0
  • 0
    Tri value logic makes sense.

    It's the representation that leaves a lot to be desired I guess...

    Ina programming language I definitely like Optionals more than plain NULL.
  • 0
    I feel you.
Add Comment