witchcrafters/algae

Type variables for use in typespecs

mbuhot opened this issue · 1 comments

It would be great if defdata / defsum allowed type variables that could be used for writing typespecs to clarify the expected contents of types like Either.t, Maybe.t etc..

Eg:

defmodule Maybe do
  defsum(a) do
    defdata Nothing :: none()
    defdata Just    :: a
  end
end

@spec try_parse_int(String.t) :: Maybe.t(integer)

Would this be as simple as adding generic typespecs to Algae- (and custom-) defined modules? For example:

defdata Ints :: list(integer())
defdata Errors :: list(String.t())

defmodule Either do

  @type right(a) :: a
  @type left(b) :: b
  @type t(a,b) :: right(a) | left(b)

  defsum do
    defdata Left  :: any()
    defdata Right :: any()
  end
end

defmodule A do
  @spec f(any()) :: Either.t(%Ints{}, %Errors{})
  def f(_), do: Ints.new() |> Either.Right.new()
end