scope() statements and return

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 3 12:35:31 PDT 2014


On 10/3/14, 11:18 AM, H. S. Teoh via Digitalmars-d wrote:
> On Fri, Oct 03, 2014 at 10:50:39AM -0700, Andrei Alexandrescu via Digitalmars-d wrote:
>> On 10/2/14, 8:23 PM, Ali Çehreli wrote:
>>> "A scope(exit) or scope(success) statement may not exit with a throw,
>>> goto, break, continue, or return; nor may it be entered with a goto."
>>
>> Seems to me all these restrictions should be lifted. -- Andrei
>
> Please don't. It will lead to pathological behaviour. For example:
>
> 	int func() {
> 		scope(exit)
> 			return 1;
> 		return 0;
> 	}
>
> What does func() return?

1

> Better yet:
>
> 	int func() {
> 		scope(exit)
> 			return 1;
> 		scope(exit)
> 			return 2;
> 		return 0;
> 	}

2

> Worse yet:
>
> 	// What does this function do? What *should* it do??
> 	int func() {
> 		scope(success)
> 			throw new Exception("");
> 		scope(failure)
> 			return 1;
> 		return 0;
> 	}

1

> And:
>
> 	int func() {
> 		hahaha: scope(exit) goto hahaha;
> 		return 1;
> 	}

infinite loop

> Or:
>
> 	int func() {
> 		foreach (i; 0..10) {
> 			scope(exit) break;
> 			scope(exit) continue;
> 			return i; // hahahahahaha
> 		}
> 		return i;
> 	}

compile error

> And do we really want to get into this one:
>
> 	struct S {
> 		void opApply(scope void delegate(int) loopBody) {
> 			foreach (i; 0..10) {
> 				scope(success) continue;
> 				auto rc = loopBody(i);
> 				if (rc != 0)
> 					return rc; // ORLY?!
> 			}
> 		}
> 	}
> 	void main() {
> 		foreach (i; S.init) {
> 			scope(failure) continue; // what does this do?
> 			throw new Exception("");
> 		}
> 	}

I guess I'm convinced it adds more complication than expressiveness!


Andrei



More information about the Digitalmars-d mailing list