Live data from Hacker News

Be careful with Go struct embedding

mattjhall.co.uk

11–20 of 101 posts

Re: Be careful with Go struct embedding

#11
post #2

That’s actually crazy. Why is this even a feature?

At risk of being excessively sassy this looks like a case of wanting the ergonomics of multiple inheritance without fully grappling with the complexities or implications of it.

In most cases people just want any inheritance, this is the backwards way the Golang devs decided to implement it based on their 80s view of programming languages.

Re: Be careful with Go struct embedding

#13
post #2

That’s actually crazy. Why is this even a feature?

At the very least, the Go authors have been convinced this should be a feature since the Plan 9 C dialect[1].

[1] http://doc.cat-v.org/plan_9/4th_edition/papers/comp, look for “anonymous structure or union” and note that a (different) part of that extension has since been standardized.

Re: Be careful with Go struct embedding

#15
post #3
post #2

That’s actually crazy. Why is this even a feature?

Because type Foo struct { sync.Mutex whatever string } var foo Foo foo.Lock() foo.whatever = 42 foo.Unlock() is convenient.

Hmm, never realized the convenience came this way. Seems the compiler could emit a warning if two equal depth names might cause confusion, which could be ignored if acceptable.

Re: Be careful with Go struct embedding

#17
post #12
post #2

That’s actually crazy. Why is this even a feature?

See how it's used in the standard library io types, it makes for quite nice composition: https://go.googlesource.com/go/+/refs/heads/master/src/io/io...

I’m sympathetic to parts of the Go design philosophy, but the only thing that comes to mind looking at this is “damn, that’s some awkward (nominal-looking) syntax for (structural) intersection types”.

(It also feels to me that this sort of anonymous embedding is materially different for interfaces vs structs, though I admit that from a type-theoretic perspective it’s not.)

Re: Be careful with Go struct embedding

#18
post #5
post #2

That’s actually crazy. Why is this even a feature?

IMHO it should be a compiler error. This is just so loose... a wheel fell off.

A wheel is generous. This seems more like inviting the computing equivalent of spilling twenty thousand tons of crude into the sea, which then promptly catch fire.

Re: Be careful with Go struct embedding

#19
post #3
post #2

That’s actually crazy. Why is this even a feature?

Because type Foo struct { sync.Mutex whatever string } var foo Foo foo.Lock() foo.whatever = 42 foo.Unlock() is convenient.

almost always, the recommendation is to not embed your mutex; give it a name.

foo.mu.Lock()

This way you don't expose your primitives, preventing poor usage from causing a deadlock. Generally you don't want the user of your struct to have to know when or when to not lock.

Re: Be careful with Go struct embedding

#20
post #2

That’s actually crazy. Why is this even a feature?

Normally you wouldn’t contrive to use embedded struct fields in this way. And you can’t have the same kind of composition with methods - it’s a compiler error:

https://go.dev/play/p/r04tPta1xZo

So the whole article is basically about using the language in a way you normally would ever do.

Post reply on HN