News for hackers. Stuff that matters.
Re:Option and null in dynamic languages by Dewie on Tuesday December 09, 2014 @03:40AM (#8721708)
> This returns the zero value of the value type in the map if the key doesn't exist in the map.

Wow, that's quite horrible (for a static language, anyway). So if my map returns `0`, I can't really be sure whether that is the zero-value (no value at this key), or that the value that I queried actually is `0`.

> This returns true or false in ok based on whether the value exists, zero value for foo if it doesn't exist.

Better, but still unnecessarily error-prone.

> however, go does have the compilation requirement that the ok value gets used somehow, or it'll give you an unused variable compilation error.

OK, I can see how that could alleviate the problem in practice: maybe in most contexts, there aren't anything more sensible to use the `ok` variable for than to check its value. In that case, the programmer will be alerted if he tries to use the `foo` variable without checking `ok` first.

Re:Option and null in dynamic languages by ajuc on Tuesday December 09, 2014 @08:00AM (#8722438)
> Wow, that's quite horrible (for a static language, anyway). So if my map returns `0`, I can't really be sure whether that is the zero-value (no value at this key), or that the value that I queried actually is `0`.

Replace "0" with null, and it's no different from Java maps.

Re:Option and null in dynamic languages by Dewie on Tuesday December 09, 2014 @08:14AM (#8722490)
I think there is a difference, and for the worse: `0` is a perfectly valid integer value which can be used like any other value. `null` is a valid reference value, but is invalid as far as dereferencing goes; refer NullPointerException. `0` has more potential for propagating through the program, and being erroneously used as a valid map value, while it should really be treated like a sentinel value by the program (no-value-in-map). A reference which is assumed to be valid can only propagate through the program to the point where it is dereferenced, in which case the erroneous assumption will be challenged.

It seems like the strong/weak typing distinction: one value can be used freely like any other value, since that is what it is. The other can only be used as a regular value to a degree: it can be passed around, but not actually used.

Re:Option and null in dynamic languages by ajuc on Tuesday December 09, 2014 @08:43AM (#8722614)
BTW, there's one more gotcha with java maps:

    public static void main(String[] args) {
        Map<String, String> testHashMap = new HashMap<String, String>();
        Map<String, String> testTreeMap = new TreeMap<String, String>();
        testHashMap.put(null, "foobar");
        testTreeMap.put(null, "foobar");
        System.out.println(testHashMap.get(null));
        System.out.println(testTreeMap.get(null));
    }
This will throw NullPointerException at the last line :) For hashmaps it works, for treemaps not.

Happened on production in my previous company (we used map2.get(map.get(...)) and used null rturned from the inner map to get the default value from the outer. Then we changed to tree maps to keep order when displaying the maps...

Re:Option and null in dynamic languages by NateDad on Tuesday December 09, 2014 @11:05AM (#8723481)
This is one nice thing about go maps... They won't panic if you try to access a null map, they just return the zero value (and false).

    var m map[int]int = nil
    a, ok := m[1] // returns 0, false
Re:Option and null in dynamic languages by ajuc on Tuesday December 09, 2014 @12:52PM (#8724319)
I don't know go, but this seems very dangerous.

I prefer Python solution - throw Exception, and if programmer want default - there's always defaultdict.

Re:Option and null in dynamic languages by NateDad on Tuesday December 09, 2014 @01:45PM (#8724737)
I don't think it's dangerous at all. What's the difference between an empty, non-nil map and a nil map? Why should their behavior differ? The fact that a map can be nil is effectively like an implementation detail. You asked for the value by a key, and the map tells you that key doesn't exist. Why should a map not having a value in it cause an exception? It's not exactly a rare occurrence. go supports this with the two value return, with the second value telling you if the key existed.
Re:Option and null in dynamic languages by ajuc on Tuesday December 09, 2014 @02:24PM (#8725019)
> Why should a map not having a value in it cause an exception?

    map controlCenters = database.getControlCenters(); // returns map column->value, nil if no connection
    int controlCentersInExistence = controlCenters["count"]; //FIXME - use the other syntax, I forgot how it looked like
    if (controlCentersInExistence == 0) { // someone nuked USA!
        WW3Manager.launchMissiles(); // we need to retaliate!
    }
Re:Option and null in dynamic languages by NateDad on Tuesday December 09, 2014 @04:23PM (#8725862)
See, that's not a problem of the map, that's a problem of poor error handling. If for some reason database.ControlCenters() could ever not return a valid map, it would also return an error value.

    controlCenters, err := database.getControlCenters()
    if err != nil {
        return fmt.Errorf("couldn't get map of control centers: %v", err)
    }
    // now we know controlCenters is valid.
    controlCentersInExistence, ok := controlCenters["count"]
    if !ok {
        return errors.New("no count of control centers!")
    }
    // now we know the column existed
    if controlCentersInExistence == 0 {
        WW3Manager.launchMissiles()
    }
Go's conventions are actually excellent at making this code MUCH more robust.
Re:Option and null in dynamic languages by ajuc on Tuesday December 09, 2014 @05:01PM (#8726132)
Yes obviously it's bad code, but it's bad code that's easier to write, than good code. That's dangerous. That's why Python does it better IMHO.

BTW err != nil? I don't know, it seem fragile. Maybe it's years of exception indoctrination speaking, but I am scared that someone will return false for errors and someone else will check err != nil, or the reverse, or some other misunderstanding, and it will fail.

Re:Option and null in dynamic languages by NateDad on Wednesday December 10, 2014 @03:58PM (#8731645)
False is not a valid value for an error... Welcome to static typing ;). It's either nil or some valid error.
Re:Option and null in dynamic languages by Dewie on Wednesday December 10, 2014 @05:03PM (#8732005)
> False is not a valid value for an error... Welcome to static typing ;).

I'm confused. Previously you gave this example:

    a, ok := m[1] // returns 0, false
Which means that `ok == false`. But how can it be false if that is not a valid value for an error?
Re:Option and null in dynamic languages by NateDad on Friday December 12, 2014 @11:52AM (#8741137)
Sorry to be confusing and/or confused. Map access in Go returns value, boolean where the boolean indicates if the key existed in the map. This is in contrast to most functions which can fail with a generic "error" value. So, for errors, nil means no errors. For map access, false means it didn't exist in the map.

About your original point about someone returning "the wrong thing" in an error... there's really no misunderstanding possible. Because Go is statically typed, the ONLY things you can return for an error are nil (no error), or something that fulfills the error interface (a type with a method Error() that returns a string). You can't return false, or 0, or "" when the code expects you to return an error... the compiler won't let you.

This is how all error handling works in Go. From the most basic program you write, you learn that err == nil means success, anything else means failure.

Does that help any?

Re:Option and null in dynamic languages by anonymoushn on Tuesday December 09, 2014 @10:43PM (#8727563)
This way the program will fail somewhere else long after the actual failure occurred. Great!
Re:Option and null in dynamic languages by Dewie on Tuesday December 09, 2014 @12:50PM (#8724297)
That doesn't seem nice if the map wasn't supposed to be nil (if you assume that failing fast on buggy behaviour is good). But I guess you might want to, on purpose, instantiate and use a map which is nil?
Re:Option and null in dynamic languages by NateDad on Tuesday December 09, 2014 @01:49PM (#8724765)
What's the difference between a nil map and a non-nil but empty map? You asked if the map had a value for your key, and it said no.
Re:Option and null in dynamic languages by Dewie on Tuesday December 09, 2014 @04:35PM (#8725941)
> What's the difference between a nil map and a non-nil but empty map?

A nil map is the absence of a map. An empty map is an empty map. The distinction is clear.