|
| 1 | +defmodule Stage3.TypedServer do |
| 2 | + |
| 3 | + ## This doesn't play well with: |
| 4 | + ## {:ok, srv} = MultiServer.start_link() |
| 5 | + ## Due to: |
| 6 | + ## The pattern {ok, _srv@1} on line 90 doesn't have the type 'Elixir.TypedServer':on_start(t()) |
| 7 | + ## A bug/limitation in Gradualizer pattern match typing? |
| 8 | + @type on_start(t) :: {:ok, t} | :ignore | {:error, {:already_started, t} | any()} |
| 9 | + |
| 10 | + def wrap(on_start, module) do |
| 11 | + case on_start do |
| 12 | + {:ok, pid} -> {:ok, {module, pid}} |
| 13 | + {:error, {:already_started, pid}} -> {:error, {:already_started, {module, pid}}} |
| 14 | + other -> other |
| 15 | + end |
| 16 | + end |
| 17 | +end |
| 18 | + |
| 19 | +defmodule TypedGenServer.Stage3.Server do |
| 20 | + use GenServer |
| 21 | + use GradualizerEx.TypeAnnotation |
| 22 | + alias Stage3.TypedServer |
| 23 | + |
| 24 | + ## Start IEx with: |
| 25 | + ## iex -S mix run --no-start |
| 26 | + ## |
| 27 | + ## Then use the following to recheck the file on any change: |
| 28 | + ## recompile(); GradualizerEx.type_check_file(:code.which( TypedGenServer.Stage3.Server ), [:infer]) |
| 29 | + |
| 30 | + @opaque t :: {__MODULE__, pid()} |
| 31 | + |
| 32 | + ## Try switching between the definitions and see what happens |
| 33 | + @type message :: Contract.Echo.req() | Contract.Hello.req() |
| 34 | + #@type message :: Contract.Echo.req() |
| 35 | + #@type message :: {:echo_req, String.t()} | {:hello, String.t()} |
| 36 | + |
| 37 | + @type state :: map() |
| 38 | + |
| 39 | + @spec start_link() :: TypedServer.on_start(t()) |
| 40 | + def start_link() do |
| 41 | + GenServer.start_link(__MODULE__, %{}) |> TypedServer.wrap(__MODULE__) |
| 42 | + end |
| 43 | + |
| 44 | + @spec echo(t(), String.t()) :: String.t() |
| 45 | + # @spec echo(t(), String.t()) :: {:echo_req, String.t()} |
| 46 | + def echo(_server = {__MODULE__, _pid}, message) do |
| 47 | + case annotate_type( GenServer.call(_pid, {:echo_req, message}), Contract.Echo.res() ) do |
| 48 | + #case call_echo(_server, message) do |
| 49 | + ## Try changing the pattern or the returned response |
| 50 | + {:echo_res, response} -> response |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + #@spec call_echo(t(), String.t()) :: Contract.Echo.res() |
| 55 | + #defp call_echo({__MODULE__, pid}, message) do |
| 56 | + # GenServer.call(pid, {:echo_req, message}) |
| 57 | + #end |
| 58 | + |
| 59 | + @spec hello(t(), String.t()) :: :ok |
| 60 | + def hello({__MODULE__, pid}, name) do |
| 61 | + case GenServer.call(pid, {:hello, name}) |> annotate_type(Contract.Hello.res()) do |
| 62 | + :ok -> :ok |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + @impl true |
| 67 | + def init(state) do |
| 68 | + {:ok, state} |
| 69 | + end |
| 70 | + |
| 71 | + @impl true |
| 72 | + def handle_call(m, from, state) do |
| 73 | + {:noreply, handle(m, from, state)} |
| 74 | + end |
| 75 | + |
| 76 | + @spec handle(message(), any, any) :: state() |
| 77 | + ## Try breaking the pattern match, e.g. by changing 'echo_req' |
| 78 | + def handle({:echo_req, payload}, from, state) do |
| 79 | + GenServer.reply(from, {:echo_res, payload}) |
| 80 | + state |
| 81 | + end |
| 82 | + |
| 83 | + ## Try commenting out the following clause |
| 84 | + def handle({:hello, name}, from, state) do |
| 85 | + IO.puts("Hello, #{name}!") |
| 86 | + GenServer.reply(from, :ok) |
| 87 | + state |
| 88 | + end |
| 89 | +end |
| 90 | + |
| 91 | +defmodule Test.TypedGenServer.Stage3.Server do |
| 92 | + alias TypedGenServer.Stage3.Server |
| 93 | + |
| 94 | + ## Typecheck with: |
| 95 | + ## recompile(); GradualizerEx.type_check_file(:code.which( Test.TypedGenServer.Stage3.Server ), [:infer]) |
| 96 | + |
| 97 | + @spec test :: any() |
| 98 | + def test do |
| 99 | + {:ok, srv} = Server.start_link() |
| 100 | + pid = self() |
| 101 | + "payload" = Server.echo(srv, "payload") |
| 102 | + ## This won't typecheck, since Server.echo only accepts Server.t(), that is Server pids |
| 103 | + #"payload" = Server.echo(pid, "payload") |
| 104 | + end |
| 105 | +end |
0 commit comments