[erlang-questions] Local namespaces in case/try
Dmitrii Dimandt
dmitrii@REDACTED
Tue Jul 10 12:19:01 CEST 2012
On Jul 10, 2012, at 11:54 , Ivan Uemlianin wrote:
>> On 07/10/2012 10:43 AM, Dmitry Groshev wrote:
>> case do_something() of
>> {ok, Result} -> Result;
>> {error, Error} -> Error
>> end,
>> case do_another() of
>> {ok, Result} -> Result;
>> {error, Error} -> Error
>> end,
>>
>> Result and Error are bound in first case and we will probably have a
>> match failure in second one. Compiler warns about this, but it's still
>> very unwieldy to fix it with names like Error1, Error2, etc.
>
> Are you expecting that the "Result" variable returned from do_another() should have the same value as the "Result" returned from do_something()? (and likewise for Error.)
>
> If so, I don't see why you would get a match failure: e.g., you surely wouldn't get a match failure if do_something() and do_another() both returned {ok, 5}?
>
> If not, yes you will get match failures.
>
> To this naive observer, the code looks like the "Result"s (and the "Errors"s) are expected to be identical.
It's easy to extend the example as
Val1 = case do_something() of
{ok, Result} -> Result;
{error, Error} -> Error
end,
Val2 = case do_another() of
{ok, Result} -> Result;
{error, Error} -> Error
end,
Why would Result from the first case need to match Result in the second case is beyond my comprehension.
Of course, we can do to the extreme and refactor each and every case into its own function. Should we?
More information about the erlang-questions
mailing list