From 830671c2210191f69ddc403cc12f5454bb55b0f0 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 21 Feb 2020 10:39:05 -0800 Subject: 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 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. --- tests/compute/this-type.slang | 48 ++++++++++++++++++++++++++++++ tests/compute/this-type.slang.expected.txt | 4 +++ 2 files changed, 52 insertions(+) create mode 100644 tests/compute/this-type.slang create mode 100644 tests/compute/this-type.slang.expected.txt (limited to 'tests') 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 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 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 -- cgit v1.2.3