GNOME Bugzilla – Bug 606482
Constructor chain-up in structs
Last modified: 2010-01-29 16:37:52 UTC
The following examples are constructing an object and a struct which the constructor calls to another constructor. The problem here is that in classes you can call 'this (x)' to run another constructor from the class constructor, but this is not supported by structs. The given code is wrong, because it constructs an unused struct in the constructor (Foo (3)) but doesnt calls the constructor of the struct passing the struct reference which results on no warning for this invalid code and no possibility to do it in structs (only works for classes)
Created attachment 151087 [details] Class construct example
Created attachment 151088 [details] Test construction example
Created attachment 152118 [details] [review] Allow chaining struct initialization/ctor Fixes bug 606482.
Created attachment 152119 [details] [review] Allow chaining struct initialization/ctor Fixes bug 606482.
This patch doesnt works for me. This is the example test case for structs. Can you check if there's something wrong here? /* This example works with class. but not struct */ public struct Foo { private int pwn; public Foo (int bar) { pwn = bar; } public Foo.with_bar () { this (3); } public string to_string () { return "pwn is %d\n".printf (pwn); } } void main () { var foo = Foo.with_bar (); print ("%s\n", foo.to_string ()); }
Created attachment 152255 [details] [review] Let `this' be a creation method call on struct as well This finally compile test case in bug 606482.
Created attachment 152256 [details] [review] Let `this' be a creation method call on struct as well This finally compile test case in bug 606482.
Only `this` and `base` should be supported here, so we can move the implementation a bit closer to what we do for classes. I'll adapt the patch.
commit e42d65d7856ab1f9ab048daf5749951f7d1c9f20 Author: Jürg Billeter <j@bitron.ch> Date: Fri Jan 29 16:16:52 2010 +0100 Support chaining up to constructors in structs Based on patch by Marc-André Lureau, fixes bug 606482.
hmm, so you drop the first patch or is it merged in this commit?
It's merged but most of it wasn't needed anymore, as I don't convert the method call to an object creation expression. That's consistent in how we handle the class case. Do you see any issues with that approach?
I remember the first patch address a situation like this: struct A { A.base () { } A () { A.base (); } } Was that a bad idea? Yeah, perhaps it's a bad idea. I did that, because for some reason, that's the test case I had :)
This should be A () { this.base (); } This works with classes but doesn't seem to work yet for structs.