Search code examples
elixirwxwidgets

FunctionClauseError when calling :wxFrame.new from Elixir


I have just two questions :)

  1. What's wrong?
  2. How do I understand what's wrong without asking on Stackoverflow?

Elixir code:

import WxConstants
...
wx = :wx.new
frame = :wxFrame.new(wx, wxID_ANY, "Game of Life", size: {500, 500})

Output:

** (FunctionClauseError) no function clause matching in :wxFrame.new/4
    gen/wxFrame.erl:111: :wxFrame.new({:wx_ref, 0, :wx, []}, -1, "Game of Life", [size: {500, 500}])

WxConstants module: https://github.com/ElixirWin/wxElixir


Solution

  • Dogbert had already answered the first question, I would answer the second one.

    ** (FunctionClauseError) no function clause matching in ...

    is one of the most frequently happening errors in Elixir, as well as any other language supporting pattern matching in function clauses. Consider this contrived example:

    defmodule M do
      def test(param) when is_binary(param), do: "binary"
      def test(param) when is_list(param), do: "list"
    end
    M.test("Hello, world")
    #⇒ "binary"
    M.test([1, 2, 3])
    #⇒ "list"
    

    When there is no function clause, that could be matched against the parameters given, the error above happens:

    M.test(42)
    #⇒ ** (FunctionClauseError) no function clause matching in M.test/1
    

    That said, the library, you are using, expected other type(s) of one or many parameters. Let’s check: :wxFrame.new/4 expects:

    Parent = wxWindow:wxWindow()
    Id = integer()
    Title = unicode:chardata()
    Option = {pos, {X::integer(), Y::integer()}} | 
             {size, {W::integer(), H::integer()}} | 
             {style, integer()}
    

    The third parameter is expected to be unicode:chardata() which is in turn Erlang charlist, that is denoted in Elixir by single quotes. Hence the comment by @Dogbert: use single quotes around 'Game of Life'.