[erlang-questions] Missing features in Erlang type system and several additional question related to it
Tue Aug 31 15:11:29 CEST 2010
Sergii Boiko wrote:
> Hi all,
> I'm working with json using mochiwebjson2 library and need to
> specialize next type:
> -type packet_encoding() :: 1 | 0.
> -type packet() :: [integer(), packet_encoding(), binary()].
> But it's turned out, Erlang doesn't have support for lists with fixed
> data amount and type packet doesn't compiling. If you wondered why I
> choose list over tuple, it's because in json there is no tuple and i
> serialize packets from erlang to json via mochiwebjson2, which also
> doesn't work with tuples.
Your arguments are very weak, but you probably know that already. In
Erlang it's tuples and not lists which are supposed to be used for
collections of items of known number. If json or mochiwebjson2 or
whatever has limitations, then the answer to what you are trying to do
is most probably a call to tuple_to_list/1 away...
If you want to stick with lists, the closest you currently can get with
the current type language is to use the following definition:
-type packet() :: [integer() | packet_encoding() | binary()].
which specifies a list which can contain integers, packet_encodings
and/or binaries (of any size). This is a type which is more general
than the one you want to specify, but it should work OK.
> But in any case i guess this is missed feature of Erlang type system
> and it should be addressed.
Well, that's your opinion. I beg to differ here, but perhaps this is
irrelevant. One nice thing about Erlang/OTP is that its implementation
is open source and nowadays there is an easy way for users to submit
patches for features they feel they should be included into it.
> Another issue is with parametrized erlang modules. I use mochiweb
> which uses this feature. And for things like Req:method(), Dialyzer
> thinks that Req is atom. Maybe it even doesn't know about parametrized
> modules. Maybe, there is some type or additional hint for such cases?
Which version of dialyzer are you using, by the way? Dialyzer supports
parameterized modules since Erlang/OTP R13B04.
> I also have issue with Dialyzer and dirty check on dict'ness of value.
> Maybe there is a point to add some 'true' is_dict guard? because dirty
> is_record(Term, dict, 8) also made it complain when i lately use some
> functions from dict module.
Yes, here I agree with you. It would be a good idea for the dict module
to export an is_dict/1 function (similarly for other modules with opaque
data structures). Perhaps the OTP team can add these. Allowing them to
be used as guards is an extra bonus, which might happen some day.
More information about the erlang-questions