{"id":19972412,"url":"https://github.com/note/mini-refined","last_synced_at":"2025-05-04T01:30:49.922Z","repository":{"id":44655430,"uuid":"363034092","full_name":"note/mini-refined","owner":"note","description":"Simple encoding of refinement types in Scala 3","archived":false,"fork":false,"pushed_at":"2024-06-24T11:55:01.000Z","size":103,"stargazers_count":25,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-08T01:01:39.859Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/note.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-04-30T05:17:48.000Z","updated_at":"2025-01-25T00:09:58.000Z","dependencies_parsed_at":"2023-10-14T06:43:52.432Z","dependency_job_id":"631c64c4-7608-4c9c-9de8-f95fd7688ec1","html_url":"https://github.com/note/mini-refined","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/note%2Fmini-refined","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/note%2Fmini-refined/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/note%2Fmini-refined/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/note%2Fmini-refined/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/note","download_url":"https://codeload.github.com/note/mini-refined/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252276955,"owners_count":21722447,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T03:08:03.189Z","updated_at":"2025-05-04T01:30:49.555Z","avatar_url":"https://github.com/note.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mini-refined\n\n[![example workflow](https://github.com/note/mini-refined/actions/workflows/ci.yml/badge.svg)](https://github.com/note/mini-refined/actions)\n\nA proof of concept of a simple encoding of refinement types in Scala 3.\n\nYou can read about motivation behind and the main concepts in the [blog post](https://msitko.pl/blog/build-your-own-refinement-types-in-scala3.html).\n\n## Quick start\n\nInclude library in `build.sbt`:\n\n```\nlibraryDependencies += \"pl.msitko\" %% \"mini-refined\" % \"0.2.0\"\n```\n\nCommon imports:\n\n```scala\nimport pl.msitko.refined.auto._\nimport pl.msitko.refined.Refined\n```\n\n### Circe integration\n\nTo use circe integration:\n\n```\nlibraryDependencies += \"pl.msitko\" %% \"mini-refined-circe\" % \"0.2.0\"\n```\n\n## Int predicates\n\n```scala\nval a: Int Refined GreaterThan[10] = 5\n// fails compilation with: Validation failed: 5 \u003e 10\n```\n\n```scala\nval a: Int Refined LowerThan[10] = 15\n// fails compilation with: Validation failed: 15 \u003c 10\n```\n\n## String predicates\n\n```scala\nval s: String Refined StartsWith[\"xyz\"] = \"abc\"\n// fails compilation with: Validation failed: abc.startsWith(xyz)\n```\n\n```scala\nval s: String Refined EndsWith[\"xyz\"] = \"abc\"\n// fails compilation with: Validation failed: abc.endsWith(xyz)\n```\n\n## List predicates\n\n```scala\nval as: List[String] Refined Size[GreaterThan[1]] = List(\"a\")\n// fails compilation with: \n// Validation failed: list size doesn't hold predicate: 1 \u003e 1\n```\n\nYou can use any `Int` predicates within `Size` predicate.\n\n## Compose predicates with boolean operators\n\nYou can compose predicates with boolean operators. For example:\n\n```scala\nval c: Int Refined And[GreaterThan[10], LowerThan[20]] = 25\n// fails compilation with: Validation failed: (25 \u003e 10 And 25 \u003c 20), predicate failed: 25 \u003c 20\n```\n\n## Runtime validation\n\nEverything described so far works only for values known at a compile-time. However, values for most variables are coming\nat runtime. For those you need to use `Refined.refineV[T]` which returns `Either[String, T]`. Example:\n\n```scala\ncase class Example(a: Int, b: Int Refined GreaterThan[10])\n\ndef runtime(a: Int, b: Int): Either[String, Example] =\n  Refined.refineV[GreaterThan[10]](b).map(refined =\u003e Example(a, refined))\n```\n\n## Inferring types compatibility\n\n`mini-refined` has some basic rules that enable using more specific types in places where more general types are required.\n\nIn other words, considering such function:\n\n```scala\ndef intFun10(a: Int Refined GreaterThan[10]): Unit = ???\n```\n\nWe can call it with a value of type `Int Refined GreaterThan[20]`, as `mini-refined` recognizes that being greater than 20 implies being greater than 10.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnote%2Fmini-refined","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnote%2Fmini-refined","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnote%2Fmini-refined/lists"}