[erlang-questions] spec declaration for single list parameter with fixed size

14 views
Skip to first unread message

Roberto Ostinelli

unread,
Jul 3, 2011, 4:10:16 AM7/3/11
to Erlang
dear list,

a very stupid spec declaration question. how can you declare the specs of a function which has a single list with multiple arguments?

For instance, consider this function:

myfun([One, Two]) ->

these two following declarations do obviously not work:

-spec myfun([One::string(), Two::integer()]) ->
-spec myfun(list(One::string(), Two::integer())) ->

the proper way seems to be:

-spec myfun([string() | integer()]) ->

however i feel this to be very misleading, as the specs do not show the order nor the fixed length of the parameter list.

is there a better way to do so?

thank you.

r.

Michael Richter

unread,
Jul 3, 2011, 5:58:34 AM7/3/11
to Roberto Ostinelli, Erlang Users' List
If you have a fixed length and format, it strikes me that a list isn't the data structure you want given that a list is by definition variable-length.  Is there a reason you're not using a tuple instead of that list?  Something like this?:

myfun({One, Two}) -> ...
 
--
"Perhaps people don't believe this, but throughout all of the discussions of entering China our focus has really been what's best for the Chinese people. It's not been about our revenue or profit or whatnot."
--Sergey Brin, demonstrating the emptiness of the "don't be evil" mantra.

Jesper Louis Andersen

unread,
Jul 3, 2011, 6:20:15 AM7/3/11
to Michael Richter, Erlang Users' List, Roberto Ostinelli
On Sun, Jul 3, 2011 at 11:58, Michael Richter <ttmri...@gmail.com> wrote:
> On 3 July 2011 16:10, Roberto Ostinelli <rob...@widetag.com> wrote:
>>
>> a very stupid spec declaration question. how can you declare the specs of
>> a function which has a single list with multiple arguments?
>>
>> For instance, consider this function:
>
>
>>
>> myfun([One, Two]) ->
>

This is better since you then have a product type rather than a
recursive type on lists. It is much simpler to work with from a typing
perspective. Also note that you would not easily be able to type
["hello", 5] in a statically typed language without resorting to
either some kind of type tagging, polymorphic variants or existential
types.

My advice would be to alter the structure of the function such that it
is easier to type. It tends to be safer from a programming perspective
as well.

--
J.
_______________________________________________
erlang-questions mailing list
erlang-q...@erlang.org
http://erlang.org/mailman/listinfo/erlang-questions

Kostis Sagonas

unread,
Jul 4, 2011, 9:37:56 AM7/4/11
to Erlang Users' List, Roberto Ostinelli
Jesper Louis Andersen wrote:
> On Sun, Jul 3, 2011 at 11:58, Michael Richter <ttmri...@gmail.com> wrote:
>> On 3 July 2011 16:10, Roberto Ostinelli <rob...@widetag.com> wrote:
>>> a very stupid spec declaration question. how can you declare the specs of
>>> a function which has a single list with multiple arguments?
>>>
>>> For instance, consider this function:
>>
>>> myfun([One, Two]) ->
>
> This is better since you then have a product type rather than a
> recursive type on lists. It is much simpler to work with from a typing
> perspective. Also note that you would not easily be able to type
> ["hello", 5] in a statically typed language without resorting to
> either some kind of type tagging, polymorphic variants or existential
> types.
>
> My advice would be to alter the structure of the function such that it
> is easier to type. It tends to be safer from a programming perspective
> as well.

Both Jesper and Michael have given very good advice on this topic, but
for the record, I guess, let me add my two cents here.

First of all, as others have mentioned, when grouping together a fixed
number of items, tuples rather than lists should better be used. Not
only does this make sense from a typing perspective, but for more than
one item tuples are also more memory efficient and thus slightly faster.
For example [One, Two] needs 4 words while {One, Two} needs 3.

Coming back to the original question, it's pretty clear that what the
type language does it to make a abstraction over all terms (which belong
to some type) and thus cannot possibly express all programmer intentions
accurately. For example, it is currently not possible to declare lists
with a certain number of elements, partly due to the fact that the type
language has decided to make [T] an alias for list(T). Even if this
constraint was lifted (e.g. [T] stood for a one element list for items
of type T, [T1,T2] for a two element list of items of type T1 and T2,
etc.) the type language would not be able to express all programmer
intentions (e.g. lists of an even number of elements, lists whose length
is a prime number, lists where the middle element is 42, etc.). This is
fundamental limitation of all type languages: no matter how expressive
they become, there are things that cannot (conveniently) be expressed in
them.

Kostis

Richard O'Keefe

unread,
Jul 4, 2011, 7:14:24 PM7/4/11
to Roberto Ostinelli, Erlang

On 3/07/2011, at 8:10 PM, Roberto Ostinelli wrote:

> dear list,
>
> a very stupid spec declaration question. how can you declare the specs of a function which has a single list with multiple arguments?

...


> is there a better way to do so?

Don't (ab)use a list like this.
If the list always has two elements, why not make them separate arguments?
If the elements are fixed in number and differing in type, why is this a
list and not a tuple?

Roberto Ostinelli

unread,
Jul 5, 2011, 3:05:30 AM7/5/11
to Kostis Sagonas, Erlang Users' List


2011/7/4 Kostis Sagonas <kos...@cs.ntua.gr>

Jesper and Michael,

thank you for your feedback on this. I am aware of the necessity of using tuples in this condition instead of lists. However, I was using a library whose callback function is getting arguments as list.

Kostis, thank you for clarifying this.

Cheers,

r.
Reply all
Reply to author
Forward
0 new messages