Live data from Hacker News

Go Replaces Interface{} with 'Any'

github.com

241–250 of 481 posts

Re: Go Replaces Interface{} with 'Any'

#241
post #214

Earlier quoted context omitted.

I agree. I don't understand why the designers wouldn't want the language to be expressive. Wouldn't it be better to have an expressive language with conventions than one that's as rigid as it is today?

In C++ there could be 7 or 8 different ways go about implementing similar functionality. This is powerful in that it lets you do exactly what you want because each of those ways is subtly different and sometimes you need each one. Go's proponents think it's okay to be a bit less powerful so that there is only 1 maybe 2 ways to do something. This makes code at different companies more similar. It also mean that a new…

Ruby is all about conventions, most Ruby, and by extension Ruby on Rails apps look pretty similar despite the fact that the language is extremely expressive.

I don't buy that a staff SWE and a new grad will write very similar code in Go.

As far as simple language. It is possible to design simple languages that are very expressive, Lisp and Erlang come to mind.

Re: Go Replaces Interface{} with 'Any'

#242
post #62

Earlier quoted context omitted.

It's definitely more streamlined, but my concern would be that newcomers might not understand that it's just an alias. Learning Go really reframed my concept of what an interface is, and I thought that using an empty interface to represent "any type" was kind of ingenious, and helps reinforce its ethos. An empty interface can represent any type because every type inherently implements an interface with no methods. An…

> I thought that using an empty interface to represent "any type" was kind of ingenious It makes me a bit sad to read that. We all are on a journey to be become better developers every day. But things like that are like a distraction. They make you think you found a really cool and smart concept, but you actually didn't and it's essentially just a hack. Not sure what the solution can be. But PL designers should be mo…

Nulls aren't a hack they're the not yet set state.

The problem is that languages don't create both nullable and non nullable types.

Forcing everything to use options which infect everything with guarding at every layer of the stack is just as bad as nullable types.

That is, nullable types aren't the problem, never was, the problem is developers not dereferencing them at the edges of code boundaries and using non nullable types internally.

E.g. in rust, the first point you encounter an option type should be the point you handle the optionality of it and deref into a standard type with the value. From then on, your code is so much freeer.

Re: Go Replaces Interface{} with 'Any'

#243
post #147

Earlier quoted context omitted.

Go exceptions do carry stack trace information. However, the topic is errors, not exceptions. Those are very different concepts. Of what use is stack trace information in debugging values that you have assigned error meaning to when not other types of values? If you had a function func add(a, b int) int { return a * b } there would be no expectation of carrying a stack trace to debug it. So what's different about fun…

Current state of the art: func read_file(filename string) (string, error) { return "", errors.New("oops") } func foo() error { a, err := read_file("a.txt") if err != nil { return errors.New(fmt.Sprintf("read a: %s", err)) } b, err := read_file("b.txt") if err != nil { return errors.New(fmt.Sprintf("read b: %s", err)) } // do stuff with a and b return nil } func main() { err := foo() if err != nil { fmt.Fprintln(os.St…

[deleted]

Re: Go Replaces Interface{} with 'Any'

#244
post #147

Earlier quoted context omitted.

Go exceptions do carry stack trace information. However, the topic is errors, not exceptions. Those are very different concepts. Of what use is stack trace information in debugging values that you have assigned error meaning to when not other types of values? If you had a function func add(a, b int) int { return a * b } there would be no expectation of carrying a stack trace to debug it. So what's different about fun…

Current state of the art: func read_file(filename string) (string, error) { return "", errors.New("oops") } func foo() error { a, err := read_file("a.txt") if err != nil { return errors.New(fmt.Sprintf("read a: %s", err)) } b, err := read_file("b.txt") if err != nil { return errors.New(fmt.Sprintf("read b: %s", err)) } // do stuff with a and b return nil } func main() { err := foo() if err != nil { fmt.Fprintln(os.St…

Your "state of the art" Go code is not really a good example of how that functionality would be written.

If you want to check for a specific error condition, then just define a value for that error and use `errors.Is` to check for it. This works as you'd expect with wrapping: https://go.dev/play/p/rJIlKKSYn9Q

> With the current go error handling, you need to add the informations yourself in the string, not as a real data structure.

This is completely false! If you want to provide a structured error, then you just need to define a type for it. In your example, a Go programmer might use errors.Is(err, fs.ErrNotExist) and errors.As if they wanted to retrieve the specific file path that does not exist in a strongly-typed way, something like https://go.dev/play/p/hdHPLAVbQuW.

> Delegating error handling to a try/catch block with a typed data structure allows the caller to care for certain type of errors and delegate the others to its own caller. With the current error type in Go, what would you do? parse the error message?

Certainly not! I think there is a misconception that "an error is a string" -- in Go, an error is actually any type that satisfies the error interface, i.e. has an `Error() string` method. It can be any type at all, and have as many other methods as you like in order to provide the functionality you need.

> what if the function is defined in a dependency you have no control over?

There's nothing stopping you from writing `throw new Exception(String.format("file not found: %s", filename))` in languages with exceptions either. In both cases, it would be recognized as poor API design.

Regarding stack traces, Go makes a strong distinction between errors (generally a deviation from the happy path) and panics (a true programming error, e.g. nil pointer dereference, where the program must exit). Errors do not provide stack traces since there is no need for them in a flow control context, panics do provide stack traces for useful debugging information.

Re: Go Replaces Interface{} with 'Any'

#245

Earlier quoted context omitted.

Upgrade

> Upgrade Upgrading is the problem in this scenario. Let's say 727 is the modern version. If users upgrade to 728, when 728 is based on 402, we have an issue.

> we have an issue.

No, we don’t.

Re: Go Replaces Interface{} with 'Any'

#246

gofmt -w -r 'interface{} -> any' src Is this a real command? If so, I’m very impressed. Is there any equivalent for c++ and other languages?

'cargo fix' will upgrade your code from one Rust edition to the next as well as apply corrections for lint warnings.

Re: Go Replaces Interface{} with 'Any'

#248
post #227

It's a type alias, introduced for generics. By the way, Go 1.18 Beta 1 is released (with generics): https://groups.google.com/g/golang-announce/c/eAjK4Oezs_A

Disclaimer: I am totally newbie in Go. But quite experienced in Java and Js/Ts. I never jumped the Go bandwagon because of the lack of generics. Can I now try Go? The following article seems to say that the lack of other features can be frustrating (the lack of lambdas and the lack of the functional handling of collections seems problematic to me) Also I wonder whether the language has a IDE support as good as Intell…

Go has lambdas

https://go.dev/ref/spec#Function_literals

Re: Go Replaces Interface{} with 'Any'

#249

Earlier quoted context omitted.

Well there’s a joke somewhere. Less has my favorite version numbering system. Sequential. Latest stable release: Version 598 https://www.greenwoodsoftware.com/less/

My favorite has become just making the version number the release date.

Not the git hashcode?

Re: Go Replaces Interface{} with 'Any'

#250

Loading this commit in safari almost killed my phone

Loaded just fine on my iPhone SE. I hope you’re not using anything older than that…

Limiting a phone's usable life to about five years feels somewhat arbitrary, and there are iPhones older than the (original) SE that are still getting security updates (5S/6).
Post reply on HN