Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with maybe expr #55

Open
egobrain opened this issue Apr 22, 2024 · 1 comment
Open

Issue with maybe expr #55

egobrain opened this issue Apr 22, 2024 · 1 comment

Comments

@egobrain
Copy link

This simplified code:

-module(t).
-feature(maybe_expr, enable).

-export([mult2/1]).

-spec mult2(string()) -> {ok, string()} | {error, bad_int}.
mult2(Str) ->
    maybe
        {ok, Int} ?= convert_to_int(Str),
        {ok, integer_to_list(Int*2)}
    end.

-spec convert_to_int(string()) -> {ok, integer()} | {error, bad_int}.
convert_to_int(Str) ->
    try list_to_integer(Str) of Int ->
        {ok, Int}
    catch _:_ -> {error, bad_int}
    end.

leads to issue:

error: incompatible_types
  ┌─ src/t.erl:9:22
  │
9 │         {ok, Int} ?= convert_to_int(Str),
  │                      ^^^^^^^^^^^^^^^^^^^
  │                      │
  │                      convert_to_int(Str).
Expression has type:   {'ok', number()} | {'error', 'bad_int'}
Context expected type: {'ok', string()} | {'error', 'bad_int'}

See https://fb.me/eqwalizer_errors#incompatible_types
  │

  {'ok', number()} | {'error', 'bad_int'} is not compatible with {'ok', string()} | {'error', 'bad_int'}
  because
  {'ok', number()} is not compatible with {'ok', string()} | {'error', 'bad_int'}
  because
  at tuple index 2:
  {'ok', number()} is not compatible with {'ok', string()}
  because
  number() is not compatible with string()

Everything works well If i replace it with equivalent case expression:

-spec mult2(string()) -> {ok, string()} | {error, bad_int}.
mult2(Str) ->
    case convert_to_int(Str) of
        {ok, Int} ->
            {ok, integer_to_list(Int*2)};
        Other ->
            Other
    end.
@ilya-klyuchnikov
Copy link
Member

Yes, that is a false positive. Unfortunately, eqwalizer has very limited support for handling maybe expressions and we don't have any plans to improve it in the nearest future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants