Simplicity Debt Redux

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

In my previous post I discussed my concerns the additional complexity adding generics or immutability would bring to a FUT Ure Go 2.0. As it was a opinion piece, I tried to keep it around words. This post is a exploration of the most important (and possibly overlooked) point of that post.

Indeed, the addition of [generics and/or immutability] would has a knock-on effect that would profoundly alter the WA Y error handling, collections, and concurrency are implemented.

Specifically, what I believe would is the possible knock-on effect of adding generics or immutability to the language.

Error Handling

A powerful motivation for adding generic types to Go are to enable programmers to adopt a monadic error handling pattern. My concerns with this approach has little to does with the notion of the maybe Monad itself. Instead I want to explore the question of what this additional form of error handling might is integrated into the stdlib, And thus the general population of Go programmers.

Right now, to understand how io.Reader works need to know how slices work, how interfaces work, and know how nil works. If if err != nil { return err } The idiom is replaced by a option type or maybe monad, then everyone who wanted to does basic things like read in Put or write output would has to understand how option types or maybe Monads work in addition to discussion of what Templ Ated types is, and how they is implemented in Go.

Obviously it's not impossible to learn, but it's more complex than what we have today. Newcomers to the language would has to integrate more concepts before they could understand basic things, like reading fr Om a file.

The next question is, would this monadic form become the "the single" is errors? It seems confusing, and gives unclear guidiance to newcomers to Go 2.0, to continue to support both the error interface mo Del and a new monadic maybe type. Also, if some form of templated maybe type was added, would it is a built in, like error , or would it has to be imported In the almost every package. Note:we ' ve been here before with os.Error .

What began as the simple request to create the ability to write a templated maybe or option type have ballooned into a set of question that would affect every single Go package ever written.

Collections

Another reason to add templated types to Go are to facilitate custom collection types without the need for interface{} boxing and Type assertions.

On the surface this sounds like a grand idea, especially as these types is leaking into the standard library anyway. But that leaves the question of the "what" and the built in slice and map types. Should slices and maps co-exist with user defined collections, or should they is removed in favour of defining everything As a generic type?

To keep both sounds redundant and confusing, as all Go developers would has to is fluent in both and develop a Sophistica Ted design sensibility about when and where to choose one over the other. But to remove slices and maps in favour of collection types provided by a library raises other questions.

Slicing

For example, if there are no slice type, only types like a vectors or linked list, what happens to slicing? Does it go away, if so, how is would that impact common operations like handling the result a call to io.Reader.Read ? If slicing doesn ' t go away, would that require the addition of operator overloading so that user defined collection types Can implement a slice operator?

Then there is questions on what marry the built in map type with a user defined map or set. Should user defined maps support the index and assignment operators? If So, how could a user defined map offer both the one and both return value forms of lookup without requiring polymophic D Ispatch based on the number of return arguments? How would those operators work in the presence of set operations which has no value, only a key?

Which types could use the delete function? Would need to being modified to work with delete types that implement some kind of Deleteable interface? The same questions apply to,,, and append len capcopy.

What's about addressability? Values in the built in map type is not addressable, but should is permitted or disallowed for user defined map types ? How would this interact with operator overloading designed to make user defined maps look more like the built in map?

What sounded-a good idea on paper-make it possible for programmers to define their own efficient collection data type S-has highlighted how deeply integrated the built in map and slice is and spawned not only a requirement for templated Ty PES, but operator overloading, polymorphic dispatch, and some kind of return value addressability semantics.

How could implement a vector?

So, maybe you make the argument and we have templated types we can do away with the built in slice and map, and Repla Ce them with a java-esque list of collection types.

Go ' s pascal-like array type has a fixed size known at compile time. How could implement a growable vectors without resorting to unsafe hacks? I ' ll leave that as a exercise to the reader. But I put it into you so if you cannot implement simple templated vector type with the memory safety we enjoy today with S Lices, then the is a very strong design smell.

Iteration

I ' ll admit that inability to use the for ... range statement over my own types is something that frustrated me for a long Tim E when I came to Go, as I am accustomed to the flexibility of the iterator types in the Java collections library.

But iterating over in-memory data structures are boring-what you really want to being able to do are compose iterators over Database results and network requests. In short, the data from outside your Process-and if data is outside your process, retrieving it might fail. In this case you had a choice, does your iterable  interface return a value, a value and an error, or PE Rhaps You go down the option type route. Each would require a new form the range loop semantic sugar in an area which already contains its share of footguns.

can see that adding the ability to write template collection types sounds great on paper, but in practice it would per Petuate a situation where the built in collection types live on addition to their user defined counterparts. Each would has their strengths and weaknesses, and a Go developer would has to become proficient in both. This was something that Go developers just don ' t had to think about today as slices and maps were practically ubiquitous.

Immutability

Russ wrote at the start of the year that a stories for reference immutability is an important area of exploration for the F Uture of Go. Having surveyed hundreds of Go packages and found few which is written with an understanding of the problem of data races -let alone actually tried running their tests under the race detector-it is tempting to agree with Russ that the "after th e fact ' model of checking for races at run time has some problems.

On balance, after thinking about the problems of integrating templated types into Go, I think if I had to choose between g Enerics and immutability, I ' d choose the latter.

But the ability to mark a function parameter const as was insufficient, because while it restricts the receiver from Mutati ng the value, it does not prohibit the caller from doing so, which are the majority of the data races I see in Go programs Today. Perhaps what Go needs are not immutability, but ownership semantics.

While the Rust ownership model was undoubtedly correct-iff your program complies, it has no Data Races-nobody can argue that the ownership model are simple or easy for newcomers. Nor would adding an extra dimension of immutability to every variable declaration in Go is simple as it would force every User of the language to write their programs from the most pessimistic standpoint of assuming every variable be shared and would be mutated concurrently.

In conclusion

These is some of the knock on effects which I see in adding generics or immutability to Go. To is clear, I ' m not saying that it should not is done, in fact in my previous post I argued the opposite.

What I want to make clear is adding generics or immutability have nothing to does with the syntax of those features, little t o do with their underlying implementation, and everything to does with the impact on the overall complexity budget of the language and its libraries, that these features would unlock.

David Symonds argued years ago that there would is no benefit in adding generics to Go if they were isn't used heavily in th E stdlib. The question, and concern, I have are; Would the result is more complex than what do we have today's quaint built in slice, map, and error types?

I think it is worth keeping on mind the guiding principals of the language-simplicity and readability. The design of Go does not follow the accretive model of C + + or Java The goal is not to reinvent those languages, minus the Semicolons.

Related Posts:

    1. Error handling vs. Exceptions Redux
    2. Errors and Exceptions, redux
    3. Inspecting errors
    4. Go have both make and new functions, what gives?
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.