<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">On 2016-09-22 18:39, 月忧茗 wrote:<br>
</div>
<blockquote
cite="mid:CALZWPGjqLZeWwvyi_MbXeyEwSO5xPe7gdwTEbqUFXLw5C2od7A@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>According this document: <a moz-do-not-send="true"
href="http://erlang.org/doc/reference_manual/typespec.html">http://erlang.org/doc/reference_manual/typespec.html</a></div>
<div><br>
</div>
<div>I can define a map type like this:</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">-type
my_type() :: #{integer() := string()}.</blockquote>
<div><br>
</div>
<div>I have a map called State, it's type is `my_type`, key
type is integer, value type is string.</div>
<div><br>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px
0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">-spec
find_value(atom(), my_type()) -> string().<br>
find_value(Key, State) when is_atom(Key) -><br>
#{Key := Value} = State,<br>
Value.</blockquote>
<div><br>
</div>
<div><br>
</div>
<div>Using dialyzer analysis the file. nothing happens.</div>
<div>dialyzer say there is no error for this file.</div>
<div><br>
</div>
<div>I know maps are dynamic, my_type() just meas this map must
have a integer key associated with a string value.</div>
<div>But not limit to add other type key and value in the map.</div>
<div><br>
</div>
<div>My question is that Is it possible limit the map types of
key and value ?</div>
<div>So dialyzer can find the match error in my program.</div>
</div>
</blockquote>
<br>
Actually, your type is already restricted in this way, and I'm
surprised your <tt>find_value</tt> example does not produce any
warnings. Dialyzer should be quite capable of finding the problem.
Indeed, if we enforce the type of the State argument in this
(slightly ridiculous) way instead of using a type specification, the
problem <i>is</i> found:<br>
<pre>find_value(Key, State) ->
StateList = [{K, V} || {K, V} <- maps:to_list(State),
is_integer(K), is_list(V)],
find_value_1(Key, maps:from_list(StateList)).
find_value_1(Key, State) when is_atom(Key) ->
#{Key := Value} = State,
Value.
</pre>
When I get some time over, I'll have a look at why your example does
not fail, but for now rest assured that your type syntax is correct
and means what you want it to mean.<br>
<br>
// Magnus<br>
</body>
</html>