Learn R Programming

sicher (version 0.1.0)

infer_type: Infer a Type from an R Object

Description

Infers the most appropriate sicher type constructor for a given R object. By default, inference focuses on the underlying type and does not lock in the observed length of vectors. Set `strict = TRUE` to also infer scalar and fixed-size vector constraints from the example value.

Usage

infer_type(obj, strict = FALSE)

Value

A sicher_type object (e.g., Numeric, String, create_list_type(...), ListOf(...), etc.)

Arguments

obj

Any R object (primitive, vector, list, data.frame, function, etc.)

strict

Logical scalar. When `FALSE` (default), infer only the base type shape, such as `Numeric`, `String`, `ListOf(Integer)`, or a `create_dataframe_type()` schema without fixed lengths. When `TRUE`, also infer `Scalar()` and `[n]` size constraints from the observed object.

Examples

Run this code
infer_type(42L)                # Integer
infer_type(3.14)               # Double
infer_type(c(1, 2, 3))         # Double or Numeric, no length constraint
infer_type("abc")              # String
infer_type(c("a", "b"))        # String
infer_type(TRUE)               # Bool
infer_type(NULL)               # Null
infer_type(function(x) x + 1)  # Function
infer_type(list(a = 1, b = "x"))   # create_list_type(list(a = Double, b = String))
infer_type(list(1, 2, 3))           # ListOf(Double)
infer_type(data.frame(x = 1:3))     # create_dataframe_type(list(x = Integer))
infer_type(list(a = NULL, b = 1))   # create_list_type(list(a = Optional(Any), b = Double))

# Strict mode keeps observed length constraints
infer_type(42L, strict = TRUE)          # Scalar(Integer)
infer_type(c("a", "b"), strict = TRUE)  # String[2]

Run the code above in your browser using DataLab