-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add compiletime Error type #7951
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Error: tests/neg/singleton-ops-error.scala:18:33 -------------------------------------------------------------------- | ||
18 | val t1: Positive = Positive[-1](-1) // error | ||
| ^ | ||
| The provided value (-1) isn't positive | ||
-- Error: tests/neg/singleton-ops-error.scala:20:23 -------------------------------------------------------------------- | ||
20 | val t3 = Positive[-1](-1) // error | ||
| ^ | ||
| The provided value (-1) isn't positive | ||
-- Error: tests/neg/singleton-ops-error.scala:22:11 -------------------------------------------------------------------- | ||
22 | val err: Error["My error message"] = ??? // error | ||
| ^ | ||
| My error message |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import scala.compiletime.ops.int.{ToString, >=} | ||
import scala.compiletime.ops.string.{+, Error} | ||
|
||
object Test { | ||
type Require[Cond <: Boolean, Msg <: String] = Cond match { | ||
case true => Any | ||
case false => Error[Msg] | ||
} | ||
|
||
opaque type Positive = Int | ||
|
||
object Positive { | ||
type RequirePositive[T <: Int] = Require[T >= 0, "The provided value (" + ToString[T] + ") isn't positive"] | ||
def apply[T <: Int](value: T)(given RequirePositive[T]): Positive = value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After thinking about this, I'm actually surprised this is working. This style is similar to the singleton-ops library and assumes that there is an implicit for |
||
} | ||
|
||
val t0: Positive = Positive[1](1) | ||
val t1: Positive = Positive[-1](-1) // error | ||
val t2 = Positive[1](1) | ||
val t3 = Positive[-1](-1) // error | ||
|
||
val err: Error["My error message"] = ??? // error | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be
Require[T > 0, ...