5

I have the following code:

// eventloop.go
type Object interface {
    ActivateSlot(name string, parameters vector.Vector);
}



// main.go
import loop "./eventloop"

// ...

const slotname = "printer"

type printer struct {
    slot loop.Slot;
}

func (p *printer) Init() {
    p.slot = loop.Slot{slotname, p}; // offending line
}

func (p *printer) ActivateSlot(name string, parameters vector.Vector) {
    fmt.Println("Slot called: ", name); 
}

When I try to compile, I get the following error:

jurily@jurily ~/workspace/go $ ./build.sh
main.go:23: cannot use p (type *printer) as type *eventloop.Object in field value

If I comment the offending line out, it compiles and runs fine. What's happening here? What am I missing?

1 Answer 1

3

Update: This code compiles fine here (all in the same package):

type Object interface {
    ActivateSlot(name string, parameters vector.Vector);
}
type Slot struct {
  name string;
  stuff Object;
}

const slotname = "printer"
type printer struct {
    slot Slot;
}
func (p *printer) Init() {
    p.slot = Slot{slotname, p}; // offending line
}
func (p *printer) ActivateSlot(name string, parameters vector.Vector) {
    fmt.Println("Slot called: ", name);
}

It seems that what you are missing is that *printer is of type Object, and you are trying to assign it to a field of type *Object, which is a different type.

In most cases you would write it like above - without pointers to interface types - but if you have to, you can make it compile like this:

type Slot struct {
  name string;
  stuff *Object;
}
func (p *printer) Init() {
     var o Object = p;
    p.slot = Slot{slotname, &o}; // offending line
}

So p is an Object, you need to take the address of p to match the *Object specification.

Sign up to request clarification or add additional context in comments.

3 Comments

eventloop.Object is an interface. In theory, every type satisfies it which implement the declared function. There is no explicit eventloop.Object type.
That's the Object interface at the top of your code? Yes, if that is defined in package eventloop, and contains just that method, it should work. Have you tried with a cast?
If Object is an interface, did you specify your slot as type *Object? That doesn't work - *printer is type Object, not type *Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.