[erlang-questions] Erlang type spec questions

zxq9 zxq9@REDACTED
Thu Oct 15 01:23:39 CEST 2015


On 2015年10月14日 水曜日 18:38:35 lloyd@REDACTED wrote:
> Hello,
> 
> 1. -spec open_read(Table :: list(), Type :: atom()) -> {ok, Name} | {error, Reason}.
> 
> Gives me Error: ...Reason is unbound
> 
> Question: How should I represent?

When the spec gets even slightly too long for a single line I tend to do this:

-spec open_read(Table, Type) -> {ok, Name} | {error, Reason}.
    when Table  :: ets:tab(),
         Type   :: atom(),
         Name   :: unicode:chardata(),
         Reason :: term().

or whatever applies to Name and Reason.

 
> 2. How does one represent a File?
>     
> E.g. -type get_tags(File :: ??? 

file:io_device()

This is a general type; a union of `pid() | file:fd()`.

Check out the "Data types" section:
http://www.erlang.org/doc/man/file.html

> 3. Is a dets table a file or a list? 
> 
> E.g. How would one represent it?

Similar to the above:

ets:tab()

Same place in the docs format, under "Data types":
http://www.erlang.org/doc/man/ets.html

I don't recall that there is any place in the manual that actually says "the page for each module has a section of exported types called 'data types', and each type can be used in your programs by using the module in question as a module type prefix." There maybe should be a note like that somewhere, and may be, but I don't recall ever seeing it. Nearly every unique structure in the whole system has an exported type to represent it. Sometimes they are as simple as `foo:name() :: atom()`, but using the exported name *instead* of atom makes your code understandable at a glance and future-proofs it against the underlying value ever changing in a later release.

-Craig



More information about the erlang-questions mailing list