Less Boilerplate with Auto-Conversion

Writing clean, expressive code is central to the ThingsDB philosophy. With every release, we aim to strip away repetitive administrative code so developers can focus on core application logic and data modeling.

In ThingsDB v1.11.2, we are excited to introduce automatic type conversion and implicit type coercion, drastically cutting down on manual type casting boilerplate across procedures, type definitions, and event handlers.


⚡ The Problem: Type Casting Boilerplate

Before v1.11.2, when working with strict data types (int, float, str, datetime), developers frequently had to write defensive conversion calls like .to_int(), .to_str(), or manual parsing routines whenever data arrived from external APIs, web forms, or JSON payloads:

// Pre-v1.11.2: Manual boilerplate required
new_procedure('update_sensor', |sensor_id_raw, reading_val_raw| {
    // 1. Explicitly convert arguments to matching schema types
    sensor_id = sensor_id_raw.to_int();
    reading_val = reading_val_raw.to_float();

    // 2. Fetch thing and update fields
    s = thing(sensor_id);
    s.last_reading = reading_val;
    s.updated_at = datetime();
});

While explicit typing provides safety, re-writing input conversion code across dozens of procedures created unnecessary friction.


🚀 The Solution: Automatic Type Conversion in v1.11.2

In ThingsDB v1.11.2, the interpreter natively handles coercion when assigning values to typed fields or executing typed procedure parameters.

1. Auto-Conversion on Type Instantiation & Assignment

When creating instances of custom data types, ThingsDB automatically coerces compatible scalar values into the target field schema:

// Define a sensor schema with strict types
set_type('Sensor', {
    id: 'int',
    name: 'str',
    voltage: 'float',
    is_online: 'bool',
});

// v1.11.2: Automatic coercion from incoming raw scalars
s = Sensor{
    id: "1042",         // "1042" (str) -> 1042 (int)
    name: "Gateway 01",
    voltage: "3.28",    // "3.28" (str) -> 3.28 (float)
    is_online: 1,       // 1 (int)      -> true (bool)
};

// All properties are guaranteed to hold their strict target type
assert(is_int(s.id));
assert(is_float(s.voltage));
assert(is_bool(s.is_online));

2. Streamlined Procedure Arguments

Procedure parameters defined with type annotations now automatically perform type validation and coercion at the invocation boundary:

// Type-hinted procedure definition
new_procedure('set_threshold', |device_id: int, limit: float| {
    d = thing(device_id);
    d.threshold = limit;
});

// Calling with string or integer values automatically converts to target types:
set_threshold("501", 42); // Automatically converts "501" -> 501 int, and 42 -> 42.0 float

📈 Key Benefits & Performance Impact

  • Zero Runtime Overhead: Conversions are executed directly within ThingsDB’s optimized C interpreter core during variable bind operations, avoiding secondary interpreter loop passes.
  • Cleaner Codebases: Reduces lines of code in database procedures by up to 30%, leaving clean, readable business logic.
  • Safer External Integrations: External HTTP payloads and client queries can pass stringified JSON values without breaking procedure execution.

Upgrade to the latest ThingsDB release today and experience cleaner, friction-free data modeling!