How do I write a type spec for the function that accepts, let's say, one parameter which has a default value? Something like the following:
def foo(bar \\ 10) do
bar
end
Would it be this:
@spec foo(integer) :: integer
Or what would it be?
Thank you.
For this purpose Elixir has @spec annotation to describe the specification of a function that will be checked by compiler. However in some cases specification is going to be quite big and complicated. If you would like to reduce complexity, you want to introduce a custom type definition.
TypeSpec provides metadata describing an object accepted or returned by TensorFlow APIs. Concrete subclasses, such as tf. TensorSpec and tf. RaggedTensorSpec , are used to describe different value types.
Yes.
I would add that if your question is if there is a difference between the typespec of a function which has an argument with a default value and an argument that doesn't, then no there is no difference.
It works as expected because you actually define two functions.
@spec foo(integer) :: integer
def foo(bar \\ 10) do
bar
end
Is equivalent to:
def foo() do
foo(10)
end
@spec foo(integer) :: integer
def foo(bar) do
bar
end
So you basically end up with two functions but only one of them has @spec
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With