FAQ
I want to do something like this http://play.golang.org/p/eIVIE_wP67
(pasted below) but i get

cannot use newAny() (type any) as type all in field value: need type
assertion


"all" is allowed to be an "any" in "newAny" but isn't allowed to be an
"any" when embedded in "allThing"?


package main

import "fmt"

type any interface{}
type all struct{}

func newAny() any {
  return all{}
}

type anyThing interface {
  any
}
type allThing struct {
  all
}

func newThing() anyThing {
  return allThing{all: newAny()} // This is not allowed
}

func main() {
  n := newThing()
  fmt.Printf("%T %+v", n, n)
}


Thanks

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

  • Jan Mercl at Nov 6, 2014 at 11:48 am
    On Thu, Nov 6, 2014 at 12:43 PM, nicerobot wrote:

    As the error message "...need type assertion" says:
    http://play.golang.org/p/F0WlK1ZYhA

    -j

    --
    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.
  • Nicerobot at Nov 6, 2014 at 1:17 pm
    Thanks. But please explain why. "all" is an "any". Why isn't an embedded
    "all" also an "any"?
    On Thursday, November 6, 2014 6:48:13 AM UTC-5, Jan Mercl wrote:

    On Thu, Nov 6, 2014 at 12:43 PM, nicerobot <nice...@gmail.com
    <javascript:>> wrote:

    As the error message "...need type assertion" says:
    http://play.golang.org/p/F0WlK1ZYhA

    -j
    --
    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.
  • Nicerobot at Nov 6, 2014 at 1:21 pm
    Sorry, ignore that. I see the issue.
    "all" is an "any" but an "any" isn't an "all".
    Duh!
    On Thursday, November 6, 2014 8:17:10 AM UTC-5, nicerobot wrote:

    Thanks. But please explain why. "all" is an "any". Why isn't an embedded
    "all" also an "any"?
    On Thursday, November 6, 2014 6:48:13 AM UTC-5, Jan Mercl wrote:

    On Thu, Nov 6, 2014 at 12:43 PM, nicerobot wrote:

    As the error message "...need type assertion" says:
    http://play.golang.org/p/F0WlK1ZYhA

    -j
    --
    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.
  • Egon at Nov 6, 2014 at 1:30 pm
    How can i implement a "generic" base interface/struct hierarchy?

    Don't try to build type hierarchy, trying to do it in Go will only hurt
    you.

    Use two levels... interfaces, implementers (i.e. func/struct). If there is
    a lot of commonality between implementers, then embed the common parts.

    Of course, because you are not actually saying what you are doing
    <https://code.google.com/p/go-wiki/wiki/HowToAsk>. It is difficult to give
    any better advice.

    + egon

    --
    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
postedNov 6, '14 at 11:43a
activeNov 6, '14 at 1:30p
posts5
users3
websitegolang.org

3 users in discussion

Nicerobot: 3 posts Egon: 1 post Jan Mercl: 1 post

People

Translate

site design / logo © 2022 Grokbase