4 ms·
> Cannot make a type in a foreign package implement an interface I would only push back on this point, and this point alone. Sure, it would be very convenient
by lelandbatey 4y ago
> Cannot make a type in a foreign package implement an interface
I would only push back on this point, and this point alone. Sure, it would be very convenient for me, an author to be able to just extend the implementation of anything that I import. However, when I am not an author and am instead a reader of code, doing this is the number-one way to causally make code totally unreadable. By allowing anyone to extend a type anywhere, it makes it impossible to just read the code. If the package that declares a type is the sole controller of that type, then reading code is easier.
"Where is the definition of this method?" Answer: It's next to everything else for that type, in the package where that type is defined. However, if anyone can extend any other type, figuring out what methods even exist for a type becomes so difficult that you are forced to use a language-server (with all related dependencies installed) in order to use "go-to-definition". If someone wants to understand the code, requiring them to have a full-fledged IDE and/or development environment is pretty awful and quite onerous, and makes codebases tough to get into.
For example, take this line[0] from a tutorial[1] on creating a simple snake game using the Bevvy engine in Rust. The main() function wires up loads of entities, including a `snake_eating()` function. But it also calls a mysterious `.after()` method of the snake_eating() function. If you do a ctrl-f on the file, you won't find an .after() method defined for the function snake_eating(). Just reading the code in that file, the only file in the program, you'll never find what the `.after()` method does or where it comes from. Only by opening that code in an IDE with go-to-definition will you learn that it's set via an automatic macro in the bevvy library[2] which causes all functions of certain signatures to implement the System trait. Which... is pretty tough to get into, and means just reading code without computer aid is effectively impossible.
In your languages, please don't allow packages to extend the interfaces/traits/types of other packages.
[0] - https://github.com/marcusbuffett/bevy_snake/blob/0.7/src/main.rs#L282 https://github.com/marcusbuffett/bevy_snake/blob/0.7/src/mai...
[1] - https://mbuffett.com/posts/bevy-snake-tutorial/ https://mbuffett.com/posts/bevy-snake-tutorial/
[2] - https://github.com/bevyengine/bevy/discussions/1137 https://github.com/bevyengine/bevy/discussions/1137
- ngngngng 4y ago> doing this is the number-one way to causally make code totally unreadable. Absolutely, my immediate thought as well. I believe my experience reading and writing Go has been more pleasant than in other languages because people writing Go seem to shy away from hidden behavior, and I think rigidity like this helps. Also, to the author, "anyways" is not a word.
- typesanitizer 4y agoI sympathize with your point that being able to use simpler tools (such as grep) is often a good thing instead of having to rely on heavy-duty functionality (such as a full IDE/language server). That said: - Ctrl+F in a file specifically isn't guaranteed to always return a hit with Go since a package can span multiple files, and you can have methods in other files. - Increasingly many tools (like Sourcegraph and GitHub) make rich code navigation available on the web, without having to use an IDE or check out the code locally. Yes, it's not perfect for all languages, but it's improving every day. Similarly for documentation tools in different ecosystems -- I think both Haddock (Haskell) and rustdoc support cross-linking references to definitions. - If you have textual code search for dependencies, you can still grep through the code with a regex like `func \(.* *?MyType\) myMethod`, and it will give you hits in other packages too.
- lelandbatey 4y agoI also agree that if I had the full IDE like power everywhere, then that'd be great. And having those tools in more places is also great. Ctrl-f being limited to a single file is a downside, but it degenerates to cases where you may have to ctrl-f in 5 other files in the folder up to maybe 50 files in the folder for a huge package. And from there, it's narrow-able based on things like names of files. However, allowing any package to modify any other package causes that number to explode well past what is ever humanly tractable. It goes from "inconvenient" to "impossible". Language designers, please design your language so we can read it with our mere human eyes.