eqwalizer icon indicating copy to clipboard operation
eqwalizer copied to clipboard

Issue with maybe expr

Open egobrain opened this issue 1 year ago • 1 comments
trafficstars

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.

egobrain avatar Apr 22 '24 14:04 egobrain

Deploy Preview for label-studio-docs-new-theme canceled.

Name Link
Latest commit 264d10d80e5285ae9ec42e1988db463092e2e084
Latest deploy log https://app.netlify.com/sites/label-studio-docs-new-theme/deploys/6685f93d8c18f100086e2588

netlify[bot] avatar Jun 19 '24 19:06 netlify[bot]

Deploy Preview for heartex-docs canceled.

Name Link
Latest commit 264d10d80e5285ae9ec42e1988db463092e2e084
Latest deploy log https://app.netlify.com/sites/heartex-docs/deploys/6685f93d818eab0008063918

netlify[bot] avatar Jun 19 '24 19:06 netlify[bot]

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

robot-ci-heartex avatar Aug 18 '24 01:08 robot-ci-heartex

This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.

robot-ci-heartex avatar Oct 19 '24 01:10 robot-ci-heartex

This PR was closed because it has been stalled for 10 days with no activity.

robot-ci-heartex avatar Oct 29 '24 02:10 robot-ci-heartex