Learn R Programming

sicher (version 0.1.0)

check_type: Type Checking Function

Description

Validates that a value conforms to a specified type. This is the core validation function used internally by the type system, but can also be called directly for manual type checking.

Usage

check_type(value, type, context = NULL)

Value

Returns `TRUE` invisibly if the value matches the type, otherwise throws an error with a descriptive message.

Arguments

value

The value to check

type

A sicher_type, sicher_union, or sicher_readonly object

context

Optional character string describing where the check is occurring (used in error messages)

Details

This function:

  • Checks if a value matches a type specification

  • Handles union types (checks if value matches any type in the union)

  • Handles readonly types (strips the readonly modifier before checking)

  • Provides detailed error messages when checks fail

See Also

create_type for creating custom types

Examples

Run this code
# Direct type checking
check_type(5L, Integer)  # Returns TRUE
try(check_type("hello", Integer))  # Throws error

# With context for better error messages
try(check_type(5L, String, context = "user_name"))

# With union types
check_type(5L, Integer | String)  # Returns TRUE
try(check_type(5.5, Integer | String))  # Throws error

Run the code above in your browser using DataLab