FAQ
Suppose I have an interface Expr and I know that a variable of this type is
a pointer to some concrete struct. Is there a sensible way to determine
whether it is nil if I don't know the specific type of the pointer?

The motivation here is that I have an interface

     type Expr interface {
         Begin() int
     }

And all my implementations are pointers to structs:

     type NameExpr struct {
         ...
     }

Then at various points in my code I have a list of Expr objects, some of
which are nil pointers, and I want to get the Begin() of the first one that
is non-nil.

So I have a function

     func getFirst(exprs []Expr) {
         for _, expr := range expr {
             if expr != nil { return expr.Begin() }
         }
     }

But of course "if expr != nil" doesn't do what I expect because the
interface variable, is not nil, but the value it contains is a nil pointer.

I realize I could do a giant type switch over all the implementations of
Expr, but there are rather a lot of them!

Any suggestions?

--
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

  • Nodir Turakulov at Oct 15, 2015 at 7:00 pm
    reflection would work (isNil := e == nil ||
    !reflect.ValueOf(e).Elem().IsValid()), but I'd recommend to return a true
    nil in code that converts a *NameExpr to Expr:

    func foo() Expr {
       var ne *NameExpr = ...
       if ne == nil {
         return nil
       }
       return ne
    }
    On Thursday, October 15, 2015 at 11:41:56 AM UTC-7, Alex Flint wrote:

    Suppose I have an interface Expr and I know that a variable of this type
    is a pointer to some concrete struct. Is there a sensible way to determine
    whether it is nil if I don't know the specific type of the pointer?

    The motivation here is that I have an interface

    type Expr interface {
    Begin() int
    }

    And all my implementations are pointers to structs:

    type NameExpr struct {
    ...
    }

    Then at various points in my code I have a list of Expr objects, some of
    which are nil pointers, and I want to get the Begin() of the first one that
    is non-nil.

    So I have a function

    func getFirst(exprs []Expr) {
    for _, expr := range expr {
    if expr != nil { return expr.Begin() }
    }
    }

    But of course "if expr != nil" doesn't do what I expect because the
    interface variable, is not nil, but the value it contains is a nil pointer.

    I realize I could do a giant type switch over all the implementations of
    Expr, but there are rather a lot of them!

    Any suggestions?
    --
    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.
  • Ian Lance Taylor at Oct 15, 2015 at 8:54 pm

    On Thu, Oct 15, 2015 at 11:41 AM, Alex Flint wrote:
    Suppose I have an interface Expr and I know that a variable of this type is
    a pointer to some concrete struct. Is there a sensible way to determine
    whether it is nil if I don't know the specific type of the pointer?

    The motivation here is that I have an interface

    type Expr interface {
    Begin() int
    }

    And all my implementations are pointers to structs:

    type NameExpr struct {
    ...
    }

    Then at various points in my code I have a list of Expr objects, some of
    which are nil pointers, and I want to get the Begin() of the first one that
    is non-nil.

    So I have a function

    func getFirst(exprs []Expr) {
    for _, expr := range expr {
    if expr != nil { return expr.Begin() }
    }
    }

    But of course "if expr != nil" doesn't do what I expect because the
    interface variable, is not nil, but the value it contains is a nil pointer.

    I realize I could do a giant type switch over all the implementations of
    Expr, but there are rather a lot of them!

    Any suggestions?
    It sounds like it is an important feature of your code to know whether
    a value of type Expr is a nil pointer. So one approach would be to
    add an IsNil method to the interface. Then write several

    func (n *NameExpr) IsNil() bool {
         return n == nil
    }

    methods.

    Ian

    --
    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.
  • Roberto Zanotto at Oct 15, 2015 at 9:07 pm
    reflect.ValueOf(expr).IsNil() should work...
    https://play.golang.org/p/Okyy0SohIs
    On Thursday, October 15, 2015 at 8:41:56 PM UTC+2, Alex Flint wrote:

    Suppose I have an interface Expr and I know that a variable of this type
    is a pointer to some concrete struct. Is there a sensible way to determine
    whether it is nil if I don't know the specific type of the pointer?

    The motivation here is that I have an interface

    type Expr interface {
    Begin() int
    }

    And all my implementations are pointers to structs:

    type NameExpr struct {
    ...
    }

    Then at various points in my code I have a list of Expr objects, some of
    which are nil pointers, and I want to get the Begin() of the first one that
    is non-nil.

    So I have a function

    func getFirst(exprs []Expr) {
    for _, expr := range expr {
    if expr != nil { return expr.Begin() }
    }
    }

    But of course "if expr != nil" doesn't do what I expect because the
    interface variable, is not nil, but the value it contains is a nil pointer.

    I realize I could do a giant type switch over all the implementations of
    Expr, but there are rather a lot of them!

    Any suggestions?
    --
    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.
  • Henrik Johansson at Oct 16, 2015 at 5:59 am
    I am not sure if it applies to your case but transparently handling nil in
    the underlying type might work if there is a sensible default value. I.e.
    It is like Ian said but the test moves into the underlying type.
    On Thu, Oct 15, 2015, 23:07 Roberto Zanotto wrote:

    reflect.ValueOf(expr).IsNil() should work...
    https://play.golang.org/p/Okyy0SohIs

    On Thursday, October 15, 2015 at 8:41:56 PM UTC+2, Alex Flint wrote:

    Suppose I have an interface Expr and I know that a variable of this type
    is a pointer to some concrete struct. Is there a sensible way to determine
    whether it is nil if I don't know the specific type of the pointer?

    The motivation here is that I have an interface

    type Expr interface {
    Begin() int
    }

    And all my implementations are pointers to structs:

    type NameExpr struct {
    ...
    }

    Then at various points in my code I have a list of Expr objects, some of
    which are nil pointers, and I want to get the Begin() of the first one that
    is non-nil.

    So I have a function

    func getFirst(exprs []Expr) {
    for _, expr := range expr {
    if expr != nil { return expr.Begin() }
    }
    }

    But of course "if expr != nil" doesn't do what I expect because the
    interface variable, is not nil, but the value it contains is a nil pointer.

    I realize I could do a giant type switch over all the implementations of
    Expr, but there are rather a lot of them!

    Any suggestions?
    --
    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.
    --
    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
postedOct 15, '15 at 6:41p
activeOct 16, '15 at 5:59a
posts5
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase