{"action":"created","issue":{"url":"https://api.github.com/repos/dry-rb/dry-struct/issues/80","repository_url":"https://api.github.com/repos/dry-rb/dry-struct","labels_url":"https://api.github.com/repos/dry-rb/dry-struct/issues/80/labels{/name}","comments_url":"https://api.github.com/repos/dry-rb/dry-struct/issues/80/comments","events_url":"https://api.github.com/repos/dry-rb/dry-struct/issues/80/events","html_url":"https://github.com/dry-rb/dry-struct/issues/80","id":304009484,"number":80,"title":"Struct gets coerced into **kwargs (when *args is also present)","user":{"login":"sevos","id":120079,"avatar_url":"https://avatars2.githubusercontent.com/u/120079?v=4","gravatar_id":"","url":"https://api.github.com/users/sevos","html_url":"https://github.com/sevos","followers_url":"https://api.github.com/users/sevos/followers","following_url":"https://api.github.com/users/sevos/following{/other_user}","gists_url":"https://api.github.com/users/sevos/gists{/gist_id}","starred_url":"https://api.github.com/users/sevos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sevos/subscriptions","organizations_url":"https://api.github.com/users/sevos/orgs","repos_url":"https://api.github.com/users/sevos/repos","events_url":"https://api.github.com/users/sevos/events{/privacy}","received_events_url":"https://api.github.com/users/sevos/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-03-09T23:11:25Z","updated_at":"2018-03-10T13:09:51Z","closed_at":"2018-03-10T12:24:37Z","author_association":"NONE","body":"Why would we ever define `Dry::Struct#to_hash` method? Its purposed for implicit coercions like in the following example. We should define `#to_h` only, I guess.\r\n\r\nExample:\r\n\r\n```\r\n[1] pry(main)> HueBridge.new.lights.last\r\n=> #<HueBridge::Light id=15 name=\"Sofa\" on=true brightness=144 hue=7676 saturation=199>\r\n[2] pry(main)> a = HueBridge.new.lights.last\r\n=> #<HueBridge::Light id=15 name=\"Sofa\" on=true brightness=144 hue=7676 saturation=199>\r\n[3] pry(main)> def b(*c, **d)\r\n[3] pry(main)*   puts \"args\"\r\n[3] pry(main)*   p c\r\n[3] pry(main)*   puts \"kwargs\"\r\n[3] pry(main)*   puts d\r\n[3] pry(main)* end\r\n=> :b\r\n[4] pry(main)> b(1, a)\r\nargs\r\n[1]\r\nkwargs\r\n{:id=>15, :name=>\"Sofa\", :on=>true, :brightness=>144, :hue=>7676, :saturation=>199}\r\n=> nil\r\n[5] pry(main)> b(a, 1)\r\nargs\r\n[#<HueBridge::Light id=15 name=\"Sofa\" on=true brightness=144 hue=7676 saturation=199>, 1]\r\nkwargs\r\n{}\r\n=> nil\r\n```"},"comment":{"url":"https://api.github.com/repos/dry-rb/dry-struct/issues/comments/372028643","html_url":"https://github.com/dry-rb/dry-struct/issues/80#issuecomment-372028643","issue_url":"https://api.github.com/repos/dry-rb/dry-struct/issues/80","id":372028643,"user":{"login":"sevos","id":120079,"avatar_url":"https://avatars2.githubusercontent.com/u/120079?v=4","gravatar_id":"","url":"https://api.github.com/users/sevos","html_url":"https://github.com/sevos","followers_url":"https://api.github.com/users/sevos/followers","following_url":"https://api.github.com/users/sevos/following{/other_user}","gists_url":"https://api.github.com/users/sevos/gists{/gist_id}","starred_url":"https://api.github.com/users/sevos/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sevos/subscriptions","organizations_url":"https://api.github.com/users/sevos/orgs","repos_url":"https://api.github.com/users/sevos/repos","events_url":"https://api.github.com/users/sevos/events{/privacy}","received_events_url":"https://api.github.com/users/sevos/received_events","type":"User","site_admin":false},"created_at":"2018-03-10T13:09:51Z","updated_at":"2018-03-10T13:09:51Z","author_association":"NONE","body":"You say \"Structs are supposed to work as hashes\", but I wonder where is this requirement coming from.\r\n\r\nIn Ruby neither `Struct` nor `OpenStruct` implements the implicit conversion method `to_hash`. Both implement the explicit conversion method `#to_h`\r\nMore on the differences between implicit and explicit conversion methods [here](https://zverok.github.io/blog/2016-01-18-implicit-vs-expicit.html).\r\n\r\n\r\n```\r\nirb(main):001:0> A = Struct.new(:a, :b)\r\n=> A\r\nirb(main):002:0> def iii(**kwargs)\r\nirb(main):003:1> puts kwargs\r\nirb(main):004:1> end\r\n=> :iii\r\nirb(main):005:0> iii A.new(1,2)\r\nTraceback (most recent call last):\r\n        3: from /Users/sevos/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'\r\n        2: from (irb):5\r\n        1: from (irb):2:in `iii'\r\nArgumentError (wrong number of arguments (given 1, expected 0))\r\nirb(main):006:0> iii A.new(1,2).to_h\r\n{:a=>1, :b=>2}\r\n=> nil\r\nirb(main):009:0> iii OpenStruct.new(a: 1, b: 2)\r\nTraceback (most recent call last):\r\n        3: from /Users/sevos/.rbenv/versions/2.5.0/bin/irb:11:in `<main>'\r\n        2: from (irb):9\r\n        1: from (irb):2:in `iii'\r\nirb(main):010:0> iii OpenStruct.new(a: 1, b: 2).to_h\r\n{:a=>1, :b=>2}\r\n=> nil\r\nirb(main):007:0>\r\n```\r\n\r\nI am afraid that there may be more cases when the struct may be deconstructed into a hash prematurely or accidentally causing us to lose the information about the type of the data it carries. Please tell me what are the cases you find this behaviour useful? Maybe there is really more good than harm...\r\nPersonally I use `Dry::Struct` as a base class for business domain entities and it's more of a better-Struct than a better-Hash, so I would prefer not to deconstruct that objects easily. Of course there should be a way to convert it to a hash, when needed.\r\n\r\nThanks for the idea for the base class, but now I worry more about dry-struct compatibility with other libraries, which may deconstruct it accidentally. "}}
{"actor":{"display_login":"sevos"}}