GNOME Bugzilla – Bug 610083
SimpleType code generation broken
Last modified: 2018-05-22 13:28:52 UTC
Hi, the code generation for SimpleTypes is broken. Take as example the following: [SimpleType] [Immutable] [CCode (copy_function = "fraction_copy")] struct Fraction { public int num; public int den; public Fraction copy () { return Fraction (num, den); } public Fraction (int num, int den) { this.num = num; this.den = den; } public Fraction mult (Fraction other) { return Fraction (num * other.num, den * other.den); } } Some errors in the generated code are: void fraction_init (Fraction self, gint num, gint den): This initializes self, but as self is passed by value this has absolutely no effect. It should either be passed by reference for that function or should be returned. Code that uses fraction_init() expect that it returns the initialized instance and that it only takes the num/den parameters and not the self parameter. In fraction_init(), self is simply passed to memset while the address of it should be passed. I'll try to create a patch for this to work later.
So, how should the init function look like? void fraction_init (Fraction *f, gint num, gint den); or Fraction fraction_init (gint num, gint den); The latter should probably be called fraction_new() though.
Perhaps you're looking at it from the wrong side. Should SimpleType have any init function at all? For me, it doesn't make much sense. Why use SimpleType anyway? It's for bindings. I don't see any advantages.
(In reply to comment #2) > Perhaps you're looking at it from the wrong side. Should SimpleType have any > init function at all? For me, it doesn't make much sense. Well, in my example it makes sense, no? :) > Why use SimpleType anyway? It's for bindings. I don't see any advantages. Because instances are passed by value. Normal structs are passed by reference.
Jürg said on IRC, that he would probably prefer Fraction fraction(gint num, gint den) (other constructors would be Fraction fraction_from_double (gint num, gint den) then for example). This is similar to time_t time() then.
A construction function in this case should work like with classes, not like struct init as slomo said, and it makes sense to me.
Created attachment 211043 [details] [review] Fix for SimpleType creation method code generation I ran into this bug too, and coded a fix for it.
Ping? There's a patch now :)
*** Bug 741466 has been marked as a duplicate of this bug. ***
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/vala/issues/72.