[erlang-questions] I think I wish I could write case Any of whatever -> _ end.

Eric Newhuis (personal) enewhuis@REDACTED
Mon May 17 16:00:56 CEST 2010


For the record, I might still disagree, so far.  I'm not sure.  Simply for argument's sake...

The context in which this might be maximally useful is the following.

case some_module:some_function(...) of
	{some, pattern} -> _;
	{some, other, pattern} -> _;
	_ -> whatever
end.

Note that I belong to the school of philosophy that suggests that the number of temporary variables should be minimized.  I don't understand why the above would be called wild-card abuse.  It is clear from context that the wild-card represents something other than matching.  We've seen this idea before.  There are those grammars that expose variables whose value is whatever matched.

I suppose the following is where this great idea of mine might break down.

case some_module:some_function(...) of
	{some, pattern} ->
		{encapsulated, _};
	{some, other, pattern} ->
		{another, _, encapsulation}
end.

Although I still don't have a problem with that.  From context I know that the right hand side of the arrow isn't pattern matching.

I guess where readability might break down is in nesting:

case some_module:some_function(...) of
	{some, _, pattern} -> % _1
		case _ of ->  % _2
			{some, great, pattern} ->
				not_so_bad;
			_ -> % _3
				{_, Kind, _} = _, % _4, _5, _6
				Kind
		end
end.

Although I can still read the above once I learn that underscore ('_') is context sensitive.

_1 :: any()
_2 :: {some, any(), pattern}
_3 :: {some, any(), pattern}, not {some, great, pattern}
_4 :: some
_5 :: pattern
_6 :: _3

On May 16, 2010, at 9:55 PM, Richard O'Keefe wrote:

> 
> On May 15, 2010, at 7:05 AM, Eric Newhuis (personal) wrote:
> 
>> Consider:
>> 
>> X = case Any of
>> 		very_long_pattern_or_whatever ->
>> 			very_long_pattern_or_whatever
>> 	end.
>> 
>> 
>> I'd rather abbreviate that like this.
>> 
>> X = case Any of
>> 	very_long_pattern_or_whatever -> _
>> 	end.
>> 
>> It also seems more readable to me than:
>> 
>> X = case Any of
>> 	very_long_pattern_or_whatever=Y -> Y
>> 	end.
> 
> Abusing the wild-card like that would be very confusing.
> It certainly is NOT more readable than
> 
> 	X = case Any
> 	      of Y = very_long_pattern_or_whatever ->
> 		 Y
> 	    end
> or
> 	X = case Y = Any
> 	      of very_long_pattern_or_whatever -> Y
> 	    end
> 
> or even
> 	Y = Any,
> 	X = case Y
> 	      of very_long_pattern_or_whatever -> Y
> 	    end
> 
> We already have so many readable ways to do it that
> we're much better off not adding an unreadable one.
> 
> 



More information about the erlang-questions mailing list