]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Allow read-only properties
authorAlistair Thomas <astavale@yahoo.co.uk>
Mon, 25 Jun 2018 17:57:08 +0000 (18:57 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Wed, 4 Jul 2018 15:43:09 +0000 (17:43 +0200)
See https://gitlab.gnome.org/GNOME/vala/merge_requests/10

tests/Makefile.am
tests/objects/property-read-only-auto.vala [new file with mode: 0644]
vala/valaproperty.vala

index c202f30157bd48d765bc681e245dc21ef470d9c0..9f9d614b3187382715a4f2f095706f50be4f3b1d 100644 (file)
@@ -240,6 +240,7 @@ TESTS = \
        objects/paramspec.vala \
        objects/properties.vala \
        objects/property-notify.vala \
+       objects/property-read-only-auto.vala \
        objects/regex.vala \
        objects/signals.vala \
        objects/signals-delegate.vala \
diff --git a/tests/objects/property-read-only-auto.vala b/tests/objects/property-read-only-auto.vala
new file mode 100644 (file)
index 0000000..e782d43
--- /dev/null
@@ -0,0 +1,26 @@
+class Foo {
+       public int read_only { get; default = 42; }
+}
+
+class Bar : Object {
+       public int read_only { get; default = 23; }
+}
+
+interface IBaz : Object {
+       public abstract int read_only { get; }
+}
+
+class Baz : Object, IBaz {
+       public int read_only { get; default = 4711; }
+}
+
+void main () {
+       var foo = new Foo ();
+       assert (foo.read_only == 42);
+
+       var bar = new Bar ();
+       assert (bar.read_only == 23);
+
+       var baz = new Baz ();
+       assert (baz.read_only == 4711);
+}
index 6e5ae6973cf841e9d15c09e4f75ea0a459467415..aa899f636143a702f9cf7d8f52aa3882a6cbe76a 100644 (file)
@@ -103,17 +103,19 @@ public class Vala.Property : Symbol, Lockable {
                get {
                        if (!_field_checked) {
                                if (!is_abstract && source_type == SourceFileType.SOURCE) {
-                                       bool empty_get = (get_accessor != null && get_accessor.body == null);
-                                       bool empty_set = (set_accessor != null && set_accessor.body == null);
-                                       if (empty_get != empty_set) {
-                                               if (empty_get) {
-                                                       Report.error (source_reference, "Property getter must have a body");
-                                               } else if (empty_set) {
-                                                       Report.error (source_reference, "Property setter must have a body");
-                                               }
+                                       bool has_get = (get_accessor != null);
+                                       bool get_has_body = (has_get && get_accessor.body != null);
+                                       bool has_set = (set_accessor != null);
+                                       bool set_has_body = (has_set && set_accessor.body != null);
+                                       if (set_has_body && (has_get && !get_has_body)) {
+                                               error = true;
+                                               Report.error (source_reference, "Property getter must have a body");
+                                       }
+                                       if (get_has_body && (has_set && !set_has_body)) {
                                                error = true;
+                                               Report.error (source_reference, "Property setter must have a body");
                                        }
-                                       if (empty_get && empty_set) {
+                                       if (!get_has_body && !set_has_body) {
                                                /* automatic property accessor body generation */
                                                _field = new Field ("_%s".printf (name), property_type.copy (), initializer, source_reference);
                                                _field.access = SymbolAccessibility.PRIVATE;