summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorTim Foley <tfoleyNV@users.noreply.github.com>2020-02-21 10:39:05 -0800
committerGitHub <noreply@github.com>2020-02-21 10:39:05 -0800
commit830671c2210191f69ddc403cc12f5454bb55b0f0 (patch)
treebc242d34920715c33c827f4071d1ca8a1994a6b6 /tests
parent433ce869481b72ad44897dcc91d7038b03ba45e2 (diff)
Add surface syntax for "this type" (#1236)
Within the context of an aggregate type (or an `extension` of one), the programmer can use `this` to refer to the "current" instance of the surrounding type, but there is no easy way to utter the name of the type itself. This is especially relevant inside of an `interface`, where the type of `this` isn't actually the `interface` type, but rather a placeholder for the as-yet-unknown concrete type that will implement the interface. This change adds a keyword `This` that works similarly to `this`, but names the current *type* instead of the current instance. It can be used to declare things like binary methods or factory functions in an interface: ``` interface IBasicMathType { This absoluteValue(); This sumWith(This left); } T doSomeMath<T:IBasicMathType>(T value) { return value.sumWith(value.absoluteValue()); } ``` The `This` type is consistent with the type named `Self` in Rust and Swift (where Rust/Swift use `self` instead of `this`). Other names could be considered (e.g., `ThisType`) if we find that users don't like the name in this change.
Diffstat (limited to 'tests')
-rw-r--r--tests/compute/this-type.slang48
-rw-r--r--tests/compute/this-type.slang.expected.txt4
2 files changed, 52 insertions, 0 deletions
diff --git a/tests/compute/this-type.slang b/tests/compute/this-type.slang
new file mode 100644
index 000000000..551fd0244
--- /dev/null
+++ b/tests/compute/this-type.slang
@@ -0,0 +1,48 @@
+// this-type.slang
+
+// Confirm that that `This` type works as expected
+
+//TEST(compute):COMPARE_COMPUTE:
+//TEST(compute):COMPARE_COMPUTE:-cpu
+
+interface IFrobable
+{
+ This frob();
+ int getValue();
+}
+
+struct Thing : IFrobable
+{
+ int value;
+
+ Thing frob()
+ {
+ Thing result = { value * 16 };
+ return result;
+ }
+
+ int getValue() { return value; }
+}
+
+int frobnicate<F:IFrobable>(F f)
+{
+ return f.frob().getValue();
+}
+
+int test(int value)
+{
+ Thing t = { value };
+ return frobnicate(t) + value;
+}
+
+//TEST_INPUT:ubuffer(data=[0 1 2 3], stride=4):out,name=outputBuffer
+RWStructuredBuffer<int> outputBuffer : register(u0);
+
+[numthreads(4, 1, 1)]
+void computeMain(uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ uint tid = dispatchThreadID.x;
+ int inVal = outputBuffer[tid];
+ int outVal = test(inVal);
+ outputBuffer[tid] = outVal;
+} \ No newline at end of file
diff --git a/tests/compute/this-type.slang.expected.txt b/tests/compute/this-type.slang.expected.txt
new file mode 100644
index 000000000..d4cb1cc00
--- /dev/null
+++ b/tests/compute/this-type.slang.expected.txt
@@ -0,0 +1,4 @@
+0
+11
+22
+33