Badops
Badops

Reputation: 91

How do I define the spec of an elixir function that has an unused parameters?

@spec another(String.t, String.t) :: String.t 
def another(_para1, _para2) do
  IO.puts "this is a test"
end

can i define the type of the parameters of the function this way even though the parameters are not used in the body of the function?

Upvotes: 2

Views: 850

Answers (2)

mpm
mpm

Reputation: 3584

If you allow any arguments, you could use type any() in you definition

@spec another(any(), any()) :: String.t

Upvotes: 0

PatNowak
PatNowak

Reputation: 5812

Yes, you can do it and it's syntactically valid. The @spec attribute should contain all of the expected inputs and outputs gathered in one place. In this case you're saying to the dialyzer (and implicitly to the code's reader) that this function will accept only strings.

Question is why you want to do that? If you don't need these parameters why do you want to accept them? If it's your last clause, everything is ok, because you have other definitions for that function and they accept only strings, so @spec is valid.

Upvotes: 1

Related Questions