[erlang-questions] Ets match on a record vs Dialyzer

Zoltan Lajos Kis kiszl@REDACTED
Fri Sep 18 21:25:28 CEST 2009


Kostis Sagonas wrote:
> Zoltan Lajos Kis wrote:
>> Hello,
>>
>> I would like to match entries in an ets table based on some fields of 
>> records, as in the example module below.
>> When using the _='_' form for defining the "don't care" record 
>> fields, Dialyzer emits the following (reasonable) warnings:
>>
>> rec.erl:6: Function p/0 has no local return
>> rec.erl:9: Record construction #rec{x::1,y::'_',z::'_'} violates the 
>> declared type for #rec{}
>>
>> With multiple functions and modules, the no local return causes an an 
>> avalanche of further no local return's and never called's.
>>
>> Of course I could redefine the field types as {x::integer()|'_', 
>> y::integer()|'_', z::integer()|'_'} but I would prefer another match 
>> syntax or Dialyzer directive if available. So what choices do I have?
>
> From the options you mention, the best is to use a more relaxed type 
> declaration in the record fields of interest.
>
> IMO, it was a design mistake to use a perfectly valid Erlang term 
> (namely the atom '_') as a symbol with a different meaning in match 
> specifications.
>
> Kostis
I have to agree. For some reason I believed '_' and '$1' are merely 
syntactic sugar, and they are magically transformed into some sort of 
match specifications. I had some fun trying to figure out how to make 
the compiler accept expressions like #rec{x=1, _='_'} = '$1' until I 
realized these really are just atoms  :)
>
>> ----------------------
>>
>> -module(rec).
>> -export([p/0]).
>>
>> -record(rec, {x::integer(), y::integer(), z::integer()}).
>>
>> p() ->
>>    ets:new(table, [named_table, {keypos, #rec.x}]),
>>    ets:insert(table, [#rec{x=1,y=2,z=3}, #rec{x=4,y=5,z=6}]),
>>    ets:match_object(table, #rec{x=1, _='_'}).
>>
>> ________________________________________________________________
>> erlang-questions mailing list. See http://www.erlang.org/faq.html
>> erlang-questions (at) erlang.org
>



More information about the erlang-questions mailing list