Skip to content

Commit ca45620

Browse files
author
José Valim
committed
Define Record.options for each record with the allowed options
Closes #916, #857
1 parent fcc7fc4 commit ca45620

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

lib/elixir/lib/record.ex

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,21 +533,32 @@ defmodule Record do
533533
## Types/specs generation
534534

535535
defp core_specs(values) do
536-
types = lc { _, _, spec } inlist values, do: spec
536+
types = lc { _, _, spec } inlist values, do: spec
537+
options = if values == [], do: [], else: [options_specs(values)]
537538

538539
quote do
539540
unless Kernel.Typespec.defines_type?(__MODULE__, :t, 0) do
540541
@type t :: { __MODULE__, unquote_splicing(types) }
541542
end
542543

544+
unless Kernel.Typespec.defines_type?(__MODULE__, :options, 0) do
545+
@type options :: unquote(options)
546+
end
547+
543548
@spec new :: t
544-
@spec new(Keyword.t | tuple) :: t
545-
@spec to_keywords(t) :: Keyword.t
546-
@spec update(Keyword.t, t) :: t
549+
@spec new(options | tuple) :: t
550+
@spec to_keywords(t) :: options
551+
@spec update(options, t) :: t
547552
@spec __index__(atom) :: non_neg_integer | nil
548553
end
549554
end
550555

556+
defp options_specs([{ k, _, v }|t]) do
557+
:lists.foldl fn { k, _, v }, acc ->
558+
{ :|, [], { k, v }, acc }
559+
end, { k, v }, t
560+
end
561+
551562
defp accessor_specs([{ :__exception__, _, _ }|t], 1, acc) do
552563
accessor_specs(t, 2, acc)
553564
end

0 commit comments

Comments
 (0)