Amusing D facts: typesafe variadic arrays are lazy!

Leandro Lucarella llucax at gmail.com
Tue Oct 13 12:14:34 PDT 2009


Andrei Alexandrescu, el 13 de octubre a las 10:30 me escribiste:
> downs wrote:
> >Did you know the following code compiles?
> >
> >>module test;
> >>
> >>import std.stdio;
> >>
> >>void Assert(bool cond, string delegate()[] dgs...) {
> >>  debug if (!cond) {
> >>    string str;
> >>    foreach (dg; dgs) str ~= dg();
> >>    throw new Exception(str);
> >>  }
> >>}
> >>
> >>void main() {
> >>  Assert(false, "O hai thar! ");
> >>}
> >
> >
> >It's true! :)
> 
> Gosh!!! What's happening over here? I even tried this:
> 
> import std.stdio;
> 
> void Assert(bool cond, string delegate()[] dgs...) {
>     if (!cond) {
>         string str;
>         foreach (dg; dgs) str ~= dg();
>         throw new Exception(str);
>     }
> }
> 
> string fun(string a, string b) {
>     writeln("Concatenating...");
>     return a ~ b;
> }
> 
> void main() {
>     Assert(true, fun("O hai thar! ", "wyda"));
>     Assert(false, fun("O hai thar! ", "wyda"));
> }
> 
> This example only prints "Concatenatning..." once, meaning that fun
> is also lazified!!!
> 
> This is very exciting! The fact that this little anomaly hasn't
> caused trouble is a good sign it could actually replace lazy!

What is the relation between assert and pure/nothrow? Is assert allowed?
I think it should be, since an assert is expressing a very essential
property of the software, it can't happen in a normal flow of the program.

If this is the case, I guess assert should be kept as a language construct
so it can be always be used in pure/nothrow functions.

-- 
Leandro Lucarella (AKA luca)                      http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
En la calle me crucé con un señor muy correcto, que habitualmente anda en
Falcon; iba corriendo con dos valijas en la mano y dijo: "Voy para Miami,
tiene algún mensaje o ..." y le dije: "No, no, no..."
	-- Extra Tato (1983, Triunfo de Alfonsín)



More information about the Digitalmars-d mailing list