Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Sure:

    class Ptr:
        def __init__(self, x):
            self.x = x

    x = Ptr(None)

    if x != None:
        # Go's `%v` print directive unpacks interfaces.
        # We'll use `x.x` to emulate the same here
        print(f"{x.x} was not None")


I don't really see a problem with that code - there is no confusion, `Ptr(None)` is clearly not `None`. Whereas

    var i *int = nil
    var x interface{} = i
    if x != nil {
I assigned `nil` to `x` and yet `x` is not nil. Implicit conversions aren't unusual in programming but in Go they stand out a bit. The closest analog I could find was abusing implicit conversion operator in C#. But even that's pretty explicit and you can tell something odd is happening.

    Thing1 i = null;
    Thing2 x = i;
    if (x != null)
    {
        System.Console.WriteLine("x is not null");
    }

    class Thing1
    {
        public static implicit operator Thing2(Thing1 c) => new();
    }
    class Thing2 {}


Apologies in advance for the terse response—I’m in a hurry and on mobile.

First of all, I’m not asserting that there isn’t a problem with Go. I’m asserting that its interfaces aren’t some three-state beast per the OP.

Secondly, you don’t see the problem with the Python code because you’re not comparing it like for like with Go code. The equivalent Go code would look like this: `var x interface{} = (*int)(nil)` which is at least as conspicuous as the Python example.

> I assigned `nil` to `x` and yet `x` is not nil.

No, you put a nil pointer into an interface, thus the interface is not nil. We have to understand the semantics of the language we’re programming in—it’s not fair to criticize Go because it’s interfaces aren’t my exactly Python variables. I think there’s probably some valid criticism against Go with respect to the error-prone nature of its interfaces, but I don’t think it’s that they lack Python variable semantics.

> Implicit conversions aren't unusual in programming but in Go they stand out a bit.

This isn’t an implicit conversion, we’re explicitly putting an int pointer value (nil in this case) into an explicitly-typed interface{} variable.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: