#Compile-time error in Phoenix ShoppingCart example

4 messages · Page 1 of 1 (latest)

karmic lynx
#

Hello again!

I am following along the ShoppingCart tutorial here: https://hexdocs.pm/phoenix/contexts.html

I've followed along all the code generation portions of the tutorial thus far; however, I am now seeing this error when I run mix phx.server:

== Compilation error in file lib/hello/shopping_cart.ex ==
** (Kernel.TypespecError) lib/hello/shopping_cart.ex:144: expected key-value pairs in struct Hello.ShoppingCart.Cart
    (elixir 1.16.2) lib/kernel/typespec.ex:971: Kernel.Typespec.compile_error/2
    (stdlib 5.2.2) lists.erl:1706: :lists.mapfoldl_1/3
    (elixir 1.16.2) lib/kernel/typespec.ex:391: Kernel.Typespec.translate_spec/8
    (stdlib 5.2.2) lists.erl:1706: :lists.mapfoldl_1/3
    (elixir 1.16.2) lib/kernel/typespec.ex:236: Kernel.Typespec.translate_typespecs_for_module/2

Here is the offending bit of code:

  @spec delete_cart(%Hello.ShoppingCart.Cart{optional(any()) => any()}) :: any()
  @doc """
  Deletes a cart.

  ## Examples

      iex> delete_cart(cart)
      {:ok, %Cart{}}

      iex> delete_cart(cart)
      {:error, %Ecto.Changeset{}}

  """
  def delete_cart(%Cart{} = cart) do
    Repo.delete(cart)
  end

Commenting out the @spec line seems to be a workaround, but conceptually this doesn't seem right... any ideas on what might be happening here? Let me know. I've absolutely loved the Elixir resources so far; thanks for the time and care put into this project!

devout escarp
#

Generally when you declare type specs for structs, you don't use the %{} syntax, you would use something like Hello.ShoppingCart.Cart.t() instead

#

You can see this for String.t() in some instances.

tired cipher
#

Plus, the typespecs is incorrect, structs have mandatory keys