unsafe variables in try?

Raimo Niskanen raimo@REDACTED
Tue Oct 26 08:45:27 CEST 2004


Try this (I have not tried it myself):

split(Bin, N) ->
    try 
        <<X:N/unit:8, R/binary>> = Bin, 
        {X,R}
    catch
        error:badarg -> throw(format)
    end.

or this:

split(Bin, N) ->
    case Bin of
        <<X:N/unit:8, R/binary>> -> {X,R};
        _ -> throw(format)
    end.



mscandar@REDACTED (Mark Scandariato) writes:

> The compiler complains that X and R are unsafe:
> 
>      split(Bin, N) ->
>          try <<X:N/unit:8, R/binary>> = Bin
>          case
>              error:badarg -> throw(format)
>          end,
>          {X, R}.
> 
> Is there any way to get it to accept the above code?
> 
> This, of course, works (but but seems otherwise needless):
> 
>      split(Bin, N) ->
>          try split2(Bin, N)
>          case
>              error:badarg -> throw(format)
>          end.
> 
>      split2(Bin, N) -> <<X:N/unit:8, R/binary>> = Bin.
> 
> 
> It'd be really nice if this worked:
> 
>      split(N, <<X:N/unit:8, R/binary>>) -> {X, R};
>      split(_, _) -> throw(format).
> 
> 
> Thanks,
> Mark.

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list