Worst ideas/features in programming languages?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Nov 15 21:23:56 UTC 2021


On Mon, Nov 15, 2021 at 09:10:51PM +0000, kdevel via Digitalmars-d wrote:
> On Monday, 15 November 2021 at 17:48:35 UTC, H. S. Teoh wrote:
> [...]
> > Why can't you just write:
> > 
> > 	MyAbility ability;
> > 
> > 	switch (ability) with(MyAbility)
> > 	{
> > 	 case SOMETHING_1: break;
> > 	 case SOMETHING_2: break;
> > 	 case SOMETHING_3: break;
> > 	 case SOMETHING_4: break;
> > 	 case SOMETHING_5: break;
> > 	}
> > 
> > ?  The `with` keyword was designed specifically for this purpose.
> 
> Nice. What about
> 
>    import std.stdio;
>    enum SAB {
>       a = 1,
>    }
>    void main ()
>    {
>       with (SAB) if (a == 1) writeln ("true");
>       with (SAB) void foo () { writeln (a); } // no complaints!
>       foo(); // Error: undefined identifier `foo`
>    }
[...]

That's because `with` introduces a scope.  So you should have written
instead:

	void foo () { with (SAB) writeln (a); }

Or, for that matter:

	void main ()
	{
	   with (SAB) {
	   	if (a == 1) writeln ("true");
	   	void foo () { writeln (a); }
	   	foo(); // Now this works
	   }
	}


T

-- 
Being able to learn is a great learning; being able to unlearn is a greater learning.


More information about the Digitalmars-d mailing list