#Elixir type parameter

9 messages · Page 1 of 1 (latest)

torpid coral
#

i want the T type parameter to be either float or integer

@spec search([T], T) :: integer when arg: integer | float

is this equivalent to?

@spec search([integer], integer) :: integer
@spec search([float], float) :: float

If not, how can i rewrite it in the correct way?

sly comet
#

I don't think this is actually possible. The best you can get is @spec search([integer | float], integer | float) :: integer | float, which I know isn't quite the same thing

#

multiple @spec attributes are not supported as far as I'm aware

keen quartz
#

Multiple spec attributes are supported, but it will be merged and for Dialyzer will result with the same as what you have written, so in the end all 3 notions will be equivalent

#

There is currently no way to state what @torpid coral want (at least not exactly)

stoic cipher
#

if it always returns int like your first example