Last active
December 1, 2018 22:47
-
-
Save bmizerany/fcd0348bda96edce05a4fc7426e47751 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package question | |
func whatsTheEquivalentOfThisWithHandleCheckQuestionMark(w io.Writer) (rN int, err error) { | |
w = LimitedWriter{w, 23} | |
n, err := io.WriteString(w, "some data 1") // 11 bytes written | |
rN += n | |
if err != nil { | |
return err | |
} | |
n, err := io.WriteString(w, "some data 2") // 22 bytes written | |
rN += n | |
if err != nil { | |
return err | |
} | |
n, err := io.WriteString(w, "some data 3") // 23 bytes written: BOOM!!! | |
rN += n | |
return rN, err | |
} | |
func canItBeThisQuestionMark(w io.Writer) (n int, err error) { | |
handle err { return n, err } | |
w = LimitedWriter{w, 23} | |
n += check io.WriteString(w, "some data 1") // 11 bytes written | |
n += check io.WriteString(w, "some data 2") // 22 bytes written | |
n += check io.WriteString(w, "some data 3") // 23 bytes written: BOOM!!! | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do not see a problem.
With
handle err { return n, err }
, it will return 22, errWith
handle err { return n, err }
, it will return 0, errProgrammer is in control. Sometimes it might be exactly what you want. I.e. what if you are consuming data from a socket or pipe, or parsing, and you find an error only at position x. Similarly if the WriteString is to a socket. You can't unread / unwrite this data.
If you are asking about whatever n will be updated, in a situation where WriteString returns
(1, SomeError)
, then this1
will be ignored, andn += 1
, will not be performed.In a situation like this you would write:
maybe?
This is because
n += check X()
is rewritten to:
``
temp, err := X()
if err != nil { call handler with err; return; }
n += temp
Notice that in Go you can't express assignment to err, and increment of n in a single statement anyway.
It does mean however than
and
do have different semantic (and the first one would use extra register / stack space). This probably only applies to return values tho. In other cases, compiler should be able to do the same as before.
I did not see the semantic explained precisely in current drafts tho.