[erlang-questions] case and pattern matching

igwan igwan@REDACTED
Tue Jul 10 12:41:40 CEST 2007


Hi,

You can use guards as below :

init_indecies(Tab, Storage, PosList) ->
     case Storage of
	S when S==unknown;S==s3_copies ->
	    ignore;
	disc_only_copies ->
	    init_disc_index(Tab, PosList);
	S when S==ram_copies;S==disc_copies ->
	    make_ram_index(Tab, PosList)
     end.

You're forced to bind a variable and I also sometimes wish there was a 
shorter way of writing it too.

igwan

Joel Reymont a écrit :
> Folks,
>
> What do you do when two clauses in your case statement need to share  
> the code? Do you create an anonymous function and reuse it?
>
> I really wish there was a way to combine patterns but I can't seem to  
> find any. Consider the following bit of Mnesia code:
>
> init_indecies(Tab, Storage, PosList) ->
>      case Storage of
> 	unknown ->
> 	    ignore;
> 	s3_copies ->
> 	    ignore;
> 	disc_only_copies ->
> 	    init_disc_index(Tab, PosList);
> 	ram_copies ->
> 	    make_ram_index(Tab, PosList);
> 	disc_copies ->
> 	    make_ram_index(Tab, PosList)
>      end.
>
> What I would like to do is join unknown and s3_copies or maybe  
> ram_copies and disc_copies in a single pattern match like
>
> uknown | s3_copies -> ...
>
> Is this possible?
>
> 	Thanks, Joel
>
> --
> http://topdog.cc      - EasyLanguage to C# compiler
> http://wagerlabs.com  - Blog
>
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>   




More information about the erlang-questions mailing list