On Wednesday, 28 January 2026 at 09:53:45 UTC, Lars Johansson wrote:
In general programming with D has been a pleasent experience. BUT templates, I do not get it. Instead of having well defined types, it seems you can have anyting with as!.

Consider `java/sql`, querying a `ResultSet`...you'd use `.getInt`, `.getString`, `.getFloat`, `.getDouble`...for `d2sqlite3`, it's `as!int`, `as!string`, `as!float`, `as!double`.


And how do I know I should use e.g. 'as!quirky' in some special case? Or are there a small set of well defined as! along with the types?

From a brief look at the source code, it seems like there are constraints:
```dlang
auto as(T)(T defaultValue = T.init)
        if (isBoolean!T || isNumeric!T || isSomeString!T)
```

This gives you an indication of what kind of values `T` can be.


If you wanted to deal with the whole row, you can also provide your own struct:

```dlang
T as(T)()
        if (is(T == struct))
```

From the unittest:
```dlang
unittest
    {
        struct Item
        {
            int _id;
            string name;
        }

        auto db = Database(":memory:");
        db.run("CREATE TABLE items (name TEXT);
                INSERT INTO items VALUES ('Light bulb')");

auto results = db.execute("SELECT rowid AS id, name FROM items");
        auto row = results.front;
        auto thing = row.as!Item();

        assert(thing == Item(1, "Light bulb"));
    }
```

Generally, I find templates useful, although a little bit of a learning curve.

Jordan

Reply via email to