Created
April 28, 2018 09:13
-
-
Save technion/325d2906889ba29404f0aff43502cad2 to your computer and use it in GitHub Desktop.
Erlang dialyzer demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dial.erl | |
-module(dial). | |
-export([dial/0]). | |
addthree(X) -> | |
X + 3. | |
dial() -> | |
Y = addthree(2), | |
io:fwrite("~p~n", [Y]). | |
When run: | |
2> dial:dial(). | |
5 | |
Add a bad call: | |
_ = addthree("test"), | |
Compiles without error. | |
1> c(dial). | |
2> dial:dial(). | |
** exception error: an error occurred when evaluating an arithmetic expression | |
in function dial:addthree/1 (dial.erl, line 5) | |
in call from dial:dial/0 (dial.erl, line 9) | |
Alternately, run typEr on original code. Due to our trivial example, an overly specified function is suggested. | |
$ /usr/lib/erlang/bin/typer dial.erl | |
%% File: "dial.erl" | |
%% ---------------- | |
-spec addthree(2) -> 5. | |
-spec dial() -> none(). | |
Paste output into code. | |
Now add bad function call, see this on compile: | |
dial.erl:10: Function dial/0 has no local return | |
dial.erl:12: The call dial:addthree([101 | 115 | 116,...]) will never return since the success typing is (2) -> 5 and the contract is (2) -> 5 | |
done in 0m0.17s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment