• Slang Standard Library Reference
    • Interfaces
      • IArithmetic
      • IArithmeticAtomicable
      • IArray
        • getCount
        • subscript
      • IAtomicable
      • IBitAtomicable
      • IBufferDataLayout
      • IComparable
      • IDefaultInitializable
      • IDiffTensorWrapper
      • IDifferentiable
      • IDifferentiableFunc
      • IDifferentiableMutatingFunc
      • IDifferentiablePtrType
      • IFloat
      • IFunc
      • IInteger
      • ILogical
      • IMutatingFunc
      • IOpaqueDescriptor
      • IPhysicalBuffer
      • IRWArray
      • IRWPhysicalBuffer
      • IRangedValue
      • ITexelElement
      • __BuiltinArithmeticType
      • __BuiltinFloatingPointType
      • __BuiltinIntegerType
      • __BuiltinLogicalType
      • __ITextureShape
      • __ITextureShape1D2D3D
    • Types
    • Attributes
    • Global Declarations

interface IArray<T>

Description

Represents types that provide a subscript operator so that they can be used like an immutable array.

Generic Parameters

T

The element type returned by the subscript operator.

Methods

Remarks

This interface is implemented by Array, vector, matrix, StructuredBuffer and RWStructuredBuffer types.

Example

The follow example shows how to define a generic function that computes the sum of all elements in an array-like type.

T sum<T:IFloat, U:IArray<T>>(U array)
{
    T result = T(0);
    for (int i = 0; i < array.getCount(); i++)
    {
        result = result + array[i];
    }
    return result;
}

RWStructuredBuffer<float> outputBuffer;

[numthreads(1, 1, 1)]
void computeMain(int3 dispatchThreadID : SV_DispatchThreadID)
{
    float arr[3] = { 1.0, 2.0, 3.0 };
    float4 v = float4(1.0, 2.0, 3.0, 4.0);
    float2x2 m = float2x2(1.0, 2.0, 3.0, 4.0);

    outputBuffer[0] = sum(arr);  // 6.0

    // treat `v` as array of `float`.
    outputBuffer[1] = sum(v);    // 10.0

    // treat `m` as array of `float2`.
    float2 innerSum = sum(m); 
    outputBuffer[2] = sum(innerSum); // 10.0
}