{"id":29879452,"url":"https://github.com/typelevel/literally","last_synced_at":"2025-07-31T08:46:11.674Z","repository":{"id":37010467,"uuid":"344956517","full_name":"typelevel/literally","owner":"typelevel","description":"Compile time validation of literal values built from strings","archived":false,"fork":false,"pushed_at":"2025-07-07T11:32:41.000Z","size":278,"stargazers_count":112,"open_issues_count":1,"forks_count":6,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-07-21T17:49:23.343Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typelevel.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2021-03-05T23:06:25.000Z","updated_at":"2025-07-07T11:32:41.000Z","dependencies_parsed_at":"2023-01-17T12:58:34.185Z","dependency_job_id":"a7fbde9b-6e48-42fb-b850-550fb5b0c1b6","html_url":"https://github.com/typelevel/literally","commit_stats":{"total_commits":212,"total_committers":8,"mean_commits":26.5,"dds":0.3584905660377359,"last_synced_commit":"18084677e4e138fe3b4bccad3d564b6d1a1c6fcb"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/typelevel/literally","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelevel%2Fliterally","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelevel%2Fliterally/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelevel%2Fliterally/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelevel%2Fliterally/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typelevel","download_url":"https://codeload.github.com/typelevel/literally/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typelevel%2Fliterally/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267890438,"owners_count":24161422,"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","status":"online","status_checked_at":"2025-07-30T02:00:09.044Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":"2025-07-31T08:46:07.999Z","updated_at":"2025-07-31T08:46:11.652Z","avatar_url":"https://github.com/typelevel.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Literally\n\nCompile time validation of literal values built from strings.\n\n### Rationale\n\nConsider a type like `Port`:\n\n```scala\ncase class Port private (value: Int)\n\nobject Port {\n  val MinValue = 0\n  val MaxValue = 65535\n\n  def fromInt(i: Int): Option[Port] =\n    if (i \u003c MinValue || i \u003e MaxValue) None else Some(new Port(i))\n}\n```\n\nThis library simplifies the definition of literal values which are validated at compilation time:\n\n```scala\nval p: Port = port\"8080\"\n// p: Port = Port(8080)\n\nval q: Port = port\"100000\"\n// \u003cconsole\u003e:17: error: invalid port - must be integer between 0 and 65535\n```\n\nValidation is performed at compile time. This library provides the macro implementations for both Scala 2 and Scala 3 which powers custom string literals.\n\n### Quick Start\n\nTo get started with **literally** in an SBT project add the following dependency to your **build.sbt**:\n```\nlibraryDependencies += \"org.typelevel\" %% \"literally\" % \"\u003cversion\u003e\"\n```\nwhere `\u003cversion\u003e` is the most recent version of **literally**.\n\n### Usage\n\nDefining a custom string literal for a type `A` involves:\n- Implementing an `org.typelevel.literally.Literally[A]` instance\n- Defining an extension method on a `StringContext` which uses the defined `Literally[A]` instance\n\n```scala\nimport org.typelevel.literally.Literally\n\nobject literals:      \n  extension (inline ctx: StringContext)\n    inline def port(inline args: Any*): Port =\n      ${PortLiteral('ctx, 'args)}\n\n  object PortLiteral extends Literally[Port]:\n    def validate(s: String)(using Quotes) =\n      s.toIntOption.flatMap(Port.fromInt) match\n        case None =\u003e Left(s\"invalid port - must be integer between ${Port.MinValue} and ${Port.MaxValue}\")\n        case Some(_) =\u003e Right('{Port.fromInt(${Expr(s)}.toInt).get})\n```\n\nThe same pattern is used for Scala 2, though the syntax for extension methods and macros are a bit different:\n\n```scala\nimport scala.util.Try\nimport org.typelevel.literally.Literally\n\nobject literals {\n  implicit class short(val sc: StringContext) extends AnyVal {\n    def port(args: Any*): Port = macro PortLiteral.make\n  }\n\n  object PortLiteral extends Literally[Port] {\n    def validate(c: Context)(s: String): Either[String, c.Expr[Port]] = {\n      import c.universe.{Try =\u003e _, _}\n      Try(s.toInt).toOption.flatMap(Port.fromInt) match {\n        case None =\u003e Left(s\"invalid port - must be integer between ${Port.MinValue} and ${Port.MaxValue}\")\n        case Some(_) =\u003e Right(c.Expr(q\"Port.fromInt($s.toInt).get\"))\n      }\n    }\n\n    def make(c: Context)(args: c.Expr[Any]*): c.Expr[Port] = apply(c)(args: _*)\n  }\n}\n```\n\nThe `tests` directory in this project has more examples.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypelevel%2Fliterally","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypelevel%2Fliterally","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypelevel%2Fliterally/lists"}