Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add eu.timepit.refined.W for Scala 3 #1246

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions modules/core/shared/src/main/scala-3.0+/eu/timepit/refined/W.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package eu.timepit.refined

import scala.annotation.compileTimeOnly
import scala.compiletime.ops.int
import scala.compiletime.ops.long
import scala.compiletime.ops.string.CharAt
import scala.compiletime.ops.string.Length
import scala.compiletime.ops.string.Substring
import scala.language.dynamics

@compileTimeOnly("Illegal reference")
object W extends Dynamic {
type StringTail[A <: String] = Substring[A, 1, Length[A]]

type StringLast[A <: String] = CharAt[A, int.-[Length[A], 1]]

type Literal[A <: String] = CharAt[A, 0] match {
case '"' =>
// String
StringLast[A] match {
case '"' =>
Substring[A, 1, int.-[Length[A], 1]]
}
case '\'' =>
// Char
CharAt[A, 2] match {
case '\'' =>
Length[A] match {
case 3 =>
CharAt[A, 1]
}
}
case _ =>
// TODO support another types
// e.g. Double, Float
StringLast[A] match {
case 'L' =>
// Long
StringToLong[Substring[A, 0, int.-[Length[A], 1]]]
case _ =>
// Int
long.ToInt[StringToLong[A]]
}
}

type StringToLong[Input <: String] <: Long = CharAt[Input, 0] match {
case '-' =>
long.Negate[Loop[StringTail[Input], 0L]]
case _ =>
Loop[Input, 0L]
}

type CharToLong[C <: Char] <: Long = C match {
case '0' => 0L
case '1' => 1L
case '2' => 2L
case '3' => 3L
case '4' => 4L
case '5' => 5L
case '6' => 6L
case '7' => 7L
case '8' => 8L
case '9' => 9L
}

type Loop[Input <: String, Acc <: Long] <: Long = Length[Input] match {
case 0 =>
Acc
case _ =>
Loop[
StringTail[Input],
long.+[
long.*[10L, Acc],
CharToLong[
CharAt[Input, 0]
]
]
]
}

def selectDynamic(
selector: String
): None.type { type T = Literal[selector.type] } =
None.asInstanceOf[
None.type { type T = Literal[selector.type] }
]
}

This file was deleted.

This file was deleted.

Loading