views:

1187

answers:

2
+4  Q: 

erlang type system

hello I've been scrounging around the web looking for various typing practices of Erlang programs and there seem to be a few... although its somewhat difficult to find a solid source of info namely Im looking for practical info about:

1."-specs" - this one looks pretty attractive. a few places mention that the functions that have an associated -specs directive with it are checked at compile time(for correct type usage)... I cant seem to find more info on how to use it (which tool to use - Dialyzer,TypEr?). Im really eager to create a small parser/code-gen that would generate these "specs" from function declarations of the form - functionName(param1 :List, param2 :Tuple) ->...

I have not seen if -spec supports abstract types (user declared types - "Car" type - {car,{weight,},{height,},{maxSpeed,_}}

2."-deftype" directive http://www.sics.se/~joe/bluetail/vol1/v1_oo.html (thats where its being mentioned)

Erlang would become so much more powerful for me if I could start typing things and have them be checked at compile time... for run-time the parser/code-gen I mentioned above would generate guard type checks in the output sourcecode

+12  A: 

More info on the type and spec attributes here:

http://www.erlang.org/eeps/eep-0008.html

Dialyzer can be used to check them (see dialyzer --help).

Typer can be used to generate them (see typer --help).

Tim Fletcher
phenomenal!! lots of info, details, thanks so much for the links!
deepblue
+1  A: 

Compile time type checking is not the done thing with Erlang. Instead use the Dialyzer which performs post-compile type checking.

The way in which you create 'user-defined types' is by using tagged tuples as you suggest. The Dialyzer will examine code paths to identify ones that MIGHT end up creating function returns that fail.

For the dialyzer to work best you must embrace 'let if fail' and only write clauses that match the expected results - eschew 'else' constructs that always match and a few other best practices.

You should document your functions with edoc. The dialyzer uses the type specification of the document system to infer types for you. The edoc manual can be found here.

Gordon Guthrie
isnt edoc being depreciated for code typing in favor of -spec? I think I've seen it someplace as a note with R13 in mind, correct me if Im wrong\nthanks
deepblue
Your right - I dug up Kostas Sagonas's paper from EUC'08 at http://www.erlang.se/euc/08/1400Kostis.pdf and this is what he recommends.Unfortunately I missed EUC this year trying to raise money :(
Gordon Guthrie