{"action":"created","issue":{"url":"https://api.github.com/repos/fthomas/refined/issues/239","repository_url":"https://api.github.com/repos/fthomas/refined","labels_url":"https://api.github.com/repos/fthomas/refined/issues/239/labels{/name}","comments_url":"https://api.github.com/repos/fthomas/refined/issues/239/comments","events_url":"https://api.github.com/repos/fthomas/refined/issues/239/events","html_url":"https://github.com/fthomas/refined/issues/239","id":197518082,"number":239,"title":"Implicit conversion for libraries dependent on refined (seperating `auto` to trait and object)","user":{"login":"soronpo","id":8167952,"avatar_url":"https://avatars.githubusercontent.com/u/8167952?v=3","gravatar_id":"","url":"https://api.github.com/users/soronpo","html_url":"https://github.com/soronpo","followers_url":"https://api.github.com/users/soronpo/followers","following_url":"https://api.github.com/users/soronpo/following{/other_user}","gists_url":"https://api.github.com/users/soronpo/gists{/gist_id}","starred_url":"https://api.github.com/users/soronpo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/soronpo/subscriptions","organizations_url":"https://api.github.com/users/soronpo/orgs","repos_url":"https://api.github.com/users/soronpo/repos","events_url":"https://api.github.com/users/soronpo/events{/privacy}","received_events_url":"https://api.github.com/users/soronpo/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-12-25T22:57:23Z","updated_at":"2016-12-26T19:18:58Z","closed_at":null,"body":"Problem:\r\nLibraries which depend on refined types currently force the user to call `import eu.timepit.refined.auto._` to allow standard type variables (literals) to be implicitly converted into their refined types.\r\n\r\nExample:\r\n```\r\n  import eu.timepit.refined.api.Refined\r\n  import eu.timepit.refined.api.Validate\r\n  case class ZeroOrOne()\r\n  implicit val zeroOrOneValidate: Validate.Plain[Int, ZeroOrOne] =\r\n    Validate.fromPredicate(p => p == 0 || p == 1, p => s\"($p is in not binary)\", ZeroOrOne())\r\n\r\n  def libFunc(x : Int Refined ZeroOrOne) : Int = x.get\r\n```\r\n`libFunc(1)` fails without the import\r\n\r\nSolution (as discussed in [gitter](https://gitter.im/fthomas/refined?at=586044c2058ca967378ed2af)):\r\nSplit refined's `auto` into trait and object (see [code](https://github.com/fthomas/refined/blob/master/modules/core/shared/src/main/scala/eu/timepit/refined/auto.scala#L12)). I recommend that the trait's name will be more significant than `auto`.\r\n\r\nThis will allow the user to extend a companion object to the refined type from that trait, thus allowing the implicit conversion. So the example above changes into:\r\n```\r\n  import eu.timepit.refined.api.Refined\r\n  import eu.timepit.refined.api.Validate\r\n  case class ZeroOrOne()\r\n  object ZeroOrOne extends eu.timepit.refined.api.auto //Or other name if chosen\r\n  implicit val zeroOrOneValidate: Validate.Plain[Int, ZeroOrOne] =\r\n    Validate.fromPredicate(p => p == 0 || p == 1, p => s\"($p is in not binary)\", ZeroOrOne())\r\n\r\n  def libFunc(x : Int Refined ZeroOrOne) : Int = x.get\r\n```\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/fthomas/refined/issues/comments/269235971","html_url":"https://github.com/fthomas/refined/issues/239#issuecomment-269235971","issue_url":"https://api.github.com/repos/fthomas/refined/issues/239","id":269235971,"user":{"login":"fthomas","id":141252,"avatar_url":"https://avatars.githubusercontent.com/u/141252?v=3","gravatar_id":"","url":"https://api.github.com/users/fthomas","html_url":"https://github.com/fthomas","followers_url":"https://api.github.com/users/fthomas/followers","following_url":"https://api.github.com/users/fthomas/following{/other_user}","gists_url":"https://api.github.com/users/fthomas/gists{/gist_id}","starred_url":"https://api.github.com/users/fthomas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fthomas/subscriptions","organizations_url":"https://api.github.com/users/fthomas/orgs","repos_url":"https://api.github.com/users/fthomas/repos","events_url":"https://api.github.com/users/fthomas/events{/privacy}","received_events_url":"https://api.github.com/users/fthomas/received_events","type":"User","site_admin":false},"created_at":"2016-12-26T19:18:58Z","updated_at":"2016-12-26T19:18:58Z","body":"Splitting `auto` in a trait and an object is a low-impact change, so we can certainly do this. What I also like to explore is to mix `auto` into the companion of `Refined` so that the conversions `T => Refined[T, P]` and `Refined[T, P] => T` are in the implicit scope of every refined type so that users of the library do not need to mix `auto` into the companion of their predicates."}}
{"actor":{"display_login":"fthomas"}}