Hacker News new | ask | show | jobs
by pkolaczk 1089 days ago
> Please don't respond by saying "this API was badly designed in the first place!" Most languages don't give you the opportunity to design APIs badly in this particular way. If all APIs were perfectly designed on day one then of course we'd never have to worry about API changes.

Ok, I agree. But this is just as well a problem for any language that specifies API in some way. So it is really a decades old debate about static typing disallowing to change things easily. I mean, there might always be certain case when the library author has to change the signature because they made it too restrictive. It just happens that Rust allows you to constrain things that might not be constrainable in other languages, so I guess the chances for that happening accidentally are somewhat higher. But generally I like going from more concrete / restricted to more generic when needed rather than the other way round.

> Off topic, but you could do this in both Java and Go via generics.

I don't think so you can achieve same level of flexibility and efficiency at the same time.

Go doesn't have function level generics, so you'd need interfaces and runtime polymorphism as usual (and runtime penalty).

And in Java you cannot add a new interface implementation to a builtin type like String, so the idea of switching from String to any other type won't work. There is much more upfront ceremony needed to add such flexibility (defining custom interface + implementations and using them from the beginning).

1 comments

To my mind, the issue is that a substantial portion of lifetime constraints reflect implementation details and not actual domain-level constraints that the programmer specifically wished to enforce. The same thing can of course happen with static type constraints generally, but I think not usually to the same extent.

>Go doesn't have function level generics

Not sure what you mean by this. Go functions can take generic parameters that satisfy a given interface (at compile time). E.g.

    func firstName[T AsBytes](name T) T
(You'd have to define the AsBytes interface yourself.)

>in Java you cannot add a new interface implementation to a builtin type

Yes ok, fair point.

         func firstName[T AsBytes](name T) T
Ok, I stand corrected. Can you define AsBytes for string then?
> Can you define AsBytes for string then?

Yes. However, because strings in Go are immutable and byte arrays are necessarily mutable, you can’t safely convert strings to byte arrays (or vice versa) without copying, so it would not necessarily be a particularly useful interface.

By the way, this was an interesting discussion. I do see your overall point more than I did at the beginning of it, although I don’t think we completely see eye to eye on the costs/benefits of lifetimes.