Type inspection
Inspect concrete runtime types and homogeneous scalar, array or Tensor storage datatypes without guessing mixed collection contents.
Aner provides import free built ins to inspect a value's concrete runtime type and its scalar or homogeneous element storage datatype. Inspection returns a String name; it does not convert the value, narrow its static type, or traverse collection contents.
Concrete runtime type
type(value) and type_of(value) are equivalent. They report the concrete runtime type retained inside Any, including a user defined class specialization or a typed array specialization. Null is reported as "null". Unit is not an Any payload and cannot be passed to either built in.
let values = List(10, "sample", true, null)
print(type(values)) // List
print(type(values.get(0))) // Int64
print(type_of(values.get(1))) // String
print(type(null)) // nullThe result is a name, rather than a type object. It cannot be supplied as a dynamic type argument or used to call a constructor by name. Runtime type inspection does not enable member access on an Any expression. Use a checked cast when a concrete static type is required:
let value: Any = " sample "
if type(value) == "String" {
print(cast<String>(value).trim())
}cast<Type>(value) verifies the concrete type and does not perform a numeric or text conversion. A mismatch reports the existing cast diagnostic R1002.
Element storage datatype
dtype(value) reports the scalar datatype of a supported scalar, the declared homogeneous element datatype of a typed array, or the actual storage precision of a Tensor. A heterogeneous core collection reports "Any" even if its current elements all happen to have one type. The result is independent of collection contents and length.
| Value | type(value) | dtype(value) |
|---|---|---|
42 | Int64 | Int64 |
3.5 | Float64 | Float64 |
true | Bool | Bool |
"sample" | String | String |
List(1, 2) | List | Any |
| An empty Dictionary | Dictionary | Any |
Set("a", "b") | Set | Any |
Array<Int64>(2, 0) | Array<Int64> | Int64 |
DynamicArray<Float64>(4) | DynamicArray<Float64> | Float64 |
NDArray<Bool>(List(2, 3), false) | NDArray<Bool> | Bool |
Bytes.from_hex("ff") | Bytes | UInt8 |
Range(3) | Range | Int64 |
| A Float32 Tensor | Tensor | Float32 |
| A Float64 Tensor | Tensor | Float64 |
let numbers = DynamicArray<Float64>(capacity: 4)
numbers.append(1.5)
print(type(numbers)) // DynamicArray<Float64>
print(dtype(numbers)) // Float64
print(dtype(List(1, 2))) // AnyFor a Tensor, dtype reports actual storage precision after any explicit transfer or configured execution policy. It does not report a requested precision, a compute accumulation precision, a device, or an inferred class label type. Native Tensor dtype accessors and typed array .dtype() methods remain available.
dtype accepts an Any argument so it can inspect dynamically stored supported values. Null, user defined objects, and opaque native values without a homogeneous scalar storage datatype produce R3202. Unit is rejected by the checker. dtype never guesses a schema for a Dictionary, a neural network, a Dataset, or a credential reference. Calendar values, Duration, Optional, and Result<T, E> have no element storage datatype; use type for their concrete native type or generic specialization. Bytes reports UInt8 for its unsigned byte storage, and Range reports Int64 for its arithmetic sequence. UInt8 is not a new scalar or accepted Array dtype.
Signatures and complexity
| Built in | Result | Time | Additional space |
|---|---|---|---|
type(value: Any) | String | O(t), for t bytes in the returned type name | O(t) result. |
type_of(value: Any) | String | O(t), identical to type | O(t) result. |
dtype(value: Any) | String | O(1), metadata or tag inspection | O(1) bounded datatype name. |
These bounds describe inspection after argument evaluation. Current interpreter evaluation can copy owned scalar or native payloads according to their existing value semantics. Inspection itself does not scan values, materialize a Tensor, transfer device memory, or change an object.
See the String reference, typed arrays, and Tensor reference for their respective storage and operation contracts. Run the type inspection example to observe mixed values and homogeneous storage.