FAQ
This piece of code

type Base struct {}
func (Base) Magic() { fmt.Print("base magic") }
func (self Base) MoreMagic() {
self.Magic()
self.Magic()
}

type Foo struct {
Base
}
func (Foo) Magic() { fmt.Print("foo magic") }

f := new(Foo)
prints "base magic base magic" and not "foo magic foo magic", which would
give us more options. Brings the "Inner" keyword in the Beta programming
language to my mind, see Fig.2 on p.6 in this document:
http://fsl.cs.uiuc.edu/pubs/hills-aktemur-rosu-2005-tr.pdf

Wonder whether it could be applied here. So instead of

func (self Base) MoreMagic() {
self.Magic()
self.Magic()
}

I coud say:

func (self Base) MoreMagic() {
inner.Magic()
self.Magic()
}

and it would print "foo magic base magic". Would be really "magic", but
maybe it is not connected and I'm only dreaming ...

--
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/groups/opt_out.

Search Discussions

  • Steve wang at Feb 13, 2013 at 10:49 am
    I prefer the way which is clearer and easier to understand and does not
    need any new keywords:

    package main

    import "fmt"

    type Magician interface {

    Magic()

    }

    type Base struct{

    magician Magician

    }

    func (Base) Magic() {

    fmt.Print("base magic")

    }

    func (self Base) MoreMagic() {

    self.magician.Magic()

    self.Magic()

    }

    type Foo struct {

    Base

    }

    func (Foo) Magic() {

    fmt.Print("foo magic")

    }

    func main() {

    f := new(Foo)

    f.magician = f

    f.MoreMagic()

    }

    On Wednesday, February 13, 2013 6:31:03 PM UTC+8, Saxo wrote:

    This piece of code

    type Base struct {}
    func (Base) Magic() { fmt.Print("base magic") }
    func (self Base) MoreMagic() {
    self.Magic()
    self.Magic()
    }

    type Foo struct {
    Base
    }
    func (Foo) Magic() { fmt.Print("foo magic") }

    f := new(Foo)
    prints "base magic base magic" and not "foo magic foo magic", which would
    give us more options. Brings the "Inner" keyword in the Beta programming
    language to my mind, see Fig.2 on p.6 in this document:
    http://fsl.cs.uiuc.edu/pubs/hills-aktemur-rosu-2005-tr.pdf

    Wonder whether it could be applied here. So instead of

    func (self Base) MoreMagic() {
    self.Magic()
    self.Magic()
    }

    I coud say:

    func (self Base) MoreMagic() {
    inner.Magic()
    self.Magic()
    }

    and it would print "foo magic base magic". Would be really "magic", but
    maybe it is not connected and I'm only dreaming ...
    --
    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/groups/opt_out.
  • Saxo at Feb 13, 2013 at 12:00 pm
    In your solution Base does not need to implement Magician as in other
    languages since in Go interfaces are implicitly derived. This feature is
    really powerfull. Otherwise the coding effort would be much harder and
    things could not be made transparent. Thanks for your solution.


    func (delegate Delegate) Magic() {
    if _, ok := delegate.magician.(Magician); ok {
    delegate.magician.Magic()
    } else {
    fmt.Println("base magic")
    }
    }
    Does this incur a runtime penalty? Only asking, because I'm still a Go
    novice and don't know ...

    -- Saxo

    --
    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/groups/opt_out.
  • Mattn at Feb 13, 2013 at 12:28 pm
    Does this incur a runtime penalty? Only asking, because I'm still a Go
    novice and don't know ...

    http://en.wikipedia.org/wiki/Duck_typing

    --
    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/groups/opt_out.
  • John Asmuth at Feb 13, 2013 at 2:25 pm
    Since delegate.magician already has type Magician, the only way that ok
    could be false is if delegate.magician were nil. Best to just check "if
    delegate.magician != nil" instead.
    On Wednesday, February 13, 2013 7:00:53 AM UTC-5, Saxo wrote:

    func (delegate Delegate) Magic() {
    if _, ok := delegate.magician.(Magician); ok {
    delegate.magician.Magic()
    } else {
    fmt.Println("base magic")
    }
    }
    Does this incur a runtime penalty? Only asking, because I'm still a Go
    novice and don't know ...
    The first time you assert a particular concrete type contained in one
    interface to another interface (perhaps in your example the compiler can
    skip this), it does a comparison of the method sets of the interface and
    the concrete type. After that, it can do a fast lookup.

    --
    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/groups/opt_out.
  • Mattn at Feb 13, 2013 at 11:00 am
    You can check, the Foo implemented Magic or not.

    package main

    import (
    "fmt"
    )

    type Magician interface {
    Magic()
    }

    type Delegate struct{
    magician Magician
    }

    func (delegate Delegate) Magic() {
    if _, ok := delegate.magician.(Magician); ok {
    delegate.magician.Magic()
    } else {
    fmt.Println("base magic")
    }
    }

    func (delegate Delegate) MoreMagic() {
    delegate.magician.Magic()
    delegate.magician.Magic()
    }

    type Foo struct {
    Delegate
    }

    func (foo Foo) Magic() {
    fmt.Println("foo magic")
    }

    func main() {
    f := Delegate{new(Foo)}
    f.Magic() // if remove implement of Foo.Magic, it will show 'base magic'
    f.MoreMagic()
    }


    --
    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/groups/opt_out.
  • John Nagle at Feb 13, 2013 at 9:05 pm

    On 2/13/2013 2:31 AM, Saxo wrote:
    This piece of code

    type Base struct {}
    func (Base) Magic() { fmt.Print("base magic") }
    func (self Base) MoreMagic() {
    self.Magic()
    self.Magic()
    }

    type Foo struct {
    Base
    }
    func (Foo) Magic() { fmt.Print("foo magic") }

    f := new(Foo)
    prints "base magic base magic" and not "foo magic foo magic".
    That doesn't print anything. It won't even compile.
    I assume the intent was to call ".Magic()" on f. Like this:

    http://play.golang.org/p/EGZVPHPYpa

    That prints "foo magic", indicating that the function of the
    derived class (OK, in Go jargon "method bound to a struct type
    which uses Base as an anonymous field") was called instead of
    the one for Base.

    That's what should happen. What's the problem?

    John Nagle

    --
    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/groups/opt_out.
  • Saxo at Feb 14, 2013 at 9:23 am

    That doesn't print anything. It won't even compile.
    I assume the intent was to call ".Magic()" on f. Like this:
    Some lines of code were missing. It should be:

    f := new(Foo)
    f.Magic() //=> foo magic
    f.MoreMagic() //=> base magic base magic
    The sample is taken from http://www.infoq.com/articles/google-go-primer

    -- Saxo

    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 13, '13 at 10:31a
activeFeb 14, '13 at 9:23a
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase