Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type variables for use in typespecs #26

Open
mbuhot opened this issue Mar 19, 2018 · 1 comment
Open

Type variables for use in typespecs #26

mbuhot opened this issue Mar 19, 2018 · 1 comment

Comments

@mbuhot
Copy link

mbuhot commented Mar 19, 2018

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)
@toraritte
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants