ex_type icon indicating copy to clipboard operation
ex_type copied to clipboard

Advanced support for union type + pattern match

Open gyson opened this issue 6 years ago • 0 comments

from #8 and #9, we have basic support for union type and pattern match, like following:

@spec example({:a, t1} | {:b, t2} | {:c, t3}) :: any

def example({:a, x}) do
  # ex type should be able to know that `x` is t1 type
end

def example({:b, x}) do
  # ex type should be able to know that `x` is t2 type
end

def example({:c, x}) do
  # ex type should be able to know that `x` is t3 type
end

it could be nice if we can have more advanced support like following:

@spec example({:a, t1} | {:b, t2} | {:c, t3}) :: any

def example({:a, x}) do
  # ex type should be able to know that `x` is t1 type
end

def example({:b, x}) do
  # ex type should be able to know that `x` is t2 type
end

def example(y) do
  # ex type should be able to know that `y` is `{:c, t3}` type
end

also, similarly, for case special form:

# when x is type `{:ok, integer} | {:error, String.t()} | :error`
case x do
  {:ok, y} -> 
    # ex type should be able to figure out that `y` is `integer` type

  e ->
    # ex type should be able to figure out that `e` is `{:error, String.t()} | :error` type
end

This advanced support should cover

  • multi clauses functions
  • case special form
  • anonymous function

gyson avatar Oct 14 '19 20:10 gyson