Type inspection
Inspect concrete runtime types and homogeneous scalar, array or Tensor storage datatypes without guessing mixed collection contents.
ImplementedAner 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, including a user defined class specialization or a typed array specialization retained inside Any. A Number value reports its retained integer type, Float32, or Float64 representation. 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. cast<Number>(value) accepts any supported integer type, Float32, or Float64 while retaining its concrete representation; cast<Int8> through cast<UInt64>, and cast<Float32>/cast<Float64>, require their exact tags. A mismatch reports the existing cast diagnostic R1002. See Number for checked automatic promotion and explicit numeric constructors.
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 |
UInt8(255) | UInt8 | UInt8 |
Int16(-200) | Int16 | Int16 |
UInt64(18446744073709551615) | UInt64 | UInt64 |
Float32(3.5) | Float32 | Float32 |
3.5 | Float64 | Float64 |
Number(42) | Int64 | Int64 |
Number(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 |
Array<UInt8>(2, 0) | Array<UInt8> | UInt8 |
Array<Float32>(2, 0.5) | Array<Float32> | Float32 |
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 |
Array<Quantity<m>>(2, 1.0@m) after importing aner.units | Array<Quantity<m>> | Float64 |
DynamicArray<Point<degC>>(4) after importing aner.units | DynamicArray<Point<degC>> | Float64 |
NDArray<Quantity<"m/s">>(List(2, 3), 1.0@"m/s") after importing aner.units | NDArray<Quantity<"m/s">> | Float64 |
| 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 also a supported scalar and packed array dtype; see fixed width integers. Bytes retains its existing accessor signatures.
Nested physical unit types
Aner 0.0.4 or newer retains a unit array's full nested type while its magnitude storage remains Float64. Import aner.units to declare these element types; inspect the unit separately through .unit():
import aner.units
let lengths = Array<Quantity<m>>(size: 2, fill: 1.0@m)
let dynamic: Any = lengths
print(type(dynamic))
print(dtype(dynamic))
let shaped = cast<Array<Quantity<m>>>(dynamic).reshape(List(1, 2))
print(type(shaped))
print(shaped.unit())
print(dtype(shaped))This prints Array<Quantity<m>>, Float64, NDArray<Quantity<m>>, m and Float64. A cast checks the exact retained family, quantity/point kind and unit. Casting meters to Array<Quantity<cm>> fails with R1002; use a typed array's literal .to("cm") conversion instead. dtype does not drop physical units. An explicit .values() export produces independent primitive Float64 storage without that checked unit. See unit bearing arrays for conversion, ownership and numerical contracts.
Unit bearing table columns
A DataTable is an opaque composite rather than one homogeneous array: type(table) reports DataTable, while dtype(table) remains unsupported. Inspect each column with data.column_type(table, index) and data.column_unit(table, index) instead. A declared Fahrenheit temperature column reports Point<degF> and degF; after ingestion normalization to Celsius, its metadata reports Point<degC> and degC. Primitive columns have an empty unit code.
A literal typed accessor selects its output unit explicitly. For example, type(table.unit_values("temperature_f", "degC")) is Array<Point<degC>>, and its dtype is Float64. This does not change the table's descriptor; the extracted array is independent. The CSV unit guide supplies the complete schema/fixture and explains missing value requirements and deliberate numerical export.
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.