GNOME Bugzilla – Bug 540661
GLib.MemoryInputStream.from_data() returns InputStream
Last modified: 2008-07-09 09:18:36 UTC
The underlying C function g_memory_input_stream_new_from_data returns a GInputStream. However the bindings assume that it returns GMemoryInputStream causing warnings when building: /* * valac --pkg gio-2.0 test.vala */ using GLib; public class Test : GLib.Object { public static void on_destroy(void *data) { // Needed to allow compilation } public static void main(string[] args) { MemoryInputStream input = new MemoryInputStream.from_data ("test", 4, on_destroy); } }
Created attachment 113591 [details] [review] possible idea of how to fix Problem is that the constructors return different types than the classes that they are supposed to construct. Solution - for methods that are "constructor" in the .gi, but the return type is different from the enclosing object, vapigen should make that method a static factory method rather than a constructor. Attached is a basic idea of one way to fix this. There is probably a better way to check that the types are the same that doesn't rely on getting the class' cname, but I'm not sufficiently familiar with the type/symbol code. Note that this change to Vapigen would probably require regeneration of more than just the gio bindings.
Thanks for the patch, however, I don't think this is the right way to fix this. Semantically, it's a normal constructor. The only reason it returns a base class is to make it more convenient to use in C. I have an experimental patch in a local branch to support this: [CCode (type = "GInputStream*")] public MemoryInputStream () { ... } The same applies to Gtk Widgets.
You're right, doing it the way I suggest is stupid. Since the real problem is just the warning in the C compilation, why not just treat it as a normal constructor, and then just put a cast in the C code? e.g. GMemoryInputStream* _tmp0 = (GMemoryInputStream*) g_memory_input_stream_new_from_data (blah); Since we know for sure that the constructor will definitely return the type that we want.
Created attachment 113671 [details] [review] use cast to prevent warning in C code
Fixed in SVN trunk revision 1688. 2008-07-09 Raffaele Sandrini <raffaele@sandrini.ch> * gobject/valaccodegenerator.vala: * gobject/valaccodemethodbinding.vala: * vala/valacreationmethod.vala: * vala/valainterfacewriter.vala: * vapigen/valagidlparser.vala: Add support for creation methods with a different (mostly super) type than the type of the object it creates. Add a `type' parameter to the CCode attribute which will be used to get the casts right for such creation methods. * vapi/gio-2.0.vapi: * vapi/gtk+-2.0.vapi: Regenerated, fixes bug 540661