After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 606482 - Constructor chain-up in structs
Constructor chain-up in structs
Status: RESOLVED FIXED
Product: vala
Classification: Core
Component: Structs
unspecified
Other All
: Normal blocker
: ---
Assigned To: Vala maintainers
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2010-01-09 12:23 UTC by pancake
Modified: 2010-01-29 16:37 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Class construct example (329 bytes, application/octet-stream)
2010-01-09 12:24 UTC, pancake
  Details
Test construction example (345 bytes, application/octet-stream)
2010-01-09 12:24 UTC, pancake
  Details
Allow chaining struct initialization/ctor (4.01 KB, patch)
2010-01-24 00:14 UTC, Marc-Andre Lureau
none Details | Review
Allow chaining struct initialization/ctor (3.59 KB, patch)
2010-01-24 00:15 UTC, Marc-Andre Lureau
none Details | Review
Let `this' be a creation method call on struct as well (4.66 KB, patch)
2010-01-25 20:31 UTC, Marc-Andre Lureau
none Details | Review
Let `this' be a creation method call on struct as well (3.73 KB, patch)
2010-01-25 20:34 UTC, Marc-Andre Lureau
none Details | Review

Description pancake 2010-01-09 12:23:49 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)
Comment 1 pancake 2010-01-09 12:24:29 UTC
Created attachment 151087 [details]
Class construct example
Comment 2 pancake 2010-01-09 12:24:47 UTC
Created attachment 151088 [details]
Test construction example
Comment 3 Marc-Andre Lureau 2010-01-24 00:14:42 UTC
Created attachment 152118 [details] [review]
Allow chaining struct initialization/ctor

Fixes bug 606482.
Comment 4 Marc-Andre Lureau 2010-01-24 00:15:41 UTC
Created attachment 152119 [details] [review]
Allow chaining struct initialization/ctor

Fixes bug 606482.
Comment 5 pancake 2010-01-25 12:47:07 UTC
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 ());
}
Comment 6 Marc-Andre Lureau 2010-01-25 20:31:50 UTC
Created attachment 152255 [details] [review]
Let `this' be a creation method call on struct as well

This finally compile test case in bug 606482.
Comment 7 Marc-Andre Lureau 2010-01-25 20:34:21 UTC
Created attachment 152256 [details] [review]
Let `this' be a creation method call on struct as well

This finally compile test case in bug 606482.
Comment 8 Jürg Billeter 2010-01-29 15:20:37 UTC
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.
Comment 9 Jürg Billeter 2010-01-29 15:21:11 UTC
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.
Comment 10 Marc-Andre Lureau 2010-01-29 15:44:26 UTC
hmm, so you drop the first patch or is it merged in this commit?
Comment 11 Jürg Billeter 2010-01-29 16:00:40 UTC
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?
Comment 12 Marc-Andre Lureau 2010-01-29 16:27:35 UTC
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 :)
Comment 13 Jürg Billeter 2010-01-29 16:37:52 UTC
This should be

A () {
  this.base ();
}

This works with classes but doesn't seem to work yet for structs.