[erlang-questions] -spec tuple variable size

Attila Rajmund Nohl <>
Thu Nov 29 16:52:29 CET 2012


Hello!

Are you really sure you need to use tuples? How do you match on the
tuples if you don't know their length? How do you access the elements
in the tuple?

2012/11/29 Dmitry Kolesnikov <>:
> Hello,
>
> No this do not work!
> here is my snippet
>
> -- CLIP --
> -type entity()    :: tuple(integer()).
>
> -spec create(atom(), entity()) -> ok.
> create(abc, {1, 1})
>
> test.erl:237: The call test:create('abc',{1,1}) will never return since the
> success typing is (any(),{integer()}) -> 'ok'
>
> -- CLIP --
>
> - Dmitry
>
>
> On Nov 29, 2012, at 6:05 AM, Slava Yurin <> wrote:
>
> Hi, Dmitry.
>
> May be "tuple(Type)" help you?
>
> -type my_tuple() :: tuple(integer()). % {1}, {1,1}, {1, ...}
>
> 29.11.2012, 05:10, "Dmitry Kolesnikov" <>:
>
> Hello,
>
> Thanks for response!
> Let me explain better what I am trying to achieve.
> "The shorthand [T,...] stands for the set of non-empty proper lists whose
> elements are of type T."
> I am looking for similar definition but for tuples.
>
> My application serialises tuples into disk. The size of tuple is unbound but
> tuple elements a fixed to string, binary, number, boolean or undefined. I
> cannot use " "|" operator to define as many variants as you like" because
> number of variants is unlimited. Well practically, I do have a hard limit of
> 4096 elements per tuple but I am lazy to type in 4096 variants :-)
>
> - Dmitry
>
> On Nov 28, 2012, at 11:50 PM, Motiejus Jakštys <>
> wrote:
>
>  On Wed, Nov 28, 2012 at 9:10 PM, Dmitry Kolesnikov
>  <> wrote:
>
>  but compiler fails syntax error before: ','
>
>  -- CLIP --
>  -type value()     :: string() | binary() | number() | boolean() |
> undefined.
>  -type entity()    :: [{atom(), value()}] | {field()}.
>
>  These should be fine.
>
>  -type field()      :: value() | value(), field().
>
>  Maybe you meant
>  -type field() :: value() | {value(), field()}.
>
>  ?
>
>  In general, if you want to define tuples of different sizes in -spec,
>  you use the "|" operator to define as many variants as you like.
>
>  Likely I don't understand what you are trying to define.
>
>  --
>  Motiejus Jakštys
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list