Subject:
Re: [ruby-ffi] Inheritance and FFI::Struct
From:
Jeremy Voorhis
Date:
8/7/10 2:10 PM
To:
ruby-ffi@googlegroups.com

I would try using composition instead of inheritance in this situation. Class Bar could descend directly from FFI::Struct instead and have its own layout. You could use the Forwardable mixin to declare delegate methods that rely on Bar#p.

Best

Jeremy

On Sat, Aug 7, 2010 at 6:10 AM, Matijs van Zuijlen <matijs@matijs.net> wrote:
Hello all,

I'm trying to set up an inheritance chain that ultimately inherits from FFI::Struct. However, the derived classes have a different layouts. This results in warnings about class variables being overridden.

The following code demonstrates the problem when run with ruby -w:

 require 'ffi'

 class Foo < FFI::Struct
   layout :a, :int, :b, :int
 end

 class Bar < Foo
   layout :p, Foo, :c, :int
 end

 bar = Bar.new

I could set up separate Foo::Struct and Bar::Struct classes to avoid this problem, but I'd rather not, as it introduces a lot of extra complexity.

Any thoughts?

Regards,
--
Matijs