algae icon indicating copy to clipboard operation
algae copied to clipboard

Cant initialize wrapper via constructor

Open revati opened this issue 5 years ago • 5 comments

Following the example in docs, I created a module

defmodule Id do
  import Algae

  defdata any()
end

when I try to call Id.new("random value") i get following error:

** (FunctionClauseError) no function clause matching in anonymous fn/2 in Kernel.struct/2

    The following arguments were given to anonymous fn/2 in Kernel.struct/2:

        # 1
        :id

        # 2
        %Id{id: nil}

    (elixir) lib/kernel.ex:1975: anonymous fn/2 in Kernel.struct/2
    (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3

Environment:

$ elixir -v
Erlang/OTP 21 [erts-10.3.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

Elixir 1.7.4 (compiled with Erlang/OTP 19)

$ cat mix.lock
%{
  "algae": {:hex, :algae, "1.2.3", "6ec177482faa12573a7edc65380fe5c9cdfa52ab32e01d0753aa52425b220827", [:mix], [{:quark, "~> 2.3", [hex: :quark, repo: "hexpm", optional: false]}, {:type_class, "~> 1.2", [hex: :type_class, repo: "hexpm", optional: false]}, {:witchcraft, "~> 1.0", [hex: :witchcraft, repo: "hexpm", optional: false]}], "hexpm"},
  "exceptional": {:hex, :exceptional, "2.1.1", "f18f8c1be63aab7fe8f3000cf2a8678936544fc39af1e2bc4a5c3c9e38cd8899", [:mix], [], "hexpm"},
  "operator": {:hex, :operator, "0.2.0", "b613bdb520a20dbc016d921d444fd2ef2212136be9385d6dca448a2a38d0577f", [:mix], [], "hexpm"},
  "quark": {:hex, :quark, "2.3.0", "3742ddc14441b4930c7067741879cb5b3e4881f391ea0aed7b138f20d16b30bb", [:mix], [], "hexpm"},
  "type_class": {:hex, :type_class, "1.2.5", "b972bb22202e801d83e8670228d9a1476cd2c9fc7da00b7039b19d6d06b17905", [:mix], [{:exceptional, "~> 2.1", [hex: :exceptional, repo: "hexpm", optional: false]}], "hexpm"},
  "witchcraft": {:hex, :witchcraft, "1.0.1", "5db4f9f92be480958a51bee01d3fd283a707576e5e0a0810d4442eac1f47e395", [:mix], [{:exceptional, "~> 2.1", [hex: :exceptional, repo: "hexpm", optional: false]}, {:operator, "~> 0.2", [hex: :operator, repo: "hexpm", optional: false]}, {:quark, "~> 2.2", [hex: :quark, repo: "hexpm", optional: false]}, {:type_class, "~> 1.2", [hex: :type_class, repo: "hexpm", optional: false]}], "hexpm"},
}

revati avatar Jun 07 '19 18:06 revati

I don't think new/1 was meant to be used this way, if at all. Any reason why you want to do Id.new("random value") instead of Id.new()?

PS. The definition for new/1 can be found here: https://github.com/witchcrafters/algae/blob/cd47031ea151ab7880135f729bc0690cab579a20/lib/algae/internal.ex#L115

icidasset avatar Jul 08 '19 10:07 icidasset

Isn't his example precisely what the readme states is how it should be used? https://github.com/witchcrafters/algae#constructor

OvermindDL1 avatar Jul 08 '19 15:07 OvermindDL1

Ah, I misunderstood, thanks @OvermindDL1! I assumed the shorthand syntax (the syntax without a do block, which this person is using) didn't have any fields, but that's not the case (it has one field).

So yeah, that looks like a bug. Will look into this more soon. Thanks for the report @revati !

icidasset avatar Jul 08 '19 15:07 icidasset

Got same behaviour just now, also with newtype form of defdata.

cognivore avatar Oct 14 '21 12:10 cognivore

Similar results on Elixir 1.12.3

florius0 avatar Nov 15 '21 05:11 florius0