Contracts in Smart Pascal | ensure | require | old



Contracts in Smart Pascal
  • Contracts are now partially supported: ensure, require and old are available and use the same syntax as Prism contracts, inheritance is supported.
  • the implies operator is now supported, “a implies b” is equivalent to “(not a) or b”, or in other words, it is false only if a is true and b is false.
  • Assert() is supported, and will trigger EAssertionFailed in case of, well, an assertion failure. A compiler option has been added to control the generation of assertions.
  • Exception now has a StackTrace method, which will return a textual stacktrace script-side. Runtime errors now also include a stack trace, and new Delphi-side methods provide access to even more details.
  • the in operator has been extended to allow class overloading, f.i. you can now use “if (aString in someStringList) then…”, the not in form is also supported.
Smart pascal source code
function IncFunc(Val, delta: Integer): Integer; begin Result := Val + delta; ensure Result = old Val + 1; end; procedure IncProc(var Val: Integer; delta: Integer); begin Val += delta; ensure Val = old Val + 1; end; { main.pas } var v: Integer = 1; Begin v := IncFunc(v, 1); Writeln(v); IncProc(v, 1); Writeln(v); try v := IncFunc(v, 2) except on E: Exception do Writeln(E.Message); end; try IncProc(v, 2) except on E: Exception do Writeln(E.Message); end; Writeln(v); // 2 3 7