digitalmars.D - Out parameters and the strong exception guarantee
- Michel Fortin (31/31) Jun 08 2010 The strong exception guarantee guaranties that if an exception is
 - bearophile (6/33) Jun 08 2010 In a recent post here:
 - Justin Johansson (9/46) Jun 09 2010 I re-read that recent post of yours in the context of this new post and
 - Nick Sabalausky (5/11) Jun 09 2010 Existential typing, is that the thing where there's a type that indicate...
 - FeepingCreature (2/4) Jun 13 2010 I think that guarantee is too constraining for too little payoff.
 
The strong exception guarantee guaranties that if an exception is 
thrown, the function will have no side effect. Of course, not all 
function can support this (a file I/O error in the middle of writing 
will have side effects), but often it can and it's generally good 
practice to offer the guaranty whenever possible.
<http://en.wikipedia.org/wiki/Exception_guarantees>
But if one of your function has an 'out' parameter, it's impossible to 
implement the strong guarantee, as illustrated by this trivial example:
	void testOut(out int a) {
		throw new Exception("hello!");
	}
	void main() {
		int a = 2;
		try
			testOut(a);
		finally
			writeln(a);
	}
Prints:
	0
	object.Exception: hello!
This happens because the out parameter gets reset to its default value 
as soon as you enter the function, so you can't throw an exception 
before it has been changed.
So should 'out' be reformed to behave more like a return value? I'm not 
sure. But I think this is something to keep in mind when using out 
parameters.
-- 
Michel Fortin
michel.fortin michelf.com
http://michelf.com/
 Jun 08 2010
Michel Fortin:
 But if one of your function has an 'out' parameter, it's impossible to 
 implement the strong guarantee, as illustrated by this trivial example:
 
 	void testOut(out int a) {
 		throw new Exception("hello!");
 	}
 
 	void main() {
 		int a = 2;
 		try
 			testOut(a);
 		finally
 			writeln(a);
 	}
 
 Prints:
 
 	0
 	object.Exception: hello!
 
 This happens because the out parameter gets reset to its default value 
 as soon as you enter the function, so you can't throw an exception 
 before it has been changed.
 
 So should 'out' be reformed to behave more like a return value? I'm not 
 sure. But I think this is something to keep in mind when using out 
 parameters.
In a recent post here:
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=110908
and in some successive answers I have tried to explain that out arguments are a
hack, they aren't as logically clean as multiple return values. I didn't think
about exceptions too, your example adds one more case to what I was saying
there. Thank you.
Bye,
bearophile
 Jun 08 2010
bearophile wrote:Michel Fortin:I re-read that recent post of yours in the context of this new post and I must say that I agree with you. The ability to return mutiple return values, as a tuple of sorts, is a good idea. Short of this a stronger type system with existential typing (cardinalities) would suffice. Of course that might bring us into a discussion about nullable types which is which is which is which is which is a recursive topic on this ng. Regards JustinBut if one of your function has an 'out' parameter, it's impossible to implement the strong guarantee, as illustrated by this trivial example: void testOut(out int a) { throw new Exception("hello!"); } void main() { int a = 2; try testOut(a); finally writeln(a); } Prints: 0 object.Exception: hello! This happens because the out parameter gets reset to its default value as soon as you enter the function, so you can't throw an exception before it has been changed. So should 'out' be reformed to behave more like a return value? I'm not sure. But I think this is something to keep in mind when using out parameters.In a recent post here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=110908 and in some successive answers I have tried to explain that out arguments are a hack, they aren't as logically clean as multiple return values. I didn't think about exceptions too, your example adds one more case to what I was saying there. Thank you. Bye, bearophile
 Jun 09 2010
"Justin Johansson" <no spam.com> wrote in message news:huo7rk$9ae$1 digitalmars.com...I re-read that recent post of yours in the context of this new post and I must say that I agree with you. The ability to return mutiple return values, as a tuple of sorts, is a good idea. Short of this a stronger type system with existential typing (cardinalities) would suffice.Existential typing, is that the thing where there's a type that indicates "this doesn't return"?Of course that might bring us into a discussion about nullable types which is which is which is which is which is a recursive topic on this ng.He he he :)
 Jun 09 2010
On 08.06.2010 14:28, Michel Fortin wrote:The strong exception guarantee guaranties that if an exception is thrown, the function will have no side effect.I think that guarantee is too constraining for too little payoff.
 Jun 13 2010








 
 
 
 "Nick Sabalausky" <a a.a> 