[erlang-questions] specs for fixed length lists

Tony Rogvall <>
Sat Jul 7 13:57:45 CEST 2012


Hi!

On 6 jul 2012, at 18:28, Daniel Goertzen wrote:

> While working with lists of fixed length, I noted that type specification for such lists are conspicuously missing.  For example, the following does not work...
> 
> -spec get_buttons() -> {ok, [boolean(), boolean(), boolean(), boolean()]}.
> 
Try this:

    -type buttons() :: [boolean() | [boolean() | [boolean() | [boolean() | []]]]].

    -spec get_buttons() -> {ok, buttons()}.

    get_buttons() ->
       [true | [false | [true | [false | [] ] ] ] ].

Of course my formatting of the get_buttons return value is intensional. You could write it
as normal [true,false,true,false].

You can of course write it as:

-spec get_buttons() -> {ok,[boolean() | [boolean() | [boolean() | [boolean() | []]]]] }.

Regards

/Tony


> Is there some sneaky way to make this go?  Now I know you would normally use a tuple in this situation, but I will be using the lists module to process this data so I want to stick with lists.
> 
> I would love to see something like this for list specification...
> 
> 
>   List :: list(Type)                        %% Proper list ([]-terminated)
> +       | list(Type, Length)                %% Proper list of fixed size
> +       | tlist(TList)                      %% Proper list specified like a tuple
>         | improper_list(Type1, Type2)       %% Type1=contents, Type2=termination
>         | maybe_improper_list(Type1, Type2) %% Type1 and Type2 as above
> 
>   Tuple :: tuple()                          %% stands for a tuple of any size
>         | {}
>         | {TList}
> 
>   TList :: Type
>         | Type, TList
> 
> 
> Thanks,
> Dan.
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions

"Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120707/0ffb243c/attachment.html>


More information about the erlang-questions mailing list