View Source Corsa.Within (corsa v0.1.2)
Within contracts, there are time bounds to function executions. In Corsa, when an annotated function does not execute within the time bound specified by the contract, a runtime error is thrown.
Example
iex> defmodule Elixir.Corsa.Within.Example do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x) do x * 1000 end
...> def f(1) do Process.sleep(10000) end
...> def f(x) do x end
...> end
iex> Elixir.Corsa.Within.Example.f(10)
10
iex> Elixir.Corsa.Within.Example.f(1)
** (Corsa.WithinViolationError) @within does not hold in call 'Corsa.Within.Example.f(1)'
Summary
Functions
Errors
iex> defmodule Elixir.Corsa.Within.ExampleError1 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x) do x * 1000 end
...> within f(x) do x * 1000 end
...> def f(1) do Process.sleep(10000) end
...> def f(x) do x end
...> end
** (Corsa.WithinError) @within for function 'f/1' already defined
iex> defmodule Elixir.Corsa.Within.ExampleError2 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x, x) do x * 1000 end
...> def f(1, _) do Process.sleep(10000) end
...> def f(x, _) do x end
...> end
** (Corsa.WithinError) arguments in @within should contain different names
iex> defmodule Elixir.Corsa.Within.ExampleError3 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x, _) do x * 1000 end
...> def f(1, _) do Process.sleep(10000) end
...> def f(x, _) do x end
...> end
** (Corsa.WithinError) arguments in @within cannot be ignored with _
Functions
Errors
iex> defmodule Elixir.Corsa.Within.ExampleError1 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x) do x * 1000 end
...> within f(x) do x * 1000 end
...> def f(1) do Process.sleep(10000) end
...> def f(x) do x end
...> end
** (Corsa.WithinError) @within for function 'f/1' already defined
iex> defmodule Elixir.Corsa.Within.ExampleError2 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x, x) do x * 1000 end
...> def f(1, _) do Process.sleep(10000) end
...> def f(x, _) do x end
...> end
** (Corsa.WithinError) arguments in @within should contain different names
iex> defmodule Elixir.Corsa.Within.ExampleError3 do
...> use Corsa.Assert
...> use Corsa.Within
...> within f(x, _) do x * 1000 end
...> def f(1, _) do Process.sleep(10000) end
...> def f(x, _) do x end
...> end
** (Corsa.WithinError) arguments in @within cannot be ignored with _