scope() statements and return

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Oct 3 11:18:58 PDT 2014


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? Better yet:

	int func() {
		scope(exit)
			return 1;
		scope(exit)
			return 2;
		return 0;
	}

Worse yet:

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

And:

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

Or:

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

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("");
		}
	}


T

-- 
Береги платье снову, а здоровье смолоду. 


More information about the Digitalmars-d mailing list