CT foreaches

Nick Treleaven nick at geany.org
Fri Aug 1 15:15:35 UTC 2025


On Friday, 1 August 2025 at 13:51:43 UTC, monkyyy wrote:
> ```d
> import std;
> void foo(int i:0)()=>"foo".writeln;
> void foo(int i:1)()=>"bar".writeln;
> void foo(int i:2)()=>"foobar".writeln;
> void main(){
> 	foreach(i,alias b; AliasSeq!("foo","bar")){
> 		pragma(msg, b.stringof);
> 	        break;
> 	}
> 	pragma(msg,"---");
> 	foreach(i,enum b; AliasSeq!("foo","bar")){
> 		pragma(msg, b.stringof);
> 		break;
> 	}
> 	pragma(msg,"---");
> 	static foreach(i;0..3){
> 		pragma(msg, i.stringof);
> 		//break; //the scope error
> 	}

Up to here is all as expected, right?

> 	pragma(msg,"---");
> 	foreach(enum i;0..3){// ERROR ISNT CT. ISNT AN ENUM
> 		pragma(msg, i.stringof);
> 		//foo!i; //doesnt compile

You're using runtime foreach here. It's only expanded at 
compile-time when the aggregate is a sequence - as the spec says.

The confusing thing is that enum there is silently ignored. I 
think we could add an error for that.

> 		break;
> 	}
> }
> ```
>
> Theres bugs and unintended features here :D ; alias foreach is 
> undocumented even in the template book;

That hasn't been updated in 8 years.

> the dip 1010 code block that *may* be why this behavior exists 
> doesnt actaully work, the docs were wrong a week ago
>
> etc. etc.
>
> currently I believe:
>
> a) static makes it a block, makes enum or alias infered
> b) enum or alias of aliasSeq, whoever made thought there be an 
> upgrade to fully generalize the syntax

Not sure what you mean for (b).

> c) the dip 1010 section was ambiguous if it was real causing 
> enum to be ignored
>
> anyone got any code blocks?




More information about the Digitalmars-d mailing list