![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
In addition, Sather offers other runtime safety nets such as assert and invariant.
These safety net is activated by adding the -chk flag when compiled. See sacomp man page for detailed information. It is recommended to activate the safety net when you write a big application as unexpected bugs may lurk in it.
create(n:INT):SAME pre n>=0 is
-- Create a new array of size `n' all of whose elements are void.
return new(n) end;
The following code is a safety version of the point.sa that I've showed previously.
const eta:FLT:= 0.00001; -- acceptable error -- (snip) abs:FLT post result >=0.0 -- check if the length is not negative is return (x*x+y*y).sqrt; end; rotate(theta:FLT):SAME post result.abs/abs > 1.0-eta and result.abs/abs < 1.0+eta -- Check if the difference of the distances from the orign before is -- and after rotation is in the acceptable error. theta := y.atan2(x)+theta; return #SAME(self.abs*theta.cos, self.abs*theta.sin); end;
Following is another safety version of the point.sa.
out2file(a0,a1:ARRAY{POINT}) pre a0.size > 0 and a1.size > 0 -- the size of the array should be positive. is f:FILE:=FILE::open_for_write(fout); assert ~f.error; -- if file open error, terminate p0,p1:POINT; i:INT; loop i:=(a0.size+1).times!; if i = a0.size then i:=0; end; p0:=a0[i]; p1:=a1[i]; f + p0.x + "\t" + p0.y + "\t" + p1.x + "\t" + p1.y + "\n"; end; f.close; end;
$ sacomp -chk point2.sa -o point2
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |