[erlang-bugs] Inconsistent and incomprehensible handling of variables in spec's by compiler
Fri Feb 17 19:33:02 CET 2012
Yes, I know that. My comment was mainly about how variables are handled. It is not logical that the compiler complains about a variable being used once but not when used more than once. It also doesn't make sense that prefixing the variable name with a '_' makes the compiler keep quite. It seems like it is reusing some of the variable checking code for functions but generating errors instead of warnings.
In my case just using variable names does add information, it tells me what it is. In the respect the actual type is unimportant, if I need to type check my functions dialyzer will do it for me without a type spec. Just giving it an appropriate name is enough. It is also a way of giving long names in the "documentation" which allows me to use short names in the code. Reusing the same name tells me it is the same type. In this respect it gives me as much information as your alternative and is shorter and more concise.
Again the actual errors I get from the compiler don't make sense and I ask why the compiler should worry as it does not use the information at all. The type interpretation is actually very close to my usage and would work if it wasn't for the compiler errors, and the similar dialyzer errors.
----- Original Message -----
> There is a big difference between this:
> > -spec set_var(PrefixExp, Val, State) -> State.
> and this:
> > -spec set_var(PrefixExp::any(), Val::any(), State::any()) ->
> > State::any().
> The argument and return value type specifications are either Type
> or Name::Type, so the first declaration says that the arguments
> are of _types_ PrefixExp, Val and State, while the second declaration
> says that all three arguments are of type any(). (As far as I know,
> argument names are ignored by the compiler and dialyzer, and only
> used by edoc.) Specifying a singleton variable as the _type_ of an
> argument is most likely not what the programmer meant, since it
> doesn't add any information, so to me it makes sense to have the
> compiler reject it.
> That specification could be written like this:
> -spec set_var(PrefixExp::any(), Val::any(), State::X) -> NewState::X.
> which specifies that State and NewState must have the same type, no
> matter which one.
> Hope this helps,
> Magnus Henoch
> Erlang Solutions Ltd
More information about the erlang-bugs