Live data from Hacker News

FP-Go: Functional programming library for Golang

github.com

161–170 of 185 posts

Re: FP-Go: Functional programming library for Golang

#161
post #153

Earlier quoted context omitted.

So you've defined idiomatic Go code in your own way, that no one else's definitions match with, such that no idiomatic Go code actually exists. If no idiomatic Go code exists, then it's definitionally true that all idiomatic Go code is without flaws, but I don't agree with your personalized definition of idiomatic Go in the first place. > That may be true, but of no relevance. So you agree with this, and it rebuts th…

Just in support of your position, the Google Style Guide says the following: > If a function returns an error, callers must treat all non-error return values as unspecified unless explicitly documented otherwise It is absolutely not idiomatic to give any meaning to non-error values under error conditions in the usual case. That is extremely unusual and would be generally confusing. https://google.github.io/styleguide…

> It is absolutely not idiomatic to give any meaning to non-error values under error conditions in the usual case.

Based on what? We have come to see that it is beneficial to make zero values useful. In fact, Go Proverbs even says so. Likewise, we have learned it is useful to return the zero value when you have an error. Most commonly, this means returning nil, which is packed full of all kinds of useful information. Therefore, the T value can be expected to useful if the code is idiomatic.

I'd love to see some real-world code you think is idiomatic, but doesn't return a useful T value when there is an error.

> If a function returns an error, callers must treat all non-error return values as unspecified unless explicitly documented otherwise

This is not at odds with that. This merely warns that not all code you may call will be idiomatic. In fact, if I recall correctly, doesn't os.Open (maybe os.Create) return an invalid file handle in some error cases? The standard library is old and what helped us eventually see what is idiomatic. It is decidedly not idiomatic for the most part. If you rely on a function being written idiomatically, then you are going to run into trouble, as that is not a guarantee (unless the documentation provides such a guarantee).

But in the case of this Either wrapper, it explicitly states it is for use with idiomatic code, not any old code you can throw at it.

Re: FP-Go: Functional programming library for Golang

#162
post #140

Earlier quoted context omitted.

Anything that implements or consumes `io.Reader` or `io.Writer` would dispute that.

Yeah, there are counterexamples, but the only way to know is to read the comments or source code of the function you're calling. (T, err) doesn't convey any useful information and, in the overwhelming majority of cases, err != nil means T is a meaningless default value that should be ignored or a null pointer. By and large I think the stuff in this repo is too much and doesn't fit Go. I don't particularly want Go to…

> means T is a meaningless default value

Go Proverb #5: Make the zero value useful.

> that should be ignored or a null pointer

nil is the zero value of a pointer, so it should be made useful per the above, but it is also inherently useful even if you put no thought into it. It allows you to know that there is an absence of a value out of the box.

And this is actually why the vast majority of (T, error) cases in idiomatic code sees T be a pointer, despite the computational and programatic downsides of using a pointer, so that nil can be returned when the value is not otherwise useful – exactly to ensure the value is as useful as possible, denoting the absence of a usable value.

If you read through idiomatic code, you'll notice that only when the underlying type is more meaningful is a pointer not used. Returning a slice is one such example. An empty set upon error is more meaningful than nil, usually. Another common instance is when 0 is meaningful, like in the aforementioned io.Reader interface. Idiomatically, one will always strive to return the most meaningful value they can.

> Either and Option at least would be nice to have in the stdlib

And if it were, then this Either wrapper in question would become useful as an overlay to it, as they would then share the same intent and meaning. But it does not match the current semantics of idiomatic Go code using the (T, error) pattern.

You can probably make it work, but code is about communicating ideas to other programmers. Either implies a dependence between variables. (T, error) has no such dependence. There is an impedance mismatch here which fails to properly communicate what is happening.

Re: FP-Go: Functional programming library for Golang

#163
post #140

Earlier quoted context omitted.

Yeah, there are counterexamples, but the only way to know is to read the comments or source code of the function you're calling. (T, err) doesn't convey any useful information and, in the overwhelming majority of cases, err != nil means T is a meaningless default value that should be ignored or a null pointer. By and large I think the stuff in this repo is too much and doesn't fit Go. I don't particularly want Go to…

> means T is a meaningless default value Go Proverb #5: Make the zero value useful. > that should be ignored or a null pointer nil is the zero value of a pointer, so it should be made useful per the above, but it is also inherently useful even if you put no thought into it. It allows you to know that there is an absence of a value out of the box. And this is actually why the vast majority of (T, error) cases in idiom…

>Go Proverb #5: Make the zero value useful.

Yeah, it's a nice quip, but that's all it is. It sounds nice on first read to someone who doesn't program much. But it is inaccurate and not followed by Go, and is explicitly against the Google style guide.

The sophistry trying to paint a nil pointer as "useful" is just trying to defend a position you've dug yourself into in the process of this argument, so it doesn't really need to be addressed again.

>An empty set upon error is more meaningful than nil, usually.

This in particular is just a mistake in Go. Nil maps, unlike nil slices, cause panics, so people try to avoid ever returning them.

>But it does not match the current semantics of idiomatic Go code using the (T, error) pattern.

But it does match how (T, error) is actually used the majority of the time. The impedance mismatch is that code that currently has the semantics of Either, which is the vast majority of idiomatic Go, needs to use (T, error).

Re: FP-Go: Functional programming library for Golang

#164
post #153

Earlier quoted context omitted.

So you've defined idiomatic Go code in your own way, that no one else's definitions match with, such that no idiomatic Go code actually exists. If no idiomatic Go code exists, then it's definitionally true that all idiomatic Go code is without flaws, but I don't agree with your personalized definition of idiomatic Go in the first place. > That may be true, but of no relevance. So you agree with this, and it rebuts th…

> I don't agree with your personalized definition of idiomatic Go in the first place. "That's the point. They are not equivalent, they represent different things" – Oh snap. Well, so much for that silly tangent. If you want to actually have a discussion, you are going to have to go and understand the discussion that was taking place before you tried to take it in some weird and nonsensical direction. The non-sequitor…

Since you seem unwilling or unable to follow an argument you started this seems more like an attempt to bait a response that you can report to the mods instead of an honest conversation. If you want to know why the alleged tangent was relevant, well, the posts are still there.

Re: FP-Go: Functional programming library for Golang

#165

Earlier quoted context omitted.

Just in support of your position, the Google Style Guide says the following: > If a function returns an error, callers must treat all non-error return values as unspecified unless explicitly documented otherwise It is absolutely not idiomatic to give any meaning to non-error values under error conditions in the usual case. That is extremely unusual and would be generally confusing. https://google.github.io/styleguide…

> It is absolutely not idiomatic to give any meaning to non-error values under error conditions in the usual case. Based on what? We have come to see that it is beneficial to make zero values useful. In fact, Go Proverbs even says so. Likewise, we have learned it is useful to return the zero value when you have an error. Most commonly, this means returning nil, which is packed full of all kinds of useful information.…

>Based on what? ... In fact, Go Proverbs even says so.

And the Go style guide says otherwise: if err is non-nil, you shouldn't even check the other return values. The Go proverbs are just words, it doesn't make them true or even good ideas. When Go proverbs don't agree with how Go code is written, including idiomatic Go code, reality wins over a bad theory. The Go style guide happens to better match real code written by real people, even the people directly responsible for the proverbs.

>I'd love to see some real-world code you think is idiomatic

To be fair, I have been talking about the Go programming language and idiomatic code written in that language. You seem to have a different idea of "Idiomatic Go" from everyone else's. Most of the stdlib, including the newest additions, is idiomatic as judged by other people but evidently not by you.

Re: FP-Go: Functional programming library for Golang

#166
post #165

Earlier quoted context omitted.

> It is absolutely not idiomatic to give any meaning to non-error values under error conditions in the usual case. Based on what? We have come to see that it is beneficial to make zero values useful. In fact, Go Proverbs even says so. Likewise, we have learned it is useful to return the zero value when you have an error. Most commonly, this means returning nil, which is packed full of all kinds of useful information.…

> Based on what? ... In fact, Go Proverbs even says so. And the Go style guide says otherwise: if err is non-nil, you shouldn't even check the other return values. The Go proverbs are just words, it doesn't make them true or even good ideas. When Go proverbs don't agree with how Go code is written, including idiomatic Go code, reality wins over a bad theory. The Go style guide happens to better match real code writte…

> if err is non-nil, you shouldn't even check the other return values.

Yes, it says you cannot trust functions, unless documented, to be idiomatic. Which is reasonable as not all code is idiomatic. The language goes to no lengths to enforce how the code is written in this regard (obviously). But we are talking about code that is known to be idiomatic.

> Most of the stdlib, including the newest additions, is idiomatic as judged by other people but evidently not by you.

1. I don't see how it could be. What is idiomatic emerges from writing code and seeing what works and what doesn't. Most of the stdlib was written in the early days before anyone understood what works best. And, thanks to the go1 guarantee, modifying it now is out of the question.

2. If you believe that the stdlib is idiomatic, then you have to accept that returning an int (-1) to represent an error is idiomatic. The standard library is full of that. Which violates the premise of FP-Go that (T, error) is idiomatic. That is not my claim, that is theirs.

Re: FP-Go: Functional programming library for Golang

#167
post #165

Earlier quoted context omitted.

> Based on what? ... In fact, Go Proverbs even says so. And the Go style guide says otherwise: if err is non-nil, you shouldn't even check the other return values. The Go proverbs are just words, it doesn't make them true or even good ideas. When Go proverbs don't agree with how Go code is written, including idiomatic Go code, reality wins over a bad theory. The Go style guide happens to better match real code writte…

> if err is non-nil, you shouldn't even check the other return values. Yes, it says you cannot trust functions, unless documented, to be idiomatic. Which is reasonable as not all code is idiomatic. The language goes to no lengths to enforce how the code is written in this regard (obviously). But we are talking about code that is known to be idiomatic. > Most of the stdlib, including the newest additions, is idiomatic…

>Yes, it says you cannot trust functions, unless documented, to be idiomatic.

That is not what it says, in fact, it almost says the opposite. It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented. It never affirms your particular, personal, definition of what "Idiomatic Go" is and directly contradicts it.

>If you believe that the stdlib is idiomatic

I believe most of the stdlib is idiomatic Go, especially the newer stuff. Your excuse earlier was that the older stuff in the stdlib isn't idiomatic but the newer stuff is. But the newer stuff doesn't match your personal standards for being idiomatic either. I would challenge you to point to a large body of code that actually matches your definition of idiomatic, I do not think it exists.

Re: FP-Go: Functional programming library for Golang

#168
post #167

Earlier quoted context omitted.

> if err is non-nil, you shouldn't even check the other return values. Yes, it says you cannot trust functions, unless documented, to be idiomatic. Which is reasonable as not all code is idiomatic. The language goes to no lengths to enforce how the code is written in this regard (obviously). But we are talking about code that is known to be idiomatic. > Most of the stdlib, including the newest additions, is idiomatic…

> Yes, it says you cannot trust functions, unless documented, to be idiomatic. That is not what it says, in fact, it almost says the opposite. It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented. It never affirms your particular, personal, definition of what "Idiomatic Go" is and directly contradicts it. > If you believe that the stdlib is idiomatic I believe most of the…

> It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented.

No, it is very much written from a consumer perspective. There is known, non-idiomatic, code where relying on T in its error state is problematic, so the guidance is reasonable and logical.

But we're talking about this from the producer perspective. These do not challenge each other and can exist in harmony.

> believe most of the stdlib is idiomatic Go, especially the newer stuff.

I wholeheartedly agree, at least with respect to the newer stuff, and have already said as such. Now this is where, in that newer work which is idiomatic, you point to a good example of where you can find meaningless return values when there is an error.

Clearly the vast majority of the standard library, especially in the newer stuff, does return meaningful values upon error, so we need to identify those which buck the trend if we want to hold it as a counterexample of this being an idiomatic practice.

Re: FP-Go: Functional programming library for Golang

#169
post #167

Earlier quoted context omitted.

> Yes, it says you cannot trust functions, unless documented, to be idiomatic. That is not what it says, in fact, it almost says the opposite. It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented. It never affirms your particular, personal, definition of what "Idiomatic Go" is and directly contradicts it. > If you believe that the stdlib is idiomatic I believe most of the…

> It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented. No, it is very much written from a consumer perspective. There is known, non-idiomatic, code where relying on T in its error state is problematic, so the guidance is reasonable and logical. But we're talking about this from the producer perspective. These do not challenge each other and can exist in harmony. > believe…

>No, it is very much written from a consumer perspective. We're talking about from the producer perspective.

This is just a lie. It's written about Go and doesn't split its perspectives, the producer should only ever return meaningful non-error values with an error if explicitly documented, and the consumer should only ever expect those when documented.

>you point to a good example of where you can find un-meaningful return values when there is an error.

Okay, skimming the release notes for 1.21. The slog package, brand new in 1.21, contains a MarshalText method (and others, but this is the first I noticed). It returns ([]bytes, err) and it is not documented to be idiomatic as per your personal definition. Therefore, by your interpretation of the style guide and your own personal standards, it is not idiomatic. What's more, a nil/empty slice is potentially valid output from marshaling something, so the slice is entirely useless and must be ignored if err is non-nil. It is not valid to try to "derive meaning" (your terminology) from the slice being nil or not, so it is well and truly useless if err isn't nil.

Moreover, I'll assert that if Either existed in the stdlib, it would return Either[bytes[], err] instead of ([]bytes, err), and it is a limitation of Go that they're using (T, error) which does not offer the appropriate semantics.

>Clearly the vast majority of the standard library, especially in the newer stuff, does return meaningful values upon error

This, too, is simply a lie.

Re: FP-Go: Functional programming library for Golang

#170
post #169

Earlier quoted context omitted.

> It is idiomatic Go to never return useful values if error is non-nil unless explicitly documented. No, it is very much written from a consumer perspective. There is known, non-idiomatic, code where relying on T in its error state is problematic, so the guidance is reasonable and logical. But we're talking about this from the producer perspective. These do not challenge each other and can exist in harmony. > believe…

> No, it is very much written from a consumer perspective. We're talking about from the producer perspective. This is just a lie. It's written about Go and doesn't split its perspectives, the producer should only ever return meaningful non-error values with an error if explicitly documented, and the consumer should only ever expect those when documented. > you point to a good example of where you can find un-meaningf…

> The slog package, brand new in 1.21, contains a MarshalText method

At quick glance, I found three MarshalText implementations in the slog package. Which implementation are you referring to specifically?

And, for what it is worth, none of them return useless values on error, so where do I find the fourth which does?

> This, too, is simply a lie.

Let's hope. I love nothing more than being wrong. That means I get to learn something new! But I haven't found it yet, which is sorrily disappointing. Looking forward to you clarifying the above so we can put this to rest.

Post reply on HN