unsafe variables in try?

Mark Scandariato mscandar@REDACTED
Tue Oct 26 02:27:39 CEST 2004


oops! change bardarg to {badmatch, _}

(Should have cut & paste!)

Mark Scandariato wrote:
> 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.



More information about the erlang-questions mailing list