Hello guys.
It might be a dumb problem but I can't figure out. I've a structure called a. This structure looks like to be nil but the check a == nil returns always false. It comes weird when I print the variable and it show "nil".
Since I've trust issues, I ran the debugger and it shows that:
It indicates the variable has a type but indicates (nil) so I don't know what to think and how to solve this.
Thanks for your help
#Nil struct isn't report as null in "a == nil"
30 messages · Page 1 of 1 (latest)
Here is the logs:
log.Printf("??? !! %v == %v", a, a == nil)
Gives: ??? !! <nil> == false
a == nil test if the interface is nil
however here your interface is not nil, it is pointing to a nil value
interfaces have two values, a type pointer and a data pointer, the type pointer points to the runtime type *generator.member and the data value point to the data
here your type pointer is not nil, but the data pointer is nil, and a == nil only checks the type pointer
var a any = (*generator.member)(nil)
fmt.Println(a == nil) // false
you are in this situation above
you have a type within that interface so the interface is not nil, but the value attached is nil
== nil on an interface only check the interface not the underlying value
an other way to look at it is that == nil on an interface cannot check for the underlying value, because what if I try to compare something that is not nil ? For example what if I have an uint in my interface, how would == nil check against the uint ?
Great explanations, thanks. What if I want to know if the underlying value is null ? I know what the underlying type is when I want to check the value.
checking for nils can be a bad pattern because it can hide mistakes (silent bugs are the worst), I would not do this
else you can either use type assertions or methods or reflect (neither solution is good, imo methods is the least worst one)
What is the "methods" way to do it ?
I'm seeking for the more optimized way since this part of code is called a lot of times.
you are using any in performance code ?
Yes?
Sorry but why ? You are writing a json parser ?
I mean, performance is not the key but it definitely need to be taken in consideration
A savegame editor for a game. It reads the XML files and generate Go files from it
it's not like one solution is O(n), you are looking at a delta of 2ns at most
your codegen is hot ?
I'm completely open to suggestion. I'm far away to be an expert
so usually when you generate code you run the codegen once, and then you just use the already generated code in the application
Yea tha's right
else you need to include a compiler in the application
This is this function: https://github.com/cruffinoni/rimworld-editor/blob/master/generator/mismatch.go#L28
GitHub
Rimworld savegame editor. Contribute to cruffinoni/rimworld-editor development by creating an account on GitHub.
I would refactor my code to rely on checking nils, if you don't want to do this I would do this:
type I interface{
IsEmpty() bool
}
type A struct{}
func (a *A) IsEmpty() bool {
return a == nil
}
// ...
func checkInterfaceEmpty(i I) bool {
return i == nil || i.IsEmpty()
}
it's not the fastest but should be fine
wops this is this one: https://github.com/cruffinoni/rimworld-editor/blob/master/generator/mismatch.go#L241