-
-
Save Qqwy/715b31cee2c93e7a658b4b4e51ea4b37 to your computer and use it in GitHub Desktop.
debug_example.ex
This file contains 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
defmodule DebugExample do | |
@compile :inline | |
@compile {:inline_size, 100} | |
use TypeCheck, debug: true | |
@spec! stringify(integer()) :: binary() | |
def stringify(val) do | |
to_string(val) | |
end | |
end |
This file contains 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
TypeCheck.Macros @spec generated: | |
---------------- | |
if(Module.get_attribute(__MODULE__, :autogen_typespec)) do | |
@spec stringify(integer()) :: binary() | |
end | |
defoverridable(stringify: 1) | |
def(stringify(arg1)) do | |
with( | |
{{:ok, _bindings}, _index, _param_type} <- | |
{case(arg1) do | |
x when is_integer(x) -> | |
{:ok, []} | |
_ -> | |
{:error, {%{__struct__: TypeCheck.Builtin.Integer}, :no_match, %{}, arg1}} | |
end, 0, %{__struct__: TypeCheck.Builtin.Integer}} | |
) do | |
else | |
{{:error, problem}, index, param_type} -> | |
raise( | |
TypeCheck.TypeError, | |
{{__MODULE__."__TypeCheck spec for 'stringify/1'__"(), :param_error, | |
%{index: index, problem: problem}, [arg1]}, [file: "iex", line: 1]} | |
) | |
end | |
var!(super_result, nil) = super(arg1) | |
case( | |
case(super_result) do | |
x when is_binary(x) -> | |
{:ok, []} | |
_ -> | |
{:error, {%{__struct__: TypeCheck.Builtin.Binary}, :no_match, %{}, super_result}} | |
end | |
) do | |
{:ok, _bindings} -> | |
nil | |
{:error, problem} -> | |
raise( | |
TypeCheck.TypeError, | |
{__MODULE__."__TypeCheck spec for 'stringify/1'__"(), :return_error, | |
%{problem: problem, arguments: [arg1]}, var!(super_result, nil)} | |
) | |
end | |
var!(super_result, nil) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For the curious, this results in the following BEAM bytecode:
Which is roughly equivalent to this Elixir code:
So as you can see, when the more advanced features of TypeCheck, such as using named types / type guards are not used, these things are optimized away by the compiler.