Improvements to switch

Basile B. b2.temp at gmx.com
Sat Apr 27 23:58:43 UTC 2024


On Saturday, 27 April 2024 at 14:58:19 UTC, IchorDev wrote:
> On Tuesday, 16 April 2024 at 16:00:48 UTC, Basile B. wrote:
>> About this, the main point is rather
>>
>> ```d
>> /*-->*/ const /*<--*/ int myvalue = switch(code) {
>>     // ...
>> };
>> ```
>>
>> "ah finally you can define a const var decl that relies on 
>> branching" (without using the conditional expression...)
>
> You can already do that.
> ```d
> const string myValue = (){
> 	switch(code){
> 		case 1:    return "what";
> 		case 2, 3: return "ok";
> 		default:   return "no";
> 	}
> }();

Sure but the matching AST is unnecessarily complex. A function 
literal, plenty of return statements, at least 1 capture. 
Consequently the path borrowed by the compiler is much more 
complex, it has to do things that would not be done with the 
expression: return type inference, block exits, etc. Then without 
optimizations enabled that does not have the equivalent runtime 
performances.



More information about the dip.ideas mailing list