vans163

vans163

Typespecing a map of arbitrary integer keys?

I am wondering if this is correct, this should be a map of arbitrary size (could be 0, could be 1000) of integer keys.

@type quests() :: %{Integer => any()}
defmodule GameState do
    @type obj() :: %{0=> Integer, optional(1)=> Integer, optional(2)=> Integer}
    @type quest() :: %{id: Integer, step: Integer, obj: obj()}
    @type quests() :: %{Integer => quest()}

    @type gamestate() :: %{quests: quests()}

    @spec get_quests(gamestate()) :: quests()
    def get_quests(gs) do
        :erlang.map_get(:quests, gs)
    end

    @spec act(gamestate()) :: any()
    def act(gamestate) do
        quests = get_quests(gamestate)

        cur_quest_id = quests
        |> Map.keys()
        |> List.last()
        cur_quest = Map.fetch!(quests, cur_quest_id)

        cur_objs = Table.QuestStep.by_id_order(cur_quest.id, cur_quest.step)
        done_objs = Enum.filter(cur_objs, fn{idx,obj}->
            cur_quest.obj[idx].amount >= obj.amount
        end)
    end

    def go do
        quest = %{id: 10010, step: 2, obj: %{0=> 14}}
        quests = %{10010=> quest}
        gamestate = %{quests: quests}
        act(gamestate)
    end
end

The idea here is to trigger a error from dialyzer here cur_quest.obj[idx].amount that .amount is not a valid key for obj().

Instead I get this, but if I call this from the REPL it works just fine. The function call succeeds.

lib/game/gamestate.ex:28:call
The function call will not succeed.

Table.QuestStep.by_id_order(Integer, Integer)

will never return since it differs in arguments with
positions 1st and 2nd from the success typing arguments:

(pos_integer(), 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)

Can anyone advise please?

Most Liked

hauleth

hauleth

Integer mean that you are matching against :"Elixir.Integer" atom, you wanted to use integer() type.

ityonemo

ityonemo

you probably want:

@type quests :: %{optional(integer) => any}

or maybe

@type quests :: %{optional(non_neg_integer) => any}

I prefer omitting parens on non parametric types, to cut down on line noise, but if you prefer keeping them in (which I believe is idiomatic?):

@type quests() :: %{optional(non_neg_integer()) => any()}
asummers

asummers

Yikes I need to fix that message.

Last Post!

vans163

vans163

Heh dialzyer with typespecs is not what we were looking for. So not sure if I will continue looking into this.

Basically because of that lol.

Waiting now to see if Facebook reveals anything interesting on Nov, if they made a totally new static typed language that compiles to beam that seems useless. If they build a new analysis tool or IL to spot type errors in beam files/core erlang that is more interesting now.

If nothing interesting from FB will probably take an attempt at walking compiled beam bytecode.

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement