Re: [Vala] Understanding object creation (and other things)



You can do this with construct-only properties without storing the value
anywhere.
...
The construct in the parameter of the creation method is just a short
version of setting the property x to the value of the argument x.

Ok, I think I'm getting some of this.  To add a little complexity
though, say I want to do something like a multiplier that stores the
answer and not the terms.  This means I need two construct parameters
to set one value:

class Mul {
        private int answer;

        public int get_answer() {
                return answer;
        }

        public void set_terms(int x, int y) {
                answer = x * y;
        }

        public Mul(int x, int y) {
                set_terms(x, y)
        }
}

The closest I can get in vala seems to be:

class Mul {
        public int x { get; construct; }
        private int y { get; construct; }

        private int answer;

        public int get_answer() {
                return answer;
        }

        public void set_terms(int x, int y) {
                answer = x * y;
        }

        construct {
                set_terms(x, y);
        }

        public Mul(construct int x, construct int y) {
        }
}

But that keeps the original x and y around for ever, even when the
terms have been changed.  And is a lot of work also.

Is there a way of cutting this back now, or is the syntax likely to
shrink in the future maybe?

--
Phil Housley



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]