Live data from Hacker News

Be careful with Go struct embedding

mattjhall.co.uk

31–40 of 101 posts

Re: Be careful with Go struct embedding

#31
post #8
post #3

Earlier quoted context omitted.

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

It's dangerous. This is awful. Any coding construct that can cause defects is an antipattern. Your language should discourage defects by design. Especially if the faults crop up at runtime. This struct field dereferencing is like NULLs and "goto". Language design that is anti-defect yet ergonomic include the modern Option and Result as seen in languages such as Swift and Rust, with first class destructuring that does…

I mostly agree.

> Language design that is anti-defect yet ergonomic include the modern Option and Result as seen in languages such as Swift and Rust, with first class destructuring that doesn't make it painful to use.

Funny enough, this is only 'modern' in imperative languages. It's been a staple in the ML family since approximately forever. (But hey, I do appreciate progress when we get it!)

Re: Be careful with Go struct embedding

#32

I like the Go language because it's straightforward and clear, even if it looks a bit plain. I hope the feature mentioned in the article will cause a compiler error. However, I wouldn't use this approach when writing my own code.

> I hope the feature mentioned in the article will cause a compiler error.

Read the article. It won't.

At best you can perhaps find a linter that'll report it?

> However, I wouldn't use this approach when writing my own code.

You might use it by accident.

Re: Be careful with Go struct embedding

#34
My personal conspiracy is that Golang is an epic prank.

Make a language that's really good in some ways and just horrible in other ways for no reason whatsoever.

So that when it's critics point out contradictory features like embedding, it's defenders can be the ultimate troll and say things like "but, actually, it's a simple language because it doesn't have while loops".

It's the best explanation I have for some of the cognitive dissonance surrounding the language design.

Re: Be careful with Go struct embedding

#35

Over the course of ~10 years of writing Go, my ratio of "embedding a struct" to "regretting embedding a struct" is nearly 1:1. I do not embed structs anymore. It is almost always a mistake. I would confidently place it in the "you should be required to import 'unsafe' to use this feature" bin.

Using struct embedding for pure data to implement discriminated unions is fine, better than MarshalJSON() that is lost on a type definition. Using it to save typing, or going crazy with it (I consider embedding two things going crazy) is bad.

Re: Be careful with Go struct embedding

#37
post #34

My personal conspiracy is that Golang is an epic prank. Make a language that's really good in some ways and just horrible in other ways for no reason whatsoever. So that when it's critics point out contradictory features like embedding, it's defenders can be the ultimate troll and say things like "but, actually, it's a simple language because it doesn't have while loops". It's the best explanation I have for some of…

As with most small-team languages, it was built mostly to solve the problems that its initial author had in front of them.

Re: Be careful with Go struct embedding

#39
It's my common code review comment to the beginners to not embed structs. There's rarely anything to be gained by doing so. The only use case I found to be useful is to embed something like:

    type MockHandlers struct {
        UnimplementedHandlers
    }

    func (m MockServer) LoginHandler{ /* ... */ }
where I get to override only a part of a bigger interface at a time and have the embedding take care of the rest by saying panic("unimplemented") to satisfy the interface.

Re: Be careful with Go struct embedding

#40
Maybe I see it differently, but it made sense: embeding works only at 0 depth, it's like a macro to access rapidly the fields of the embedded struct, it doesn't go beyond that,there is no inheritance.

When embedding BarService, the field being embedded is BarConnectionOptions

Post reply on HN