Discussing exception translation with GPT-5 mini: lazy void parameters

Forum User forumuser at example.com
Tue Apr 7 19:47:04 UTC 2026


After having a chat with GPT-5 mini (via duck.ai) about exception 
translation the AI came up with some strange examples. I then 
thought it would be a good idea if I tell the AI something new:

Well you can use UFCS to append the exceptionTranslator with a 
lazy argument this way:

```d
T translate (Ein, Eout, T) (lazy T t)
{
     try return t;
     catch (Ein e) throw new Eout (e.msg);
}
```

Then the AI tries to convince me that I need two template 
variants:

Yes — that's a neat, idiomatic D solution. A few concise 
suggestions to make it robust and idiomatic:

* Constrain template parameters to exception types and preserve 
ref/return semantics:

```d
T translate(Ein, Eout, T)(lazy T t)
if (is(Ein : Exception) && is(Eout : Exception) && !is(T == void))
{
     try return t;
     catch (Ein e) { throw new Eout(e.msg); }
}
```


*    Support void returns:

```d
void translate(Ein, Eout)(lazy void delegate() t)
if (is(Ein : Exception) && is(Eout : Exception))
{
     try { t(); return; }
     catch (Ein e) { throw new Eout(e.msg); }
}
```

[and even more code ...]

Only after insisting that the unified version is valid, compiles 
and runs as expected the AI abondons the field (but that means 
nothing) stating:

Agreed — if your implementation uses `lazy T t` and compiles/runs 
for `T = void` you don't need separate overloads. D allows `lazy 
void` and you can write a single template without static if by 
invoking `t` directly in a try/catch and either returning its 
value or returning nothing:

Example minimal unified form: [...]

This compiles because `return t;` is valid when `t` is a lazy 
void expression (it acts like `t(); return;`). Your approach is 
idiomatic and concise.

Is she right?


More information about the Digitalmars-d mailing list