FAQ
I've read http://dave.cheney.net/2014/12/24/inspecting-errors and I think I
understand
"error values returned from functions should be opaque to the caller"
philosophy.

But I don't like sample code in the post:

func isTimeout(err error) bool {
         type timeout interface {
                 Timeout() bool
         }
         if te, ok := err.(timeout); ok {
                 return te.Timeout()
         }
         return false
}


If `Timeout() bool` and `Temporary() bool` is so common, why they are not
in errors package?
I know `net.Error` interface. It includes both of Timeout and Temporary.
Is it good to use `net.Error` for investigating error value returned from
package other than net?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Dave Cheney at Apr 29, 2015 at 10:59 pm
    Hello,

    The reason I used an anonymous interface e type in that example was to show that no one package needs to define or own the definition of an interface which has a Timeout method.

    What I was trying to show is that any piece of code can inspect a error value they get to see if it has the behaviour they want, because it implements a commonly understood method. Putting the example in a helper function was probably misleading, this was my mistake.

    Dave

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Naoki INADA at Apr 29, 2015 at 11:38 pm
    Hi, David. Thanks for quick reply.
    On Thursday, April 30, 2015 at 7:59:36 AM UTC+9, Dave Cheney wrote:

    Hello,

    The reason I used an anonymous interface e type in that example was to
    show that no one package needs to define or own the definition of an
    interface which has a Timeout method.

    In the same post, you wrote:
    so Timeout() bool and Temporary() bool would cover a large set of the
    use cases.

    My question is: if Timeout() bool and Temporary() bool cover large use
    cases, why they aren't in stdlib?
    When I write interface for timeout, I may make a mistake (typo, or forget
    return type) and compiler won't catch it.


    What I was trying to show is that any piece of code can inspect a error
    value they get to see if it has the behaviour they want, because it
    implements a commonly understood method. Putting the example in a helper
    function was probably misleading, this was my mistake.

    >
    Dave


    What I can't understand is who define interface for the behaviour.
    All callers should define interface for "commonly understood method" ?

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Apr 30, 2015 at 1:52 am

    On Thursday, 30 April 2015 09:38:53 UTC+10, Naoki INADA wrote:
    Hi, David. Thanks for quick reply.
    On Thursday, April 30, 2015 at 7:59:36 AM UTC+9, Dave Cheney wrote:

    Hello,

    The reason I used an anonymous interface e type in that example was to
    show that no one package needs to define or own the definition of an
    interface which has a Timeout method.

    In the same post, you wrote:
    so Timeout() bool and Temporary() bool would cover a large set of the
    use cases.

    My question is: if Timeout() bool and Temporary() bool cover large use
    cases, why they aren't in stdlib?
    Where would you put them in the standard library ? What would they look
    like specifically ?

    When I write interface for timeout, I may make a mistake (typo, or forget
    return type) and compiler won't catch it.
    That is correct, you will have to write unit test coverage for this.

    What I was trying to show is that any piece of code can inspect a error
    value they get to see if it has the behaviour they want, because it
    implements a commonly understood method. Putting the example in a helper
    function was probably misleading, this was my mistake.
    Dave


    What I can't understand is who define interface for the behaviour.
    All callers should define interface for "commonly understood method" ?
    There is no formal definition, it's a convention which seems to be logical,
    we all understand Read, Write and Close, so Timeout() bool shouldn't be
    that hard for people to agree on.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Naoki INADA at Apr 30, 2015 at 5:26 am

    On Thursday, April 30, 2015 at 10:52:32 AM UTC+9, Dave Cheney wrote:
    My question is: if Timeout() bool and Temporary() bool cover large use
    cases, why they aren't in stdlib?
    Where would you put them in the standard library ? What would they look
    like specifically ?
    errors package.
    It looks like

    import "errors"


    func isTimeout(err error) bool {
         if t, ok := err.(errors.Timeout); ok {
             return t.Timeout()
         }
         return false
    }

    When I write interface for timeout, I may make a mistake (typo, or forget
    return type) and compiler won't catch it.
    That is correct, you will have to write unit test coverage for this.
    But it looks like dynamic language...

    What I was trying to show is that any piece of code can inspect a error
    value they get to see if it has the behaviour they want, because it
    implements a commonly understood method. Putting the example in a helper
    function was probably misleading, this was my mistake.
    What I can't understand is who define interface for the behaviour.
    All callers should define interface for "commonly understood method" ?
    There is no formal definition, it's a convention which seems to be
    logical, we all understand Read, Write and Close, so Timeout() bool
    shouldn't be that hard for people to agree on.
    Dave
    I don't understand what errors can be returned by Read, Write and Close.
    While it should be opaque, I want to know which API can be return Temporary
    error.
    Standard interface may helps documenting like "It may return
    errors.Temporary error."

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 29, '15 at 10:23p
activeApr 30, '15 at 5:26a
posts5
users2
websitegolang.org

2 users in discussion

Naoki INADA: 3 posts Dave Cheney: 2 posts

People

Translate

site design / logo © 2022 Grokbase