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

In exercise TwoFer, a recommendation to use String.t() in the typespec is added, even if String.t() is used. #413

Closed
jelemux opened this issue Dec 23, 2023 · 1 comment · Fixed by #414

Comments

@jelemux
Copy link

jelemux commented Dec 23, 2023

My code:

defmodule TwoFer do
  @moduledoc """
  Provides ways to share cookies
  """

  @doc """
  Two-fer or 2-fer is short for two for one. One for you and one for me.
  """
  @spec two_fer(name :: String.t()) :: String.t()
  def two_fer(name \\ "you") when is_binary(name) do
    "One for #{name}, one for me."
  end
end

Expectation: The typespec is considered okay by the analyzer.

Reality: The following recommendation is given:

Make sure that the typespec for two_fer/1 is correct. The function accepts one argument - a string, and returns one argument - also a string.

The right type for strings is String.t().

The types String.t() and binary() are equivalent to analysis tools. Although, for those reading the documentation, String.t() implies it is a UTF-8 encoded binary.

The type string() means a charlist, not an Elixir string.

Elixir discourages the use of the string() type. The string() type refers to Erlang strings, which are known as "charlists" in Elixir. They do not refer to Elixir strings, which are UTF-8 encoded binaries.

@angelikatyborska
Copy link
Member

Thank you! This should fix it: #414

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

Successfully merging a pull request may close this issue.

2 participants