loading...

Discussion on: Go-tcha: When nil != nil

Collapse
msoedov profile image
Alex Miasoiedov

Wellcome aboard to the land of runtime panics and using reflect package in a statically typed language!!!

This seems more explicit:

func isNilValue(i Error) bool {
    return i == nil || i == Error(nil)
}
Collapse
pauljlucas profile image
Paul J. Lucas Author

The panic I got wasn’t due to using reflection. As to your definition of the function, it’s supposed to be for any interface — it has nothing to do with Error.

Collapse
msoedov profile image
Alex Miasoiedov

why do you need to generalize it to interface{} if you need in to work with the concreteError interface? What is the point of static types if you eventually still using reflect or unsafe?

Thread Thread
pauljlucas profile image
Paul J. Lucas Author

[W]hy do you need to generalize it to interface{} if you need in to work with the concrete Error interface?

Because I prefer general solutions to narrow solutions. Suppose you have not one but N interfaces where N >> 1. Do you really want to write N different functions, one for each interface, that all do the same thing? If so, feel free — in your code-base.

But the actual fix (as I wrote) is not to let the interface become (*MyError,nil) in the first place. I added the Hacks section as a general solution to show it’s possible to check for a nil-valued interface in Go, but I never said it was preferable.

What is the point of static types if you eventually still using reflect or unsafe?

I never said I was using either. Again, the actual fix is as I described. I am not using the functions I described in the Hacks section.

Please read what I actually wrote and do not assume anything I did not write explicitly.

Thread Thread
msoedov profile image
Alex Miasoiedov

(*MyError,nil) Nil as a value in interface if a hack of language design

Do you really want to write N different functions, one for each interface

I don't think that a good idea to have N different nil interfaces in the first place

If so, feel free — in your code-base.

Please read what I actually wrote and do not assume anything I did not write explicitly.

I admit I have not read your post after seeing the code examples because of obvious reasons.

Thread Thread
pauljlucas profile image
Paul J. Lucas Author

(*MyError,nil) Nil as a value in interface if [sic] a hack of language design

Take it up with the Go authors, not me. I only described the way Go is and what the authors’ rationale for it being that way is. As I mentioned, it does actually have a use. But I'm not going to argue either for or against it — that’s outside the scope of my post.

I admit I have not read your post ...

In general, perhaps actually read posts before commenting on them. Specifically for my posts, you must read them before commenting on them; or please don’t comment on them at all. Thanks for your cooperation.

after seeing the code examples because of obvious reasons.

I have no idea what that means.

Thread Thread
msoedov profile image
Alex Miasoiedov

Specifically for my posts, you must read them before commenting on them; or please don’t comment on them at all. Thanks for your cooperation.

Absolutely! That's your right sir. I promise I won't ever either read of comment your valuable content.