@safe quiz
Michel Fortin
michel.fortin at michelf.com
Wed Dec 30 20:48:40 PST 2009
On 2009-12-30 23:41:54 -0500, Michel Fortin <michel.fortin at michelf.com> said:
> Here is a pathetic example of something that does not work currently:
>
> struct R {
> @safe int opApply(int delegate(ref int) z) {
> int i = 1;
> return z(i); // Error: safe function 'opApply' cannot call system
> delegate 'z'
> }
> }
>
> @system
> void someSystemFunction() {
> R r;
> foreach (i; r) { writeln(i); }
> }
>
> Should I have to write the opApply twice so it can work with both
> system and safe functions? I sure hope not. Even then, this does not
> compile either:
I should add that I can write this to move the error somewhere else
which is more illustrative of my point by tagging the argument as a
@safe delegate:
struct R {
@safe int opApply(int delegate(ref int) @safe z) {
int i = 1;
return z(i);
}
}
@system
void someSystemFunction() {
R r;
foreach (i; r) { writeln(i); } // Error: function untitled.R.opApply
(int delegate(ref int) z) is not callable using argument types (int
delegate(ref int))
}
This version of opApply gives no error if the caller is @safe:
@safe
void someSafeFunction() {
R r;
foreach (i; r) { writeln(i); }
}
(Except about writeln not being safe, but that's not a language issue.)
--
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/
More information about the Digitalmars-d
mailing list