phpc.social is one of the many independent Mastodon servers you can use to participate in the fediverse.
A server for PHP programmers & friends. Join us for discussions on the PHP programming language, frameworks, packages, tools, open source, tech, life, and more.

Administered by:

Server stats:

791
active users

Honestly, still confuses me daily.

This snippet compiles but segfaults, but removing the embedded interface on line 8 actually makes compilation fail, as one would expect. What is going on here? Why does *adding* an interface to the struct makes it less safe?

go.dev/play/p/F73QSt9d-m1

go.devGo Playground - The Go Programming Language

@kassner `a.Perform()` is shorthand for `a.Action.Perform()` -- in other words, "Action" is a field in ActionA and it happens to be nil because no value has been assigned to it.

@serickson I understand that from the structs. But what is the rationale to have the same behavior for interfaces, given they can't have implementations themselves?

Rafael Kassner

@serickson I understand that.

My questioning is why my initial code segfaults instead of doing the proper type-check. If interfaces must be implemented, why isn't the compiler validating this?