summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-08-13 10:36:55 -0700
committerGitHub <noreply@github.com>2020-08-13 10:36:55 -0700
commit09adf10f646f01e177d412ba2d86602a51579b4f (patch)
tree496e1d155ca49d1b673047cda5c24b797ca07743 /tests
parente1ea7ed2f9b8cfcf19258a05d270de4db03fec22 (diff)
Allow both traditional and modern property syntax (#1487)
The initial change to introduce `property` declarations tied them to a "modern" syntax: property width : float { ... } In practice, a great majority of users assume that properties in Slang will be declared like those in C#: property float height { ... } This change allows both options to parse correctly. The choice made here is to only parse as the "modern" syntax when it can be detected from lookahead (an identifier followed by a `:`), and fall back to the "traditional" syntax otherwise. That choice might not produce the best diagnostic messages around syntax errors in codebases that use the modern syntax, but it is the easiest trade-offs to make. We also add similar disambiguation logic for the `newValue` parameter of a `set` declaration (and other "modern"-style parameters). This strategy cannot be applied to all function parameters in general, because traditional parameter lists can still use `:` to introduce a semantic. Note: the same disambiguation strategy applied here could be used for `let` and `var` declarations: let a : int = 1; let int b = 2; This change does not try to introduce flexibility like that, because it seems unlikely for users to care.
Diffstat (limited to 'tests')
-rw-r--r--tests/language-feature/properties/property-syntax.slang30
1 files changed, 30 insertions, 0 deletions
diff --git a/tests/language-feature/properties/property-syntax.slang b/tests/language-feature/properties/property-syntax.slang
new file mode 100644
index 000000000..41a2513d3
--- /dev/null
+++ b/tests/language-feature/properties/property-syntax.slang
@@ -0,0 +1,30 @@
+// property-syntax.slang
+
+// Confirm that property syntax is parsed correctly.
+
+//TEST:SIMPLE:
+
+struct Data
+{
+ int _a;
+ int _b;
+
+ // Traditional syntax
+ property int a
+ {
+ get { return _a; }
+ set(int newValue) { _a = newValue; }
+ }
+
+ // "Modern" syntax
+ property b : int
+ {
+ get { return _b; }
+ set(newValue : int) { _a = newValue; }
+ }
+}
+
+int test(Data d)
+{
+ return d.a + d.b;
+}