{"action":"created","issue":{"url":"https://api.github.com/repos/manastech/crystal/issues/1839","labels_url":"https://api.github.com/repos/manastech/crystal/issues/1839/labels{/name}","comments_url":"https://api.github.com/repos/manastech/crystal/issues/1839/comments","events_url":"https://api.github.com/repos/manastech/crystal/issues/1839/events","html_url":"https://github.com/manastech/crystal/issues/1839","id":114157852,"number":1839,"title":"Troubles with generics when they are too general","user":{"login":"waterlink","id":609046,"avatar_url":"https://avatars.githubusercontent.com/u/609046?v=3","gravatar_id":"","url":"https://api.github.com/users/waterlink","html_url":"https://github.com/waterlink","followers_url":"https://api.github.com/users/waterlink/followers","following_url":"https://api.github.com/users/waterlink/following{/other_user}","gists_url":"https://api.github.com/users/waterlink/gists{/gist_id}","starred_url":"https://api.github.com/users/waterlink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waterlink/subscriptions","organizations_url":"https://api.github.com/users/waterlink/orgs","repos_url":"https://api.github.com/users/waterlink/repos","events_url":"https://api.github.com/users/waterlink/events{/privacy}","received_events_url":"https://api.github.com/users/waterlink/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/manastech/crystal/labels/question","name":"question","color":"cc317c"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":12,"created_at":"2015-10-29T22:07:37Z","updated_at":"2015-10-30T21:31:42Z","closed_at":"2015-10-30T20:30:44Z","body":"Code I am trying to write: http://carc.in/#/r/l61 - this is super generic code that tries to avoid knowing types beforehand. With current generic limitations it does not work: hence `use a more specific type`.\r\n\r\nSo I tried to use virtual types a bit to fix this problem and encountered this particular issue: http://carc.in/#/r/l69 - It segfaults at runtime. Though it is not reproducible locally, unfortunately :( Local output is as expected:\r\n\r\n```\r\nresult.unwrap as Array(String) = [\"hello\", \"world\"]\r\nresult.unwrap as Bool = true\r\n```\r\n\r\nThough adding two more `register` calls for the same method and object:\r\n\r\n```crystal\r\nr.register(Message.new(\"split\", s, [\". \"]), Result.new([\"abc\", \"def\"]))\r\nr.register(Message.new(\"split\", s, [\"hello\"]), Result.new([\"world\"]))\r\n```\r\n\r\nMakes it crash both in http://carc.in/#/r/l6b and on my local machine:\r\n\r\n```\r\ncrystal-playground|⇒ ./crash \r\n[1]    32013 segmentation fault (core dumped)  ./crash\r\n```\r\n\r\nAnd finally after introducing `downcast` method into both virtual type and its implementation:\r\n\r\n```crystal\r\nmodule GenericMessage\r\n  # .. abstract definitions here ..\r\n  abstract def downcast\r\nend\r\n\r\nclass Message(T, A)\r\n  # .. class body here ..\r\n  def downcast\r\n    self\r\n  end\r\nend\r\n\r\n# Each usage of GenericMessage now looks like this:\r\ngeneric_message :: GenericMessage\r\ngeneric_message.downcast.hash   # => <hash of message>\r\n\r\n# The same goes for `GenericResult` and `Result(T)`.\r\n```\r\n\r\nAnd it finally worked correctly: http://carc.in/#/r/l6d\r\n\r\nSo my main question is what may cause the problem in the first place? And my other question is: Why introducing this `#downcast` method that will be always implemented as `return self` helps to solve it?"},"comment":{"url":"https://api.github.com/repos/manastech/crystal/issues/comments/152656104","html_url":"https://github.com/manastech/crystal/issues/1839#issuecomment-152656104","issue_url":"https://api.github.com/repos/manastech/crystal/issues/1839","id":152656104,"user":{"login":"waterlink","id":609046,"avatar_url":"https://avatars.githubusercontent.com/u/609046?v=3","gravatar_id":"","url":"https://api.github.com/users/waterlink","html_url":"https://github.com/waterlink","followers_url":"https://api.github.com/users/waterlink/followers","following_url":"https://api.github.com/users/waterlink/following{/other_user}","gists_url":"https://api.github.com/users/waterlink/gists{/gist_id}","starred_url":"https://api.github.com/users/waterlink/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/waterlink/subscriptions","organizations_url":"https://api.github.com/users/waterlink/orgs","repos_url":"https://api.github.com/users/waterlink/repos","events_url":"https://api.github.com/users/waterlink/events{/privacy}","received_events_url":"https://api.github.com/users/waterlink/received_events","type":"User","site_admin":false},"created_at":"2015-10-30T21:31:42Z","updated_at":"2015-10-30T21:31:42Z","body":"Do you mean `module` with `abstract def`s ?"}}