package anyvals
- Alphabetic
- Public
- Protected
Type Members
- trait CompileTimeAssertions extends AnyRef
Trait providing assertion methods that can be called at compile time from macros to validate literals in source code.
Trait providing assertion methods that can be called at compile time from macros to validate literals in source code.
The intent of
CompileTimeAssertions
is to make it easier to createAnyVal
s that restrict the values of types for which Scala supports literals:Int
,Long
,Float
,Double
,Char
, andString
. For example, if you are using odd integers in many places in your code, you might have validity checks scattered throughout your code. Here's an example of a method that both requires an oddInt
is passed (as a precondition, and ensures an odd *Int
is returned (as a postcondition):def nextOdd(i: Int): Int = { def isOdd(x: Int): Boolean = x.abs % 2 == 1 require(isOdd(i)) (i + 2) ensuring (isOdd(_)) }
In either the precondition or postcondition check fails, an exception will be thrown at runtime. If you have many methods like this you may want to create a type to represent an odd
Int
, so that the checking for validity errors is isolated in just one place. By using anAnyVal
you can avoid boxing theInt
, which may be more efficient. This might look like:final class OddInt private (val value: Int) extends AnyVal { override def toString: String = s"OddInt($value)" }
object OddInt { def apply(value: Int): OddInt = { require(value.abs % 2 == 1) new OddInt(value) } }An
AnyVal
cannot have any constructor code, so to ensure that anyInt
passed to theOddInt
constructor is actually odd, the constructor must be private. That way the only way to construct a newOddInt
is via theapply
factory method in theOddInt
companion object, which can require that the value be odd. This design eliminates the need for placingrequire
andensuring
clauses anywhere else that oddInt
s are needed, because the type promises the constraint. ThenextOdd
method could, therefore, be rewritten as:def nextOdd(oi: OddInt): OddInt = OddInt(oi.value + 2)
Using the compile-time assertions provided by this trait, you can construct a factory method implemented via a macro that causes a compile failure if
OddInt.apply
is passed anything besides an oddInt
literal. ClassOddInt
would look exactly the same as before:final class OddInt private (val value: Int) extends AnyVal { override def toString: String = s"OddInt($value)" }
In the companion object, however, the
apply
method would be implemented in terms of a macro. Because theapply
method will only work with literals, you'll need a second method that can work an any expression of typeInt
. We recommend afrom
method that returns anOption[OddInt]
that returnsSome[OddInt}
if the passedInt
is odd, else returnsNone
, and anensuringValid
method that returns anOddInt
if the passedInt
is valid, else throwsAssertionError
.object OddInt {
// The from factory method validates at run time def from(value: Int): Option[OddInt] = if (OddIntMacro.isValid(value)) Some(new OddInt(value)) else None
// The ensuringValid factory method validates at run time, but throws // an AssertionError if invalid def ensuringValid(value: Int): OddInt = if (OddIntMacro.isValid(value)) new OddInt(value) else { throw new AssertionError(s"$value was not a valid OddInt") }
// The apply factory method validates at compile time import scala.language.experimental.macros def apply(value: Int): OddInt = macro OddIntMacro.apply }The
apply
method refers to a macro implementation method in classPosIntMacro
. The macro implementation of any such method can look very similar to this one. The only changes you'd need to make is theisValid
method implementation and the text of the error messages.import org.scalactic.anyvals.CompileTimeAssertions import reflect.macros.Context
object OddIntMacro extends CompileTimeAssertions {
// Validation method used at both compile- and run-time def isValid(i: Int): Boolean = i.abs % 2 == 1
// Apply macro that performs a compile-time assertion def apply(c: Context)(value: c.Expr[Int]): c.Expr[OddInt] = {
// Prepare potential compiler error messages val notValidMsg = "OddInt.apply can only be invoked on odd Int literals, like OddInt(3)." val notLiteralMsg = "OddInt.apply can only be invoked on Int literals, like " + "OddInt(3). Please use OddInt.from instead."
// Validate via a compile-time assertion ensureValidIntLiteral(c)(value, notValidMsg, notLiteralMsg)(isValid)
// Validated, so rewrite the apply call to a from call c.universe.reify { OddInt.ensuringValid(value.splice) } } }The
isValid
method just takes the underlying type and returnstrue
if it is valid, elsefalse
. This method is placed here so the same valiation code can be used both in thefrom
method at runtime and theapply
macro at compile time. Theapply
actually does just two things. It calls aensureValidIntLiteral
, performing a compile-time assertion that value passed toapply
is anInt
literal that is valid (in this case, odd). If the assertion fails,ensureValidIntLiteral
will complete abruptly with an exception that will contain an appropriate error message (one of the two you passed in) and cause a compiler error with that message. If the assertion succeeds,ensureValidIntLiteral
will just return normally. The next line of code will then execute. This line of code must construct an AST (abstract syntax tree) of code that will replace theOddInt.apply
invocation. We invoke the other factory method that either returns anOddInt
or throws anAssertionError
, since we've proven at compile time that the call will succeed.You may wish to use quasi-quotes instead of reify. The reason we use reify is that this also works on 2.10 without any additional plugin (i.e., you don't need macro paradise), and Scalactic supports 2.10.
- final class FiniteDouble extends AnyVal
An
AnyVal
for finiteDouble
s.An
AnyVal
for finiteDouble
s.Because
FiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
FiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingFiniteDouble.apply
with a literalDouble
value will either produce a validFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> FiniteDouble(1.1) res1: org.scalactic.anyvals.FiniteDouble = FiniteDouble(1.1) scala> FiniteDouble(Finite.PositiveInfinity) <console>:14: error: FiniteDouble.apply can only be invoked on a finite (i != Double.NegativeInfinity && i != Double.PositiveInfinity && !i.isNaN) floating point literal, like FiniteDouble(1.1). FiniteDouble(Finite.PositiveInfinity) ^
FiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,FiniteDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> FiniteDouble(x) <console>:15: error: FiniteDouble.apply can only be invoked on a floating point literal, like FiniteDouble(1.1). Please use FiniteDouble.from instead. FiniteDouble(x) ^
The
FiniteDouble.from
factory method will inspect the value at runtime and return anOption[FiniteDouble]
. If the value is valid,FiniteDouble.from
will return aSome[FiniteDouble]
, else it will return aNone
. Here's an example:scala> FiniteDouble.from(x) res4: Option[org.scalactic.anyvals.FiniteDouble] = Some(FiniteDouble(1.1)) scala> val y = Finite.PositiveInfinity y: Double = Finite.PositiveInfinity scala> FiniteDouble.from(y) res5: Option[org.scalactic.anyvals.FiniteDouble] = None
The
FiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireFiniteDouble
, and get the same compile-time checking you get when callingFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: FiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.FiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(Finite.PositiveInfinity) <console>:15: error: FiniteDouble.apply can only be invoked on a finite (i != Double.NegativeInfinity && i != Double.PositiveInfinity && !i.isNaN) floating point literal, like FiniteDouble(1.1). invert(Finite.PositiveInfinity) ^
This example also demonstrates that the
FiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aFiniteDouble
(the type ofpos
), you can still subtractpos
, because theFiniteDouble
will be implicitly widened toDouble
. - final class FiniteFloat extends AnyVal
An
AnyVal
for finiteFloat
s.An
AnyVal
for finiteFloat
s.Because
FiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
FiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingFiniteFloat.apply
with a literalFloat
value will either produce a validFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> FiniteFloat(42.1fF) res0: org.scalactic.anyvals.FiniteFloat = FiniteFloat(42.1f) scala> FiniteFloat(Float.PositiveInfinityF) <console>:14: error: FiniteFloat.apply can only be invoked on a finite (i != Float.NegativeInfinity && i != Float.PositiveInfinity && !i.isNaN) floating point literal, like FiniteFloat(42.1fF). FiniteFloat(42.1fF) ^
FiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,FiniteFloat.from
, instead:scala> val x = 42.1fF x: Float = 42.1f scala> FiniteFloat(x) <console>:15: error: FiniteFloat.apply can only be invoked on a floating point literal, like FiniteFloat(42.1fF). Please use FiniteFloat.from instead. FiniteFloat(x) ^
The
FiniteFloat.from
factory method will inspect the value at runtime and return anOption[FiniteFloat]
. If the value is valid,FiniteFloat.from
will return aSome[FiniteFloat]
, else it will return aNone
. Here's an example:scala> FiniteFloat.from(x) res3: Option[org.scalactic.anyvals.FiniteFloat] = Some(FiniteFloat(42.1f)) scala> val y = Float.PositiveInfinityF y: Float = Float.PositiveInfinity scala> FiniteFloat.from(y) res4: Option[org.scalactic.anyvals.FiniteFloat] = None
The
FiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireFiniteFloat
, and get the same compile-time checking you get when callingFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: FiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.FiniteFloat)Float scala> invert(42.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(Float.PositiveInfinityF) <console>:15: error: FiniteFloat.apply can only be invoked on a finite (i != Float.NegativeInfinity && i != Float.PositiveInfinity && !i.isNaN) floating point literal, like FiniteFloat(42.1fF). invert(0.0F) ^ scala> invert(Float.PositiveInfinityF) <console>:15: error: FiniteFloat.apply can only be invoked on a finite (i != Float.NegativeInfinity && i != Float.PositiveInfinity && !i.isNaN) floating point literal, like FiniteFloat(42.1fF). invert(Float.PositiveInfinityF) ^
This example also demonstrates that the
FiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aFiniteFloat
(the type ofpos
), you can still subtractpos
, because theFiniteFloat
will be implicitly widened toFloat
. - final class NegDouble extends AnyVal
An
AnyVal
for negativeDouble
s.An
AnyVal
for negativeDouble
s.Because
NegDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NegDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegDouble.apply
with a literalDouble
value will either produce a validNegDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegDouble(-1.1) res1: org.scalactic.anyvals.NegDouble = NegDouble(-1.1) scala> NegDouble(1.1) <console>:14: error: NegDouble.apply can only be invoked on a negative (i < 0.0) floating point literal, like NegDouble(-1.1). NegDouble(1.1) ^
NegDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NegDouble.from
, instead:scala> val x = -1.1 x: Double = -1.1 scala> NegDouble(x) <console>:15: error: NegDouble.apply can only be invoked on a floating point literal, like NegDouble(-1.1). Please use NegDouble.from instead. NegDouble(x) ^
The
NegDouble.from
factory method will inspect the value at runtime and return anOption[NegDouble]
. If the value is valid,NegDouble.from
will return aSome[NegDouble]
, else it will return aNone
. Here's an example:scala> NegDouble.from(x) res4: Option[org.scalactic.anyvals.NegDouble] = Some(NegDouble(-1.1)) scala> val y = 1.1 y: Double = 1.1 scala> NegDouble.from(y) res5: Option[org.scalactic.anyvals.NegDouble] = None
The
NegDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNegDouble
, and get the same compile-time checking you get when callingNegDouble.apply
explicitly. Here's an example:scala> def invert(pos: NegDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(1.1) <console>:15: error: NegDouble.apply can only be invoked on a negative (i < 0.0) floating point literal, like NegDouble(-1.1). invert(1.1) ^
This example also demonstrates that the
NegDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNegDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNegDouble
(the type ofpos
), you can still subtractpos
, because theNegDouble
will be implicitly widened toDouble
. - final class NegFiniteDouble extends AnyVal
An
AnyVal
for finite negativeDouble
s.An
AnyVal
for finite negativeDouble
s.Because
NegFiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NegFiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegFiniteDouble.apply
with a literalDouble
value will either produce a validNegFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegFiniteDouble(-1.1) res1: org.scalactic.anyvals.NegFiniteDouble = NegFiniteDouble(-1.1) scala> NegFiniteDouble(1.1) <console>:14: error: NegFiniteDouble.apply can only be invoked on a finite negative (i < 0.0 && i != Double.NegativeInfinity) floating point literal, like NegFiniteDouble(-1.1). NegFiniteDouble(1.1) ^
NegFiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NegFiniteDouble.from
, instead:scala> val x = -1.1 x: Double = -1.1 scala> NegFiniteDouble(x) <console>:15: error: NegFiniteDouble.apply can only be invoked on a floating point literal, like NegFiniteDouble(-1.1). Please use NegFiniteDouble.from instead. NegFiniteDouble(x) ^
The
NegFiniteDouble.from
factory method will inspect the value at runtime and return anOption[NegFiniteDouble]
. If the value is valid,NegFiniteDouble.from
will return aSome[NegFiniteDouble]
, else it will return aNone
. Here's an example:scala> NegFiniteDouble.from(x) res4: Option[org.scalactic.anyvals.NegFiniteDouble] = Some(NegFiniteDouble(-1.1)) scala> val y = 1.1 y: Double = 1.1 scala> NegFiniteDouble.from(y) res5: Option[org.scalactic.anyvals.NegFiniteDouble] = None
The
NegFiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNegFiniteDouble
, and get the same compile-time checking you get when callingNegFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: NegFiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegFiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(1.1) <console>:15: error: NegFiniteDouble.apply can only be invoked on a finite negative (i < 0.0 && i != Double.NegativeInfinity) floating point literal, like NegFiniteDouble(-1.1). invert(1.1) ^
This example also demonstrates that the
NegFiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNegFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNegFiniteDouble
(the type ofpos
), you can still subtractpos
, because theNegFiniteDouble
will be implicitly widened toDouble
. - final class NegFiniteFloat extends AnyVal
An
AnyVal
for finite negativeFloat
s.An
AnyVal
for finite negativeFloat
s.Note: a
NegFiniteFloat
may not equal 0.0. If you want negative number or 0, use NegZFiniteFloat.Because
NegFiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NegFiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegFiniteFloat.apply
with a literalFloat
value will either produce a validNegFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegFiniteFloat(-42.1fF) res0: org.scalactic.anyvals.NegFiniteFloat = NegFiniteFloat(-42.1f) scala> NegFiniteFloat(0.0fF) <console>:14: error: NegFiniteFloat.apply can only be invoked on a finite negative (i < 0.0f && i != Float.NegativeInfinity) floating point literal, like NegFiniteFloat(-42.1fF). NegFiniteFloat(-42.1fF) ^
NegFiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NegFiniteFloat.from
, instead:scala> val x = -42.1fF x: Float = -42.1f scala> NegFiniteFloat(x) <console>:15: error: NegFiniteFloat.apply can only be invoked on a floating point literal, like NegFiniteFloat(-42.1fF). Please use NegFiniteFloat.from instead. NegFiniteFloat(x) ^
The
NegFiniteFloat.from
factory method will inspect the value at runtime and return anOption[NegFiniteFloat]
. If the value is valid,NegFiniteFloat.from
will return aSome[NegFiniteFloat]
, else it will return aNone
. Here's an example:scala> NegFiniteFloat.from(x) res3: Option[org.scalactic.anyvals.NegFiniteFloat] = Some(NegFiniteFloat(-42.1f)) scala> val y = 0.0fF y: Float = 0.0f scala> NegFiniteFloat.from(y) res4: Option[org.scalactic.anyvals.NegFiniteFloat] = None
The
NegFiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNegFiniteFloat
, and get the same compile-time checking you get when callingNegFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: NegFiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegFiniteFloat)Float scala> invert(-42.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0fF) <console>:15: error: NegFiniteFloat.apply can only be invoked on a finite negative (i < 0.0f && i != Float.NegativeInfinity) floating point literal, like NegFiniteFloat(-42.1fF). invert(0.0F) ^ scala> invert(0.0fF) <console>:15: error: NegFiniteFloat.apply can only be invoked on a finite negative (i < 0.0f && i != Float.NegativeInfinity) floating point literal, like NegFiniteFloat(-42.1fF). invert(0.0fF) ^
This example also demonstrates that the
NegFiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNegFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNegFiniteFloat
(the type ofpos
), you can still subtractpos
, because theNegFiniteFloat
will be implicitly widened toFloat
. - final class NegFloat extends AnyVal
An
AnyVal
for megativeFloat
s.An
AnyVal
for megativeFloat
s.Note: a
NegFloat
may not equal 0.0. If you want negative number or 0, use NegZFloat.Because
NegFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NegFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegFloat.apply
with a literalFloat
value will either produce a validNegFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegFloat(-42.1fF) res0: org.scalactic.anyvals.NegFloat = NegFloat(-42.1f) scala> NegFloat(0.0fF) <console>:14: error: NegFloat.apply can only be invoked on a megative (i < 0.0f) floating point literal, like NegFloat(-42.1fF). NegFloat(-42.1fF) ^
NegFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NegFloat.from
, instead:scala> val x = -42.1fF x: Float = -42.1f scala> NegFloat(x) <console>:15: error: NegFloat.apply can only be invoked on a floating point literal, like NegFloat(-42.1fF). Please use NegFloat.from instead. NegFloat(x) ^
The
NegFloat.from
factory method will inspect the value at runtime and return anOption[NegFloat]
. If the value is valid,NegFloat.from
will return aSome[NegFloat]
, else it will return aNone
. Here's an example:scala> NegFloat.from(x) res3: Option[org.scalactic.anyvals.NegFloat] = Some(NegFloat(-42.1f)) scala> val y = 0.0fF y: Float = 0.0f scala> NegFloat.from(y) res4: Option[org.scalactic.anyvals.NegFloat] = None
The
NegFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNegFloat
, and get the same compile-time checking you get when callingNegFloat.apply
explicitly. Here's an example:scala> def invert(pos: NegFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegFloat)Float scala> invert(-42.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0fF) <console>:15: error: NegFloat.apply can only be invoked on a megative (i < 0.0f) floating point literal, like NegFloat(-42.1fF). invert(0.0F) ^ scala> invert(0.0fF) <console>:15: error: NegFloat.apply can only be invoked on a megative (i < 0.0f) floating point literal, like NegFloat(-42.1fF). invert(0.0fF) ^
This example also demonstrates that the
NegFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNegFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNegFloat
(the type ofpos
), you can still subtractpos
, because theNegFloat
will be implicitly widened toFloat
. - final class NegInt extends AnyVal
An
AnyVal
for negativeInt
s.An
AnyVal
for negativeInt
s.Note: a
NegInt
may not equal 0. If you want negative number or 0, use NegZInt.Because
NegInt
is anAnyVal
it will usually be as efficient as anInt
, being boxed only when anInt
would have been boxed.The
NegInt.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegInt.apply
with a literalInt
value will either produce a validNegInt
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegInt(-42) res0: org.scalactic.anyvals.NegInt = NegInt(-42) scala> NegInt(0) <console>:14: error: NegInt.apply can only be invoked on a negative (i < 0) literal, like NegInt(-42). NegInt(0) ^
NegInt.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegInt.apply
, you'll get a compiler error that suggests you use a different factor method,NegInt.from
, instead:scala> val x = 1 x: Int = 1 scala> NegInt(x) <console>:15: error: NegInt.apply can only be invoked on a negative integer literal, like NegInt(-42). Please use NegInt.from instead. NegInt(x) ^
The
NegInt.from
factory method will inspect the value at runtime and return anOption[NegInt]
. If the value is valid,NegInt.from
will return aSome[NegInt]
, else it will return aNone
. Here's an example:scala> NegInt.from(x) res3: Option[org.scalactic.anyvals.NegInt] = Some(NegInt(1)) scala> val y = 0 y: Int = 0 scala> NegInt.from(y) res4: Option[org.scalactic.anyvals.NegInt] = None
The
NegInt.apply
factory method is marked implicit, so that you can pass literalInt
s into methods that requireNegInt
, and get the same compile-time checking you get when callingNegInt.apply
explicitly. Here's an example:scala> def invert(pos: NegInt): Int = Int.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegInt)Int scala> invert(1) res0: Int = 2147483646 scala> invert(Int.MaxValue) res1: Int = 0 scala> invert(0) <console>:15: error: NegInt.apply can only be invoked on a negative (i < 0) integer literal, like NegInt(-42). invert(0) ^ scala> invert(-1) <console>:15: error: NegInt.apply can only be invoked on a negative (i < 0) integer literal, like NegInt(-42). invert(-1) ^
This example also demonstrates that the
NegInt
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromInt
to Float in Scala can lose precision.) This makes it convenient to use aNegInt
where anInt
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Int.MaxValue - pos
. AlthoughInt.MaxValue
is anInt
, which has no-
method that takes aNegInt
(the type ofpos
), you can still subtractpos
, because theNegInt
will be implicitly widened toInt
. - final class NegLong extends AnyVal
An
AnyVal
for negativeLong
s.An
AnyVal
for negativeLong
s.Note: a
NegLong
may not equal 0. If you want negative number or 0, use NegZLong.Because
NegLong
is anAnyVal
it will usually be as efficient as anLong
, being boxed only when anLong
would have been boxed.The
NegLong.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegLong.apply
with a literalLong
value will either produce a validNegLong
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegLong(-42L) res0: org.scalactic.anyvals.NegLong = NegLong(-42L) scala> NegLong(0L) <console>:14: error: NegLong.apply can only be invoked on a negative (i < 0L) integer literal, like NegLong(-42L). NegLong(0L) ^
NegLong.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegLong.apply
, you'll get a compiler error that suggests you use a different factor method,NegLong.from
, instead:scala> val x = -42LL x: Long = -42L scala> NegLong(x) <console>:15: error: NegLong.apply can only be invoked on an long literal, like NegLong(-42L). Please use NegLong.from instead. NegLong(x) ^
The
NegLong.from
factory method will inspect the value at runtime and return anOption[NegLong]
. If the value is valid,NegLong.from
will return aSome[NegLong]
, else it will return aNone
. Here's an example:scala> NegLong.from(x) res3: Option[org.scalactic.anyvals.NegLong] = Some(NegLong(-42L)) scala> val y = 0LL y: Long = 0L scala> NegLong.from(y) res4: Option[org.scalactic.anyvals.NegLong] = None
The
NegLong.apply
factory method is marked implicit, so that you can pass literalLong
s into methods that requireNegLong
, and get the same compile-time checking you get when callingNegLong.apply
explicitly. Here's an example:scala> def invert(pos: NegLong): Long = Long.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegLong)Long scala> invert(1L) res5: Long = 9223372036854775806 scala> invert(Long.MaxValue) res6: Long = 0 scala> invert(0LL) <console>:15: error: NegLong.apply can only be invoked on a negative (i < 0L) integer literal, like NegLong(-42LL). invert(0LL) ^
This example also demonstrates that the
NegLong
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromLong
to Double in Scala can lose precision.) This makes it convenient to use aNegLong
where aLong
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Long.MaxValue - pos
. AlthoughLong.MaxValue
is aLong
, which has no-
method that takes aNegLong
(the type ofpos
), you can still subtractpos
, because theNegLong
will be implicitly widened toLong
. - final class NegZDouble extends AnyVal
An
AnyVal
for non-positiveDouble
s.An
AnyVal
for non-positiveDouble
s.Because
NegZDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NegZDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZDouble.apply
with a literalDouble
value will either produce a validNegZDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZDouble(-1.1) res1: org.scalactic.anyvals.NegZDouble = NegZDouble(-1.1) scala> NegZDouble(1.1) <console>:14: error: NegZDouble.apply can only be invoked on a non-positive (i <= 0.0) floating point literal, like NegZDouble(-1.1). NegZDouble(1.1) ^
NegZDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NegZDouble.from
, instead:scala> val x = -1.1 x: Double = -1.1 scala> NegZDouble(x) <console>:15: error: NegZDouble.apply can only be invoked on a floating point literal, like NegZDouble(-1.1). Please use NegZDouble.from instead. NegZDouble(x) ^
The
NegZDouble.from
factory method will inspect the value at runtime and return anOption[NegZDouble]
. If the value is valid,NegZDouble.from
will return aSome[NegZDouble]
, else it will return aNone
. Here's an example:scala> NegZDouble.from(x) res4: Option[org.scalactic.anyvals.NegZDouble] = Some(NegZDouble(-1.1)) scala> val y = 1.1 y: Double = 1.1 scala> NegZDouble.from(y) res5: Option[org.scalactic.anyvals.NegZDouble] = None
The
NegZDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNegZDouble
, and get the same compile-time checking you get when callingNegZDouble.apply
explicitly. Here's an example:scala> def invert(pos: NegZDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(1.1) <console>:15: error: NegZDouble.apply can only be invoked on a non-positive (i <= 0.0) floating point literal, like NegZDouble(-1.1). invert(1.1) ^
This example also demonstrates that the
NegZDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNegZDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNegZDouble
(the type ofpos
), you can still subtractpos
, because theNegZDouble
will be implicitly widened toDouble
. - final class NegZFiniteDouble extends AnyVal
An
AnyVal
for finite non-positiveDouble
s.An
AnyVal
for finite non-positiveDouble
s.Because
NegZFiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NegZFiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZFiniteDouble.apply
with a literalDouble
value will either produce a validNegZFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZFiniteDouble(-1.1) res1: org.scalactic.anyvals.NegZFiniteDouble = NegZFiniteDouble(-1.1) scala> NegZFiniteDouble(1.1) <console>:14: error: NegZFiniteDouble.apply can only be invoked on a finite non-positive (i <= 0.0 && i != Double.NegativeInfinity) floating point literal, like NegZFiniteDouble(-1.1). NegZFiniteDouble(1.1) ^
NegZFiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NegZFiniteDouble.from
, instead:scala> val x = -1.1 x: Double = -1.1 scala> NegZFiniteDouble(x) <console>:15: error: NegZFiniteDouble.apply can only be invoked on a floating point literal, like NegZFiniteDouble(-1.1). Please use NegZFiniteDouble.from instead. NegZFiniteDouble(x) ^
The
NegZFiniteDouble.from
factory method will inspect the value at runtime and return anOption[NegZFiniteDouble]
. If the value is valid,NegZFiniteDouble.from
will return aSome[NegZFiniteDouble]
, else it will return aNone
. Here's an example:scala> NegZFiniteDouble.from(x) res4: Option[org.scalactic.anyvals.NegZFiniteDouble] = Some(NegZFiniteDouble(-1.1)) scala> val y = 1.1 y: Double = 1.1 scala> NegZFiniteDouble.from(y) res5: Option[org.scalactic.anyvals.NegZFiniteDouble] = None
The
NegZFiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNegZFiniteDouble
, and get the same compile-time checking you get when callingNegZFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: NegZFiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZFiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(1.1) <console>:15: error: NegZFiniteDouble.apply can only be invoked on a finite non-positive (i <= 0.0 && i != Double.NegativeInfinity) floating point literal, like NegZFiniteDouble(-1.1). invert(1.1) ^
This example also demonstrates that the
NegZFiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNegZFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNegZFiniteDouble
(the type ofpos
), you can still subtractpos
, because theNegZFiniteDouble
will be implicitly widened toDouble
. - final class NegZFiniteFloat extends AnyVal
An
AnyVal
for finite non-positiveFloat
s.An
AnyVal
for finite non-positiveFloat
s.Because
NegZFiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NegZFiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZFiniteFloat.apply
with a literalFloat
value will either produce a validNegZFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZFiniteFloat(-1.1fF) res0: org.scalactic.anyvals.NegZFiniteFloat = NegZFiniteFloat(-1.1f) scala> NegZFiniteFloat(1.1fF) <console>:14: error: NegZFiniteFloat.apply can only be invoked on a finite non-positive (i <= 0.0f && i != Float.NegativeInfinity) floating point literal, like NegZFiniteFloat(-1.1fF). NegZFiniteFloat(-1.1fF) ^
NegZFiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NegZFiniteFloat.from
, instead:scala> val x = -1.1fF x: Float = -1.1f scala> NegZFiniteFloat(x) <console>:15: error: NegZFiniteFloat.apply can only be invoked on a floating point literal, like NegZFiniteFloat(-1.1fF). Please use NegZFiniteFloat.from instead. NegZFiniteFloat(x) ^
The
NegZFiniteFloat.from
factory method will inspect the value at runtime and return anOption[NegZFiniteFloat]
. If the value is valid,NegZFiniteFloat.from
will return aSome[NegZFiniteFloat]
, else it will return aNone
. Here's an example:scala> NegZFiniteFloat.from(x) res3: Option[org.scalactic.anyvals.NegZFiniteFloat] = Some(NegZFiniteFloat(-1.1f)) scala> val y = 1.1fF y: Float = 1.1f scala> NegZFiniteFloat.from(y) res4: Option[org.scalactic.anyvals.NegZFiniteFloat] = None
The
NegZFiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNegZFiniteFloat
, and get the same compile-time checking you get when callingNegZFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: NegZFiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZFiniteFloat)Float scala> invert(-1.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(1.1fF) <console>:15: error: NegZFiniteFloat.apply can only be invoked on a finite non-positive (i <= 0.0f && i != Float.NegativeInfinity) floating point literal, like NegZFiniteFloat(-1.1fF). invert(0.0F) ^ scala> invert(1.1fF) <console>:15: error: NegZFiniteFloat.apply can only be invoked on a finite non-positive (i <= 0.0f && i != Float.NegativeInfinity) floating point literal, like NegZFiniteFloat(-1.1fF). invert(1.1fF) ^
This example also demonstrates that the
NegZFiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNegZFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNegZFiniteFloat
(the type ofpos
), you can still subtractpos
, because theNegZFiniteFloat
will be implicitly widened toFloat
. - final class NegZFloat extends AnyVal
An
AnyVal
for non-positiveFloat
s.An
AnyVal
for non-positiveFloat
s.Because
NegZFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NegZFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZFloat.apply
with a literalFloat
value will either produce a validNegZFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZFloat(-1.1fF) res0: org.scalactic.anyvals.NegZFloat = NegZFloat(-1.1f) scala> NegZFloat(1.1fF) <console>:14: error: NegZFloat.apply can only be invoked on a non-positive (i <= 0.0f) floating point literal, like NegZFloat(-1.1fF). NegZFloat(-1.1fF) ^
NegZFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NegZFloat.from
, instead:scala> val x = -1.1fF x: Float = -1.1f scala> NegZFloat(x) <console>:15: error: NegZFloat.apply can only be invoked on a floating point literal, like NegZFloat(-1.1fF). Please use NegZFloat.from instead. NegZFloat(x) ^
The
NegZFloat.from
factory method will inspect the value at runtime and return anOption[NegZFloat]
. If the value is valid,NegZFloat.from
will return aSome[NegZFloat]
, else it will return aNone
. Here's an example:scala> NegZFloat.from(x) res3: Option[org.scalactic.anyvals.NegZFloat] = Some(NegZFloat(-1.1f)) scala> val y = 1.1fF y: Float = 1.1f scala> NegZFloat.from(y) res4: Option[org.scalactic.anyvals.NegZFloat] = None
The
NegZFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNegZFloat
, and get the same compile-time checking you get when callingNegZFloat.apply
explicitly. Here's an example:scala> def invert(pos: NegZFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZFloat)Float scala> invert(-1.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(1.1fF) <console>:15: error: NegZFloat.apply can only be invoked on a non-positive (i <= 0.0f) floating point literal, like NegZFloat(-1.1fF). invert(0.0F) ^ scala> invert(1.1fF) <console>:15: error: NegZFloat.apply can only be invoked on a non-positive (i <= 0.0f) floating point literal, like NegZFloat(-1.1fF). invert(1.1fF) ^
This example also demonstrates that the
NegZFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNegZFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNegZFloat
(the type ofpos
), you can still subtractpos
, because theNegZFloat
will be implicitly widened toFloat
. - final class NegZInt extends AnyVal
An
AnyVal
for non-positiveInt
s.An
AnyVal
for non-positiveInt
s.Because
NegZInt
is anAnyVal
it will usually be as efficient as anInt
, being boxed only when anInt
would have been boxed.The
NegZInt.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZInt.apply
with a literalInt
value will either produce a validNegZInt
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZInt(-42) res0: org.scalactic.anyvals.NegZInt = NegZInt(-42) scala> NegZInt(1) <console>:14: error: NegZInt.apply can only be invoked on a non-positive (i <= 0) literal, like NegZInt(-42). NegZInt(1) ^
NegZInt.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZInt.apply
, you'll get a compiler error that suggests you use a different factor method,NegZInt.from
, instead:scala> val x = 1 x: Int = 1 scala> NegZInt(x) <console>:15: error: NegZInt.apply can only be invoked on a non-positive integer literal, like NegZInt(-42). Please use NegZInt.from instead. NegZInt(x) ^
The
NegZInt.from
factory method will inspect the value at runtime and return anOption[NegZInt]
. If the value is valid,NegZInt.from
will return aSome[NegZInt]
, else it will return aNone
. Here's an example:scala> NegZInt.from(x) res3: Option[org.scalactic.anyvals.NegZInt] = Some(NegZInt(1)) scala> val y = 0 y: Int = 0 scala> NegZInt.from(y) res4: Option[org.scalactic.anyvals.NegZInt] = None
The
NegZInt.apply
factory method is marked implicit, so that you can pass literalInt
s into methods that requireNegZInt
, and get the same compile-time checking you get when callingNegZInt.apply
explicitly. Here's an example:scala> def invert(pos: NegZInt): Int = Int.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZInt)Int scala> invert(1) res0: Int = 2147483646 scala> invert(Int.MaxValue) res1: Int = 0 scala> invert(0) <console>:15: error: NegZInt.apply can only be invoked on a non-positive (i <= 0) integer literal, like NegZInt(-42). invert(0) ^ scala> invert(-1) <console>:15: error: NegZInt.apply can only be invoked on a non-positive (i <= 0) integer literal, like NegZInt(-42). invert(-1) ^
This example also demonstrates that the
NegZInt
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromInt
to Float in Scala can lose precision.) This makes it convenient to use aNegZInt
where anInt
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Int.MaxValue - pos
. AlthoughInt.MaxValue
is anInt
, which has no-
method that takes aNegZInt
(the type ofpos
), you can still subtractpos
, because theNegZInt
will be implicitly widened toInt
. - final class NegZLong extends AnyVal
An
AnyVal
for non-positiveLong
s.An
AnyVal
for non-positiveLong
s.Because
NegZLong
is anAnyVal
it will usually be as efficient as anLong
, being boxed only when anLong
would have been boxed.The
NegZLong.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNegZLong.apply
with a literalLong
value will either produce a validNegZLong
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NegZLong(-42L) res0: org.scalactic.anyvals.NegZLong = NegZLong(-42L) scala> NegZLong(-1L) <console>:14: error: NegZLong.apply can only be invoked on a non-positive (i <= 0L) integer literal, like NegZLong(-42L). NegZLong(-1L) ^
NegZLong.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNegZLong.apply
, you'll get a compiler error that suggests you use a different factor method,NegZLong.from
, instead:scala> val x = -42L x: Long = -42 scala> NegZLong(x) <console>:15: error: NegZLong.apply can only be invoked on an long literal, like NegZLong(-42L). Please use NegZLong.from instead. NegZLong(x) ^
The
NegZLong.from
factory method will inspect the value at runtime and return anOption[NegZLong]
. If the value is valid,NegZLong.from
will return aSome[NegZLong]
, else it will return aNone
. Here's an example:scala> NegZLong.from(x) res3: Option[org.scalactic.anyvals.NegZLong] = Some(NegZLong(-42)) scala> val y = 1L y: Long = 1 scala> NegZLong.from(y) res4: Option[org.scalactic.anyvals.NegZLong] = None
The
NegZLong.apply
factory method is marked implicit, so that you can pass literalLong
s into methods that requireNegZLong
, and get the same compile-time checking you get when callingNegZLong.apply
explicitly. Here's an example:scala> def invert(pos: NegZLong): Long = Long.MaxValue - pos invert: (pos: org.scalactic.anyvals.NegZLong)Long scala> invert(1L) res5: Long = 9223372036854775806 scala> invert(Long.MaxValue) res6: Long = 0 scala> invert(1L) <console>:15: error: NegZLong.apply can only be invoked on a non-positive (i <= 0L) integer literal, like NegZLong(-42L). invert(1L) ^
This example also demonstrates that the
NegZLong
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromLong
to Double in Scala can lose precision.) This makes it convenient to use aNegZLong
where aLong
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Long.MaxValue - pos
. AlthoughLong.MaxValue
is aLong
, which has no-
method that takes aNegZLong
(the type ofpos
), you can still subtractpos
, because theNegZLong
will be implicitly widened toLong
. - final class NonEmptyArray[T] extends AnyVal
A non-empty array: an ordered, mutable, non-empty collection of elements with
IndexedSeq
performance characteristics.A non-empty array: an ordered, mutable, non-empty collection of elements with
IndexedSeq
performance characteristics.The purpose of
NonEmptyArray
is to allow you to express in a type that anArray
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty immutable sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptyArray
sYou can construct a
NonEmptyArray
by passing one or more elements to theNonEmptyArray.apply
factory method:scala> NonEmptyArray(1, 2, 3) res0: org.scalactic.anyvals.NonEmptyArray[Int] = NonEmptyArray(1, 2, 3)
Working with
NonEmptyArray
sNonEmptyArray
does not extend Scala'sSeq
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> Array(1).tail res6: Array[Int] = Array()
On the other hand, many useful methods exist on
Seq
that when invoked on a non-emptySeq
are guaranteed to not result in an emptySeq
. For convenience,NonEmptyArray
defines a method corresponding to every suchSeq
method. Here are some examples:NonEmptyArray(1, 2, 3).map(_ + 1) // Result: NonEmptyArray(2, 3, 4) NonEmptyArray(1).map(_ + 1) // Result: NonEmptyArray(2) NonEmptyArray(1, 2, 3).containsSlice(NonEmptyArray(2, 3)) // Result: true NonEmptyArray(1, 2, 3).containsSlice(NonEmptyArray(3, 4)) // Result: false NonEmptyArray(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NonEmptyArray
does not currently define any methods corresponding toSeq
methods that could result in an emptySeq
. However, an implicit converison fromNonEmptyArray
toArray
is defined in theNonEmptyArray
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptyArray
, even thoughfilter
could result in an empty sequence—but the result type will beArray
instead ofNonEmptyArray
:NonEmptyArray(1, 2, 3).filter(_ < 10) // Result: Array(1, 2, 3) NonEmptyArray(1, 2, 3).filter(_ > 10) // Result: Array()
You can use
NonEmptyArray
s infor
expressions. The result will be anNonEmptyArray
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aArray
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NonEmptyArray(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NonEmptyArray[Int] = NonEmptyArray(2, 3, 4) scala> for (i <- NonEmptyArray(1, 2, 3) if i < 10) yield i + 1 res1: Array[Int] = Array(2, 3, 4) scala> for { | i <- NonEmptyArray(1, 2, 3) | j <- NonEmptyArray('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NonEmptyArray[(Int, Char)] = NonEmptyArray((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NonEmptyArray(1, 2, 3) if i < 10 | j <- NonEmptyArray('a', 'b', 'c') | } yield (i, j) res6: Array[(Int, Char)] = Array((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
- T
the type of elements contained in this
NonEmptyArray
- final class NonEmptyList[+T] extends AnyVal
A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.The purpose of
NonEmptyList
is to allow you to express in a type that aList
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptyList
sYou can construct a
NonEmptyList
by passing one or more elements to theNonEmptyList.apply
factory method:scala> NonEmptyList(1, 2, 3) res0: org.scalactic.anyvals.NonEmptyList[Int] = NonEmptyList(1, 2, 3)
Alternatively you can cons elements onto the
End
singleton object, similar to making aList
starting withNil
:scala> 1 :: 2 :: 3 :: Nil res0: List[Int] = List(1, 2, 3)
scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NonEmptyList[Int] = NonEmptyList(1, 2, 3)Note that although
Nil
is aList[Nothing]
,End
is not aNonEmptyList[Nothing]
, because no emptyNonEmptyList
exists. (A non-empty list is a series of connected links; if you have no links, you have no non-empty list.)scala> val nil: List[Nothing] = Nil nil: List[Nothing] = List()
scala> val nada: NonEmptyList[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NonEmptyList[Nothing] val nada: NonEmptyList[Nothing] = End ^Working with
NonEmptyList
sNonEmptyList
does not extend Scala'sSeq
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> List(1).tail res6: List[Int] = List()
On the other hand, many useful methods exist on
Seq
that when invoked on a non-emptySeq
are guaranteed to not result in an emptySeq
. For convenience,NonEmptyList
defines a method corresponding to every suchSeq
method. Here are some examples:NonEmptyList(1, 2, 3).map(_ + 1) // Result: NonEmptyList(2, 3, 4) NonEmptyList(1).map(_ + 1) // Result: NonEmptyList(2) NonEmptyList(1, 2, 3).containsSlice(NonEmptyList(2, 3)) // Result: true NonEmptyList(1, 2, 3).containsSlice(NonEmptyList(3, 4)) // Result: false NonEmptyList(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NonEmptyList
does not currently define any methods corresponding toSeq
methods that could result in an emptySeq
. However, an implicit converison fromNonEmptyList
toList
is defined in theNonEmptyList
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptyList
, even thoughfilter
could result in an empty sequence—but the result type will beList
instead ofNonEmptyList
:NonEmptyList(1, 2, 3).filter(_ < 10) // Result: List(1, 2, 3) NonEmptyList(1, 2, 3).filter(_ > 10) // Result: List()
You can use
NonEmptyList
s infor
expressions. The result will be anNonEmptyList
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aList
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NonEmptyList(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NonEmptyList[Int] = NonEmptyList(2, 3, 4) scala> for (i <- NonEmptyList(1, 2, 3) if i < 10) yield i + 1 res1: List[Int] = List(2, 3, 4) scala> for { | i <- NonEmptyList(1, 2, 3) | j <- NonEmptyList('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NonEmptyList[(Int, Char)] = NonEmptyList((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NonEmptyList(1, 2, 3) if i < 10 | j <- NonEmptyList('a', 'b', 'c') | } yield (i, j) res6: List[(Int, Char)] = List((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
- T
the type of elements contained in this
NonEmptyList
- final class NonEmptyMap[K, +V] extends AnyVal
A non-empty map: an ordered, immutable, non-empty collection of key-value tuples with
LinearSeq
performance characteristics.A non-empty map: an ordered, immutable, non-empty collection of key-value tuples with
LinearSeq
performance characteristics.The purpose of
NonEmptyMap
is to allow you to express in a type that aMap
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptyMap
sYou can construct a
NonEmptyMap
by passing one or more elements to theNonEmptyMap.apply
factory method:scala> NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three") res0: org.scalactic.anyvals.NonEmptyMap[Int, String] = NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three")
Working with
NonEmptyMap
sNonEmptyMap
does not extend Scala'sMap
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> Map(1 -> "one").tail res6: Map[Int] = Map()
On the other hand, many useful methods exist on
Map
that when invoked on a non-emptySeq
are guaranteed to not result in an emptyMap
. For convenience,NonEmptyMap
defines a method corresponding to every suchMap
method. Here are an example:NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three").map(t => (t._1 + 1, t._2)) // Result: NonEmptyMap(2 -> "one", 3 -> "two", 4 -> "three")
NonEmptyMap
does not currently define any methods corresponding toMap
methods that could result in an emptyMap
. However, an implicit converison fromNonEmptyMap
toMap
is defined in theNonEmptyMap
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptyMap
, even thoughfilter
could result in an empty map—but the result type will beMap
instead ofNonEmptyMap
:NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three").filter(_._1 < 10) // Result: Map(1 -> "one", 2 -> "two", 3 -> "three") NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three").filter(_._ 1> 10) // Result: Map()
You can use
NonEmptyMap
s infor
expressions. The result will be anNonEmptyMap
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aMap
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for ((i, j) <- NonEmptyMap(1 -> "one", 2 -> "two", 3 -> "three")) yield (i + 1, j) res0: org.scalactic.anyvals.NonEmptyMap[Int, String] = NonEmptyMap(2 -> "one", 3 -> "two", 4 -> "three") scala> for ((i, j) <- NonEmptyMap(1, 2, 3) if i < 10) yield (i + 1, j) res1: Map[Int, String] = Map(2 -> "one", 3 -> "two", 4 -> "three")
- K
the type of key contained in this
NonEmptyMap
- V
the type of value contained in this
NonEmptyMap
- final class NonEmptySet[T] extends AnyVal
A non-empty Set: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.A non-empty Set: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.The purpose of
NonEmptySet
is to allow you to express in a type that aSet
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptySet
sYou can construct a
NonEmptySet
by passing one or more elements to theNonEmptySet.apply
factory method:scala> NonEmptySet(1, 2, 3) res0: org.scalactic.anyvals.NonEmptySet[Int] = NonEmptySet(1, 2, 3)
Alternatively you can cons elements onto the
End
singleton object, similar to making aSet
starting withNil
:scala> 1 :: 2 :: 3 :: Nil res0: Set[Int] = Set(1, 2, 3)
scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NonEmptySet[Int] = NonEmptySet(1, 2, 3)Note that although
Nil
is aSet[Nothing]
,End
is not aNonEmptySet[Nothing]
, because no emptyNonEmptySet
exists. (A non-empty Set is a series of connected links; if you have no links, you have no non-empty Set.)scala> val nil: Set[Nothing] = Nil nil: Set[Nothing] = Set()
scala> val nada: NonEmptySet[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NonEmptySet[Nothing] val nada: NonEmptySet[Nothing] = End ^Working with
NonEmptySet
sNonEmptySet
does not extend Scala'sSeq
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> Set(1).tail res6: Set[Int] = Set()
On the other hand, many useful methods exist on
Seq
that when invoked on a non-emptySeq
are guaranteed to not result in an emptySeq
. For convenience,NonEmptySet
defines a method corresponding to every suchSeq
method. Here are some examples:NonEmptySet(1, 2, 3).map(_ + 1) // Result: NonEmptySet(2, 3, 4) NonEmptySet(1).map(_ + 1) // Result: NonEmptySet(2) NonEmptySet(1, 2, 3).containsSlice(NonEmptySet(2, 3)) // Result: true NonEmptySet(1, 2, 3).containsSlice(NonEmptySet(3, 4)) // Result: false NonEmptySet(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NonEmptySet
does not currently define any methods corresponding toSeq
methods that could result in an emptySeq
. However, an implicit converison fromNonEmptySet
toSet
is defined in theNonEmptySet
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptySet
, even thoughfilter
could result in an empty sequence—but the result type will beSet
instead ofNonEmptySet
:NonEmptySet(1, 2, 3).filter(_ < 10) // Result: Set(1, 2, 3) NonEmptySet(1, 2, 3).filter(_ > 10) // Result: Set()
You can use
NonEmptySet
s infor
expressions. The result will be anNonEmptySet
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aSet
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NonEmptySet(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NonEmptySet[Int] = NonEmptySet(2, 3, 4) scala> for (i <- NonEmptySet(1, 2, 3) if i < 10) yield i + 1 res1: Set[Int] = Set(2, 3, 4) scala> for { | i <- NonEmptySet(1, 2, 3) | j <- NonEmptySet('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NonEmptySet[(Int, Char)] = NonEmptySet((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NonEmptySet(1, 2, 3) if i < 10 | j <- NonEmptySet('a', 'b', 'c') | } yield (i, j) res6: Set[(Int, Char)] = Set((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
- T
the type of elements contained in this
NonEmptySet
- final class NonEmptyString extends AnyVal
A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.The purpose of
NonEmptyString
is to allow you to express in a type that aString
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptyString
sYou can construct a
NonEmptyString
by passing one or more elements to theNonEmptyString.apply
factory method:scala> NonEmptyString(1, 2, 3) res0: org.scalactic.anyvals.NonEmptyString[Int] = NonEmptyString(1, 2, 3)
Alternatively you can cons elements onto the
End
singleton object, similar to making aString
starting withNil
:scala> 1 :: 2 :: 3 :: Nil res0: String[Int] = String(1, 2, 3)
scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NonEmptyString[Int] = NonEmptyString(1, 2, 3)Note that although
Nil
is aString[Nothing]
,End
is not aNonEmptyString[Nothing]
, because no emptyNonEmptyString
exists. (A non-empty list is a series of connected links; if you have no links, you have no non-empty list.)scala> val nil: String[Nothing] = Nil nil: String[Nothing] = String()
scala> val nada: NonEmptyString[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NonEmptyString[Nothing] val nada: NonEmptyString[Nothing] = End ^Working with
NonEmptyString
sNonEmptyString
does not extend Scala'sSeq
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> String(1).tail res6: String[Int] = String()
On the other hand, many useful methods exist on
Seq
that when invoked on a non-emptySeq
are guaranteed to not result in an emptySeq
. For convenience,NonEmptyString
defines a method corresponding to every suchSeq
method. Here are some examples:NonEmptyString(1, 2, 3).map(_ + 1) // Result: NonEmptyString(2, 3, 4) NonEmptyString(1).map(_ + 1) // Result: NonEmptyString(2) NonEmptyString(1, 2, 3).containsSlice(NonEmptyString(2, 3)) // Result: true NonEmptyString(1, 2, 3).containsSlice(NonEmptyString(3, 4)) // Result: false NonEmptyString(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NonEmptyString
does not currently define any methods corresponding toSeq
methods that could result in an emptySeq
. However, an implicit converison fromNonEmptyString
toString
is defined in theNonEmptyString
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptyString
, even thoughfilter
could result in an empty sequence—but the result type will beString
instead ofNonEmptyString
:NonEmptyString(1, 2, 3).filter(_ < 10) // Result: String(1, 2, 3) NonEmptyString(1, 2, 3).filter(_ > 10) // Result: String()
You can use
NonEmptyString
s infor
expressions. The result will be anNonEmptyString
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aString
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NonEmptyString(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NonEmptyString[Int] = NonEmptyString(2, 3, 4) scala> for (i <- NonEmptyString(1, 2, 3) if i < 10) yield i + 1 res1: String[Int] = String(2, 3, 4) scala> for { | i <- NonEmptyString(1, 2, 3) | j <- NonEmptyString('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NonEmptyString[(Int, Char)] = NonEmptyString((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NonEmptyString(1, 2, 3) if i < 10 | j <- NonEmptyString('a', 'b', 'c') | } yield (i, j) res6: String[(Int, Char)] = String((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
- final class NonEmptyVector[+T] extends AnyVal
A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.A non-empty list: an ordered, immutable, non-empty collection of elements with
LinearSeq
performance characteristics.The purpose of
NonEmptyVector
is to allow you to express in a type that aVector
is non-empty, thereby eliminating the need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence withIndexedSeq
performance, seeEvery
.Constructing
NonEmptyVector
sYou can construct a
NonEmptyVector
by passing one or more elements to theNonEmptyVector.apply
factory method:scala> NonEmptyVector(1, 2, 3) res0: org.scalactic.anyvals.NonEmptyVector[Int] = NonEmptyVector(1, 2, 3)
Alternatively you can cons elements onto the
End
singleton object, similar to making aVector
starting withNil
:scala> 1 :: 2 :: 3 :: Nil res0: Vector[Int] = Vector(1, 2, 3)
scala> 1 :: 2 :: 3 :: End res1: org.scalactic.NonEmptyVector[Int] = NonEmptyVector(1, 2, 3)Note that although
Nil
is aVector[Nothing]
,End
is not aNonEmptyVector[Nothing]
, because no emptyNonEmptyVector
exists. (A non-empty list is a series of connected links; if you have no links, you have no non-empty list.)scala> val nil: Vector[Nothing] = Nil nil: Vector[Nothing] = Vector()
scala> val nada: NonEmptyVector[Nothing] = End <console>:16: error: type mismatch; found : org.scalactic.anyvals.End.type required: org.scalactic.anyvals.NonEmptyVector[Nothing] val nada: NonEmptyVector[Nothing] = End ^Working with
NonEmptyVector
sNonEmptyVector
does not extend Scala'sSeq
orTraversable
traits because these require that implementations may be empty. For example, if you invoketail
on aSeq
that contains just one element, you'll get an emptySeq
:scala> Vector(1).tail res6: Vector[Int] = Vector()
On the other hand, many useful methods exist on
Seq
that when invoked on a non-emptySeq
are guaranteed to not result in an emptySeq
. For convenience,NonEmptyVector
defines a method corresponding to every suchSeq
method. Here are some examples:NonEmptyVector(1, 2, 3).map(_ + 1) // Result: NonEmptyVector(2, 3, 4) NonEmptyVector(1).map(_ + 1) // Result: NonEmptyVector(2) NonEmptyVector(1, 2, 3).containsSlice(NonEmptyVector(2, 3)) // Result: true NonEmptyVector(1, 2, 3).containsSlice(NonEmptyVector(3, 4)) // Result: false NonEmptyVector(-1, -2, 3, 4, 5).minBy(_.abs) // Result: -1
NonEmptyVector
does not currently define any methods corresponding toSeq
methods that could result in an emptySeq
. However, an implicit converison fromNonEmptyVector
toVector
is defined in theNonEmptyVector
companion object that will be applied if you attempt to call one of the missing methods. As a result, you can invokefilter
on anNonEmptyVector
, even thoughfilter
could result in an empty sequence—but the result type will beVector
instead ofNonEmptyVector
:NonEmptyVector(1, 2, 3).filter(_ < 10) // Result: Vector(1, 2, 3) NonEmptyVector(1, 2, 3).filter(_ > 10) // Result: Vector()
You can use
NonEmptyVector
s infor
expressions. The result will be anNonEmptyVector
unless you use a filter (anif
clause). Because filters are desugared to invocations offilter
, the result type will switch to aVector
at that point. Here are some examples:scala> import org.scalactic.anyvals._ import org.scalactic.anyvals._ scala> for (i <- NonEmptyVector(1, 2, 3)) yield i + 1 res0: org.scalactic.anyvals.NonEmptyVector[Int] = NonEmptyVector(2, 3, 4) scala> for (i <- NonEmptyVector(1, 2, 3) if i < 10) yield i + 1 res1: Vector[Int] = Vector(2, 3, 4) scala> for { | i <- NonEmptyVector(1, 2, 3) | j <- NonEmptyVector('a', 'b', 'c') | } yield (i, j) res3: org.scalactic.anyvals.NonEmptyVector[(Int, Char)] = NonEmptyVector((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c)) scala> for { | i <- NonEmptyVector(1, 2, 3) if i < 10 | j <- NonEmptyVector('a', 'b', 'c') | } yield (i, j) res6: Vector[(Int, Char)] = Vector((1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b), (3,c))
- T
the type of elements contained in this
NonEmptyVector
- final class NonZeroDouble extends AnyVal
An
AnyVal
for non-zeroDouble
s.An
AnyVal
for non-zeroDouble
s.Note: a
NonZeroDouble
may not equal 0.0.Because
NonZeroDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NonZeroDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroDouble.apply
with a literalDouble
value will either produce a validNonZeroDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroDouble(1.1) res1: org.scalactic.anyvals.NonZeroDouble = NonZeroDouble(1.1) scala> NonZeroDouble(0.0) <console>:14: error: NonZeroDouble.apply can only be invoked on a non-zero (i != 0.0 && !i.isNaN) floating point literal, like NonZeroDouble(1.1). NonZeroDouble(0.0) ^
NonZeroDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> NonZeroDouble(x) <console>:15: error: NonZeroDouble.apply can only be invoked on a floating point literal, like NonZeroDouble(1.1). Please use NonZeroDouble.from instead. NonZeroDouble(x) ^
The
NonZeroDouble.from
factory method will inspect the value at runtime and return anOption[NonZeroDouble]
. If the value is valid,NonZeroDouble.from
will return aSome[NonZeroDouble]
, else it will return aNone
. Here's an example:scala> NonZeroDouble.from(x) res4: Option[org.scalactic.anyvals.NonZeroDouble] = Some(NonZeroDouble(1.1)) scala> val y = 0.0 y: Double = 0.0 scala> NonZeroDouble.from(y) res5: Option[org.scalactic.anyvals.NonZeroDouble] = None
The
NonZeroDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNonZeroDouble
, and get the same compile-time checking you get when callingNonZeroDouble.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(0.0) <console>:15: error: NonZeroDouble.apply can only be invoked on a non-zero (i != 0.0 && !i.isNaN) floating point literal, like NonZeroDouble(1.1). invert(0.0) ^
This example also demonstrates that the
NonZeroDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNonZeroDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNonZeroDouble
(the type ofpos
), you can still subtractpos
, because theNonZeroDouble
will be implicitly widened toDouble
. - final class NonZeroFiniteDouble extends AnyVal
An
AnyVal
for finite non-zeroDouble
s.An
AnyVal
for finite non-zeroDouble
s.Note: a
NonZeroFiniteDouble
may not equal 0.0.Because
NonZeroFiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
NonZeroFiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroFiniteDouble.apply
with a literalDouble
value will either produce a validNonZeroFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroFiniteDouble(1.1) res1: org.scalactic.anyvals.NonZeroFiniteDouble = NonZeroFiniteDouble(1.1) scala> NonZeroFiniteDouble(0.0) <console>:14: error: NonZeroFiniteDouble.apply can only be invoked on a finite non-zero (i != 0.0 && !i.isNaN && i != Double.PositiveInfinity && i != Double.NegativeInfinity) floating point literal, like NonZeroFiniteDouble(1.1). NonZeroFiniteDouble(0.0) ^
NonZeroFiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroFiniteDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> NonZeroFiniteDouble(x) <console>:15: error: NonZeroFiniteDouble.apply can only be invoked on a floating point literal, like NonZeroFiniteDouble(1.1). Please use NonZeroFiniteDouble.from instead. NonZeroFiniteDouble(x) ^
The
NonZeroFiniteDouble.from
factory method will inspect the value at runtime and return anOption[NonZeroFiniteDouble]
. If the value is valid,NonZeroFiniteDouble.from
will return aSome[NonZeroFiniteDouble]
, else it will return aNone
. Here's an example:scala> NonZeroFiniteDouble.from(x) res4: Option[org.scalactic.anyvals.NonZeroFiniteDouble] = Some(NonZeroFiniteDouble(1.1)) scala> val y = 0.0 y: Double = 0.0 scala> NonZeroFiniteDouble.from(y) res5: Option[org.scalactic.anyvals.NonZeroFiniteDouble] = None
The
NonZeroFiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requireNonZeroFiniteDouble
, and get the same compile-time checking you get when callingNonZeroFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroFiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroFiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(0.0) <console>:15: error: NonZeroFiniteDouble.apply can only be invoked on a finite non-zero (i != 0.0 && !i.isNaN && i != Double.PositiveInfinity && i != Double.NegativeInfinity) floating point literal, like NonZeroFiniteDouble(1.1). invert(0.0) ^
This example also demonstrates that the
NonZeroFiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aNonZeroFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aNonZeroFiniteDouble
(the type ofpos
), you can still subtractpos
, because theNonZeroFiniteDouble
will be implicitly widened toDouble
. - final class NonZeroFiniteFloat extends AnyVal
An
AnyVal
for finite non-zeroFloat
s.An
AnyVal
for finite non-zeroFloat
s.Note: a
NonZeroFiniteFloat
may not equal 0.0.Because
NonZeroFiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NonZeroFiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroFiniteFloat.apply
with a literalFloat
value will either produce a validNonZeroFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroFiniteFloat(1.1F) res0: org.scalactic.anyvals.NonZeroFiniteFloat = NonZeroFiniteFloat(1.1) scala> NonZeroFiniteFloat(0.0F) <console>:14: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F). NonZeroFiniteFloat(1.1F) ^
NonZeroFiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroFiniteFloat.from
, instead:scala> val x = 1.1F x: Float = 1.1 scala> NonZeroFiniteFloat(x) <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a floating point literal, like NonZeroFiniteFloat(1.1F). Please use NonZeroFiniteFloat.from instead. NonZeroFiniteFloat(x) ^
The
NonZeroFiniteFloat.from
factory method will inspect the value at runtime and return anOption[NonZeroFiniteFloat]
. If the value is valid,NonZeroFiniteFloat.from
will return aSome[NonZeroFiniteFloat]
, else it will return aNone
. Here's an example:scala> NonZeroFiniteFloat.from(x) res3: Option[org.scalactic.anyvals.NonZeroFiniteFloat] = Some(NonZeroFiniteFloat(1.1)) scala> val y = 0.0F y: Float = 0.0 scala> NonZeroFiniteFloat.from(y) res4: Option[org.scalactic.anyvals.NonZeroFiniteFloat] = None
The
NonZeroFiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNonZeroFiniteFloat
, and get the same compile-time checking you get when callingNonZeroFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroFiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroFiniteFloat)Float scala> invert(1.1F) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0F) <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F). invert(0.0F) ^ scala> invert(0.0F) <console>:15: error: NonZeroFiniteFloat.apply can only be invoked on a finite non-zero (i != 0.0f && !i.isNaN && i != Float.PositiveInfinity && i != Float.NegativeInfinity) floating point literal, like NonZeroFiniteFloat(1.1F). invert(0.0F) ^
This example also demonstrates that the
NonZeroFiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNonZeroFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNonZeroFiniteFloat
(the type ofpos
), you can still subtractpos
, because theNonZeroFiniteFloat
will be implicitly widened toFloat
. - final class NonZeroFloat extends AnyVal
An
AnyVal
for non-zeroFloat
s.An
AnyVal
for non-zeroFloat
s.Note: a
NonZeroFloat
may not equal 0.0.Because
NonZeroFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
NonZeroFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroFloat.apply
with a literalFloat
value will either produce a validNonZeroFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroFloat(1.1F) res0: org.scalactic.anyvals.NonZeroFloat = NonZeroFloat(1.1) scala> NonZeroFloat(0.0F) <console>:14: error: NonZeroFloat.apply can only be invoked on a non-zero (i != 0.0f && !i.isNaN) floating point literal, like NonZeroFloat(1.1F). NonZeroFloat(1.1F) ^
NonZeroFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroFloat.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroFloat.from
, instead:scala> val x = 1.1F x: Float = 1.1 scala> NonZeroFloat(x) <console>:15: error: NonZeroFloat.apply can only be invoked on a floating point literal, like NonZeroFloat(1.1F). Please use NonZeroFloat.from instead. NonZeroFloat(x) ^
The
NonZeroFloat.from
factory method will inspect the value at runtime and return anOption[NonZeroFloat]
. If the value is valid,NonZeroFloat.from
will return aSome[NonZeroFloat]
, else it will return aNone
. Here's an example:scala> NonZeroFloat.from(x) res3: Option[org.scalactic.anyvals.NonZeroFloat] = Some(NonZeroFloat(1.1)) scala> val y = 0.0F y: Float = 0.0 scala> NonZeroFloat.from(y) res4: Option[org.scalactic.anyvals.NonZeroFloat] = None
The
NonZeroFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requireNonZeroFloat
, and get the same compile-time checking you get when callingNonZeroFloat.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroFloat)Float scala> invert(1.1F) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0F) <console>:15: error: NonZeroFloat.apply can only be invoked on a non-zero (i != 0.0f && !i.isNaN) floating point literal, like NonZeroFloat(1.1F). invert(0.0F) ^ scala> invert(0.0F) <console>:15: error: NonZeroFloat.apply can only be invoked on a non-zero (i != 0.0f && !i.isNaN) floating point literal, like NonZeroFloat(1.1F). invert(0.0F) ^
This example also demonstrates that the
NonZeroFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aNonZeroFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aNonZeroFloat
(the type ofpos
), you can still subtractpos
, because theNonZeroFloat
will be implicitly widened toFloat
. - final class NonZeroInt extends AnyVal
An
AnyVal
for non-zeroInt
s.An
AnyVal
for non-zeroInt
s.Note: a
NonZeroInt
may not equal 0.Because
NonZeroInt
is anAnyVal
it will usually be as efficient as anInt
, being boxed only when anInt
would have been boxed.The
NonZeroInt.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroInt.apply
with a literalInt
value will either produce a validNonZeroInt
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroInt(42) res0: org.scalactic.anyvals.NonZeroInt = NonZeroInt(42) scala> NonZeroInt(0) <console>:14: error: NonZeroInt.apply can only be invoked on a non-zero (i != 0) literal, like NonZeroInt(42). NonZeroInt(0) ^
NonZeroInt.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroInt.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroInt.from
, instead:scala> val x = 1 x: Int = 1 scala> NonZeroInt(x) <console>:15: error: NonZeroInt.apply can only be invoked on a non-zero integer literal, like NonZeroInt(42). Please use NonZeroInt.from instead. NonZeroInt(x) ^
The
NonZeroInt.from
factory method will inspect the value at runtime and return anOption[NonZeroInt]
. If the value is valid,NonZeroInt.from
will return aSome[NonZeroInt]
, else it will return aNone
. Here's an example:scala> NonZeroInt.from(x) res3: Option[org.scalactic.anyvals.NonZeroInt] = Some(NonZeroInt(1)) scala> val y = 0 y: Int = 0 scala> NonZeroInt.from(y) res4: Option[org.scalactic.anyvals.NonZeroInt] = None
The
NonZeroInt.apply
factory method is marked implicit, so that you can pass literalInt
s into methods that requireNonZeroInt
, and get the same compile-time checking you get when callingNonZeroInt.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroInt): Int = Int.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroInt)Int scala> invert(1) res0: Int = 2147483646 scala> invert(Int.MaxValue) res1: Int = 0 scala> invert(0) <console>:15: error: NonZeroInt.apply can only be invoked on a non-zero (i != 0) integer literal, like NonZeroInt(42). invert(0) ^ scala> invert(-1) <console>:15: error: NonZeroInt.apply can only be invoked on a non-zero (i != 0) integer literal, like NonZeroInt(42). invert(-1) ^
This example also demonstrates that the
NonZeroInt
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromInt
to Float in Scala can lose precision.) This makes it convenient to use aNonZeroInt
where anInt
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Int.MaxValue - pos
. AlthoughInt.MaxValue
is anInt
, which has no-
method that takes aNonZeroInt
(the type ofpos
), you can still subtractpos
, because theNonZeroInt
will be implicitly widened toInt
. - final class NonZeroLong extends AnyVal
An
AnyVal
for non-zeroLong
s.An
AnyVal
for non-zeroLong
s.Note: a
NonZeroLong
may not equal 0.Because
NonZeroLong
is anAnyVal
it will usually be as efficient as anLong
, being boxed only when anLong
would have been boxed.The
NonZeroLong.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNonZeroLong.apply
with a literalLong
value will either produce a validNonZeroLong
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NonZeroLong(42) res0: org.scalactic.anyvals.NonZeroLong = NonZeroLong(42) scala> NonZeroLong(0) <console>:14: error: NonZeroLong.apply can only be invoked on a non-zero (i != 0L) integer literal, like NonZeroLong(42). NonZeroLong(0) ^
NonZeroLong.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNonZeroLong.apply
, you'll get a compiler error that suggests you use a different factor method,NonZeroLong.from
, instead:scala> val x = 42L x: Long = 42 scala> NonZeroLong(x) <console>:15: error: NonZeroLong.apply can only be invoked on an long literal, like NonZeroLong(42). Please use NonZeroLong.from instead. NonZeroLong(x) ^
The
NonZeroLong.from
factory method will inspect the value at runtime and return anOption[NonZeroLong]
. If the value is valid,NonZeroLong.from
will return aSome[NonZeroLong]
, else it will return aNone
. Here's an example:scala> NonZeroLong.from(x) res3: Option[org.scalactic.anyvals.NonZeroLong] = Some(NonZeroLong(42)) scala> val y = 0L y: Long = 0 scala> NonZeroLong.from(y) res4: Option[org.scalactic.anyvals.NonZeroLong] = None
The
NonZeroLong.apply
factory method is marked implicit, so that you can pass literalLong
s into methods that requireNonZeroLong
, and get the same compile-time checking you get when callingNonZeroLong.apply
explicitly. Here's an example:scala> def invert(pos: NonZeroLong): Long = Long.MaxValue - pos invert: (pos: org.scalactic.anyvals.NonZeroLong)Long scala> invert(1L) res5: Long = 9223372036854775806 scala> invert(Long.MaxValue) res6: Long = 0 scala> invert(0L) <console>:15: error: NonZeroLong.apply can only be invoked on a non-zero (i != 0L) integer literal, like NonZeroLong(42L). invert(0L) ^
This example also demonstrates that the
NonZeroLong
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromLong
to Double in Scala can lose precision.) This makes it convenient to use aNonZeroLong
where aLong
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Long.MaxValue - pos
. AlthoughLong.MaxValue
is aLong
, which has no-
method that takes aNonZeroLong
(the type ofpos
), you can still subtractpos
, because theNonZeroLong
will be implicitly widened toLong
. - final class NumericChar extends AnyVal
An
AnyVal
for numericChar
s.An
AnyVal
for numericChar
s.Note: a
NumericChar
has a value between '0' and '9'.Because
NumericChar
is anAnyVal
it will usually be as efficient as aChar
, being boxed only when aChar
would have been boxed.The
NumericChar.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNumericChar.apply
with a literalChar
value will either produce a validNumericChar
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NumericChar('4') res0: org.scalactic.anyvals.NumericChar = NumericChar('4') scala> NumericChar('a') <console>:14: error: NumericChar.apply can only be invoked on Char literals that are numeric, like NumericChar('4'). NumericChar('a') ^
NumericChar.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNumericChar.apply
, you'll get a compiler error that suggests you use a different factory method,NumericChar.from
, instead:scala> val x = '1' x: Char = 1 scala> NumericChar(x) <console>:15: error: NumericChar.apply can only be invoked on Char literals that are numeric, like NumericChar('4'). Please use NumericChar.from instead. NumericChar(x) ^
The
NumericChar.from
factory method will inspect the value at runtime and return anOption[NumericChar]
. If the value is valid,NumericChar.from
will return aSome[NumericChar]
, else it will return aNone
. Here's an example:scala> NumericChar.from(x) res3: Option[org.scalactic.anyvals.NumericChar] = Some(NumericChar('1')) scala> val y = 'a' y: Char = a scala> NumericChar.from(y) res4: Option[org.scalactic.anyvals.NumericChar] = None
The
NumericChar.apply
factory method is marked implicit, so that you can pass literalChar
s into methods that requireNumericChar
, and get the same compile-time checking you get when callingNumericChar.apply
explicitly. Here's an example:scala> def invert(ch: NumericChar): Char = ('9' - ch + '0').toChar invert: (ch: org.scalactic.anyvals.NumericChar)Char scala> invert('1') res6: Char = 8 scala> scala> invert('9') res7: Char = 0 scala> invert('a') <console>:12: error: NumericChar.apply can only be invoked on Char literals that are numeric, like NumericChar('4'). invert('a') ^
- final class NumericString extends AnyVal
An
AnyVal
for numericString
s.An
AnyVal
for numericString
s.Note: a
NumericString
contains only numeric digit characters.Because
NumericString
is anAnyVal
it will usually be as efficient as aString
, being boxed only when aString
would have been boxed.The
NumericString.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingNumericString.apply
with a literalString
value will either produce a validNumericString
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> NumericString("42") res0: org.scalactic.anyvals.NumericString = NumericString(42) scala> NumericString("abc") <console>:11: error: NumericString.apply can only be invoked on String literals that contain numeric characters, i.e., decimal digits '0' through '9', like "123". NumericString("abc") ^
NumericString.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toNumericString.apply
, you'll get a compiler error that suggests you use a different factory method,NumericString.from
, instead:scala> val x = "1" x: String = 1 scala> NumericString(x) <console>:15: error: NumericString.apply can only be invoked on String literals that contain only numeric characters, i.e., decimal digits '0' through '9', like "123" Please use NumericString.from instead. NumericString(x) ^
The
NumericString.from
factory method will inspect the value at runtime and return anOption[NumericString]
. If the value is valid,NumericString.from
will return aSome[NumericString]
, else it will return aNone
. Here's an example:scala> NumericString.from(x) res3: Option[org.scalactic.anyvals.NumericString] = Some(NumericString(1)) scala> val y = "a" y: String = a scala> NumericString.from(y) res4: Option[org.scalactic.anyvals.NumericString] = None
- final class PosDouble extends AnyVal
An
AnyVal
for positiveDouble
s.An
AnyVal
for positiveDouble
s.Because
PosDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
PosDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosDouble.apply
with a literalDouble
value will either produce a validPosDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosDouble(1.1) res1: org.scalactic.anyvals.PosDouble = PosDouble(1.1) scala> PosDouble(-1.1) <console>:14: error: PosDouble.apply can only be invoked on a positive (i > 0.0) floating point literal, like PosDouble(1.1). PosDouble(-1.1) ^
PosDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosDouble.apply
, you'll get a compiler error that suggests you use a different factor method,PosDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> PosDouble(x) <console>:15: error: PosDouble.apply can only be invoked on a floating point literal, like PosDouble(1.1). Please use PosDouble.from instead. PosDouble(x) ^
The
PosDouble.from
factory method will inspect the value at runtime and return anOption[PosDouble]
. If the value is valid,PosDouble.from
will return aSome[PosDouble]
, else it will return aNone
. Here's an example:scala> PosDouble.from(x) res4: Option[org.scalactic.anyvals.PosDouble] = Some(PosDouble(1.1)) scala> val y = -1.1 y: Double = -1.1 scala> PosDouble.from(y) res5: Option[org.scalactic.anyvals.PosDouble] = None
The
PosDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requirePosDouble
, and get the same compile-time checking you get when callingPosDouble.apply
explicitly. Here's an example:scala> def invert(pos: PosDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(-1.1) <console>:15: error: PosDouble.apply can only be invoked on a positive (i > 0.0) floating point literal, like PosDouble(1.1). invert(-1.1) ^
This example also demonstrates that the
PosDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aPosDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aPosDouble
(the type ofpos
), you can still subtractpos
, because thePosDouble
will be implicitly widened toDouble
. - final class PosFiniteDouble extends AnyVal
An
AnyVal
for finite positiveDouble
s.An
AnyVal
for finite positiveDouble
s.Because
PosFiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
PosFiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosFiniteDouble.apply
with a literalDouble
value will either produce a validPosFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosFiniteDouble(1.1) res1: org.scalactic.anyvals.PosFiniteDouble = PosFiniteDouble(1.1) scala> PosFiniteDouble(-1.1) <console>:14: error: PosFiniteDouble.apply can only be invoked on a finite positive (i > 0.0 && i != Double.PositiveInfinity) floating point literal, like PosFiniteDouble(1.1). PosFiniteDouble(-1.1) ^
PosFiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,PosFiniteDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> PosFiniteDouble(x) <console>:15: error: PosFiniteDouble.apply can only be invoked on a floating point literal, like PosFiniteDouble(1.1). Please use PosFiniteDouble.from instead. PosFiniteDouble(x) ^
The
PosFiniteDouble.from
factory method will inspect the value at runtime and return anOption[PosFiniteDouble]
. If the value is valid,PosFiniteDouble.from
will return aSome[PosFiniteDouble]
, else it will return aNone
. Here's an example:scala> PosFiniteDouble.from(x) res4: Option[org.scalactic.anyvals.PosFiniteDouble] = Some(PosFiniteDouble(1.1)) scala> val y = -1.1 y: Double = -1.1 scala> PosFiniteDouble.from(y) res5: Option[org.scalactic.anyvals.PosFiniteDouble] = None
The
PosFiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requirePosFiniteDouble
, and get the same compile-time checking you get when callingPosFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: PosFiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosFiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(-1.1) <console>:15: error: PosFiniteDouble.apply can only be invoked on a finite positive (i > 0.0 && i != Double.PositiveInfinity) floating point literal, like PosFiniteDouble(1.1). invert(-1.1) ^
This example also demonstrates that the
PosFiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aPosFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aPosFiniteDouble
(the type ofpos
), you can still subtractpos
, because thePosFiniteDouble
will be implicitly widened toDouble
. - final class PosFiniteFloat extends AnyVal
An
AnyVal
for finite positiveFloat
s.An
AnyVal
for finite positiveFloat
s.Note: a
PosFiniteFloat
may not equal 0.0. If you want positive number or 0, use PosZFiniteFloat.Because
PosFiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
PosFiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosFiniteFloat.apply
with a literalFloat
value will either produce a validPosFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosFiniteFloat(42.1fF) res0: org.scalactic.anyvals.PosFiniteFloat = PosFiniteFloat(42.1f) scala> PosFiniteFloat(0.0fF) <console>:14: error: PosFiniteFloat.apply can only be invoked on a finite positive (i > 0.0f && i != Float.PositiveInfinity) floating point literal, like PosFiniteFloat(42.1fF). PosFiniteFloat(42.1fF) ^
PosFiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,PosFiniteFloat.from
, instead:scala> val x = 42.1fF x: Float = 42.1f scala> PosFiniteFloat(x) <console>:15: error: PosFiniteFloat.apply can only be invoked on a floating point literal, like PosFiniteFloat(42.1fF). Please use PosFiniteFloat.from instead. PosFiniteFloat(x) ^
The
PosFiniteFloat.from
factory method will inspect the value at runtime and return anOption[PosFiniteFloat]
. If the value is valid,PosFiniteFloat.from
will return aSome[PosFiniteFloat]
, else it will return aNone
. Here's an example:scala> PosFiniteFloat.from(x) res3: Option[org.scalactic.anyvals.PosFiniteFloat] = Some(PosFiniteFloat(42.1f)) scala> val y = 0.0fF y: Float = 0.0f scala> PosFiniteFloat.from(y) res4: Option[org.scalactic.anyvals.PosFiniteFloat] = None
The
PosFiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requirePosFiniteFloat
, and get the same compile-time checking you get when callingPosFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: PosFiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosFiniteFloat)Float scala> invert(42.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0fF) <console>:15: error: PosFiniteFloat.apply can only be invoked on a finite positive (i > 0.0f && i != Float.PositiveInfinity) floating point literal, like PosFiniteFloat(42.1fF). invert(0.0F) ^ scala> invert(0.0fF) <console>:15: error: PosFiniteFloat.apply can only be invoked on a finite positive (i > 0.0f && i != Float.PositiveInfinity) floating point literal, like PosFiniteFloat(42.1fF). invert(0.0fF) ^
This example also demonstrates that the
PosFiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aPosFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aPosFiniteFloat
(the type ofpos
), you can still subtractpos
, because thePosFiniteFloat
will be implicitly widened toFloat
. - final class PosFloat extends AnyVal
An
AnyVal
for positiveFloat
s.An
AnyVal
for positiveFloat
s.Note: a
PosFloat
may not equal 0.0. If you want positive number or 0, use PosZFloat.Because
PosFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
PosFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosFloat.apply
with a literalFloat
value will either produce a validPosFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosFloat(42.1fF) res0: org.scalactic.anyvals.PosFloat = PosFloat(42.1f) scala> PosFloat(0.0fF) <console>:14: error: PosFloat.apply can only be invoked on a positive (i > 0.0f) floating point literal, like PosFloat(42.1fF). PosFloat(42.1fF) ^
PosFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosFloat.apply
, you'll get a compiler error that suggests you use a different factor method,PosFloat.from
, instead:scala> val x = 42.1fF x: Float = 42.1f scala> PosFloat(x) <console>:15: error: PosFloat.apply can only be invoked on a floating point literal, like PosFloat(42.1fF). Please use PosFloat.from instead. PosFloat(x) ^
The
PosFloat.from
factory method will inspect the value at runtime and return anOption[PosFloat]
. If the value is valid,PosFloat.from
will return aSome[PosFloat]
, else it will return aNone
. Here's an example:scala> PosFloat.from(x) res3: Option[org.scalactic.anyvals.PosFloat] = Some(PosFloat(42.1f)) scala> val y = 0.0fF y: Float = 0.0f scala> PosFloat.from(y) res4: Option[org.scalactic.anyvals.PosFloat] = None
The
PosFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requirePosFloat
, and get the same compile-time checking you get when callingPosFloat.apply
explicitly. Here's an example:scala> def invert(pos: PosFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosFloat)Float scala> invert(42.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(0.0fF) <console>:15: error: PosFloat.apply can only be invoked on a positive (i > 0.0f) floating point literal, like PosFloat(42.1fF). invert(0.0F) ^ scala> invert(0.0fF) <console>:15: error: PosFloat.apply can only be invoked on a positive (i > 0.0f) floating point literal, like PosFloat(42.1fF). invert(0.0fF) ^
This example also demonstrates that the
PosFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aPosFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aPosFloat
(the type ofpos
), you can still subtractpos
, because thePosFloat
will be implicitly widened toFloat
. - final class PosInt extends AnyVal
An
AnyVal
for positiveInt
s.An
AnyVal
for positiveInt
s.Note: a
PosInt
may not equal 0. If you want positive number or 0, use PosZInt.Because
PosInt
is anAnyVal
it will usually be as efficient as anInt
, being boxed only when anInt
would have been boxed.The
PosInt.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosInt.apply
with a literalInt
value will either produce a validPosInt
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosInt(42) res0: org.scalactic.anyvals.PosInt = PosInt(42) scala> PosInt(0) <console>:14: error: PosInt.apply can only be invoked on a positive (i > 0) literal, like PosInt(42). PosInt(0) ^
PosInt.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosInt.apply
, you'll get a compiler error that suggests you use a different factor method,PosInt.from
, instead:scala> val x = 1 x: Int = 1 scala> PosInt(x) <console>:15: error: PosInt.apply can only be invoked on a positive integer literal, like PosInt(42). Please use PosInt.from instead. PosInt(x) ^
The
PosInt.from
factory method will inspect the value at runtime and return anOption[PosInt]
. If the value is valid,PosInt.from
will return aSome[PosInt]
, else it will return aNone
. Here's an example:scala> PosInt.from(x) res3: Option[org.scalactic.anyvals.PosInt] = Some(PosInt(1)) scala> val y = 0 y: Int = 0 scala> PosInt.from(y) res4: Option[org.scalactic.anyvals.PosInt] = None
The
PosInt.apply
factory method is marked implicit, so that you can pass literalInt
s into methods that requirePosInt
, and get the same compile-time checking you get when callingPosInt.apply
explicitly. Here's an example:scala> def invert(pos: PosInt): Int = Int.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosInt)Int scala> invert(1) res0: Int = 2147483646 scala> invert(Int.MaxValue) res1: Int = 0 scala> invert(0) <console>:15: error: PosInt.apply can only be invoked on a positive (i > 0) integer literal, like PosInt(42). invert(0) ^ scala> invert(-1) <console>:15: error: PosInt.apply can only be invoked on a positive (i > 0) integer literal, like PosInt(42). invert(-1) ^
This example also demonstrates that the
PosInt
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromInt
to Float in Scala can lose precision.) This makes it convenient to use aPosInt
where anInt
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Int.MaxValue - pos
. AlthoughInt.MaxValue
is anInt
, which has no-
method that takes aPosInt
(the type ofpos
), you can still subtractpos
, because thePosInt
will be implicitly widened toInt
. - final class PosLong extends AnyVal
An
AnyVal
for positiveLong
s.An
AnyVal
for positiveLong
s.Note: a
PosLong
may not equal 0. If you want positive number or 0, use PosZLong.Because
PosLong
is anAnyVal
it will usually be as efficient as anLong
, being boxed only when anLong
would have been boxed.The
PosLong.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosLong.apply
with a literalLong
value will either produce a validPosLong
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosLong(42L) res0: org.scalactic.anyvals.PosLong = PosLong(42L) scala> PosLong(0L) <console>:14: error: PosLong.apply can only be invoked on a positive (i > 0L) integer literal, like PosLong(42L). PosLong(0L) ^
PosLong.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosLong.apply
, you'll get a compiler error that suggests you use a different factor method,PosLong.from
, instead:scala> val x = 42LL x: Long = 42L scala> PosLong(x) <console>:15: error: PosLong.apply can only be invoked on an long literal, like PosLong(42L). Please use PosLong.from instead. PosLong(x) ^
The
PosLong.from
factory method will inspect the value at runtime and return anOption[PosLong]
. If the value is valid,PosLong.from
will return aSome[PosLong]
, else it will return aNone
. Here's an example:scala> PosLong.from(x) res3: Option[org.scalactic.anyvals.PosLong] = Some(PosLong(42L)) scala> val y = 0LL y: Long = 0L scala> PosLong.from(y) res4: Option[org.scalactic.anyvals.PosLong] = None
The
PosLong.apply
factory method is marked implicit, so that you can pass literalLong
s into methods that requirePosLong
, and get the same compile-time checking you get when callingPosLong.apply
explicitly. Here's an example:scala> def invert(pos: PosLong): Long = Long.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosLong)Long scala> invert(1L) res5: Long = 9223372036854775806 scala> invert(Long.MaxValue) res6: Long = 0 scala> invert(0LL) <console>:15: error: PosLong.apply can only be invoked on a positive (i > 0L) integer literal, like PosLong(42LL). invert(0LL) ^
This example also demonstrates that the
PosLong
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromLong
to Double in Scala can lose precision.) This makes it convenient to use aPosLong
where aLong
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Long.MaxValue - pos
. AlthoughLong.MaxValue
is aLong
, which has no-
method that takes aPosLong
(the type ofpos
), you can still subtractpos
, because thePosLong
will be implicitly widened toLong
. - final class PosZDouble extends AnyVal
An
AnyVal
for non-negativeDouble
s.An
AnyVal
for non-negativeDouble
s.Because
PosZDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
PosZDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZDouble.apply
with a literalDouble
value will either produce a validPosZDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZDouble(1.1) res1: org.scalactic.anyvals.PosZDouble = PosZDouble(1.1) scala> PosZDouble(-1.1) <console>:14: error: PosZDouble.apply can only be invoked on a non-negative (i >= 0.0) floating point literal, like PosZDouble(1.1). PosZDouble(-1.1) ^
PosZDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZDouble.apply
, you'll get a compiler error that suggests you use a different factor method,PosZDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> PosZDouble(x) <console>:15: error: PosZDouble.apply can only be invoked on a floating point literal, like PosZDouble(1.1). Please use PosZDouble.from instead. PosZDouble(x) ^
The
PosZDouble.from
factory method will inspect the value at runtime and return anOption[PosZDouble]
. If the value is valid,PosZDouble.from
will return aSome[PosZDouble]
, else it will return aNone
. Here's an example:scala> PosZDouble.from(x) res4: Option[org.scalactic.anyvals.PosZDouble] = Some(PosZDouble(1.1)) scala> val y = -1.1 y: Double = -1.1 scala> PosZDouble.from(y) res5: Option[org.scalactic.anyvals.PosZDouble] = None
The
PosZDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requirePosZDouble
, and get the same compile-time checking you get when callingPosZDouble.apply
explicitly. Here's an example:scala> def invert(pos: PosZDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(-1.1) <console>:15: error: PosZDouble.apply can only be invoked on a non-negative (i >= 0.0) floating point literal, like PosZDouble(1.1). invert(-1.1) ^
This example also demonstrates that the
PosZDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aPosZDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aPosZDouble
(the type ofpos
), you can still subtractpos
, because thePosZDouble
will be implicitly widened toDouble
. - final class PosZFiniteDouble extends AnyVal
An
AnyVal
for finite non-negativeDouble
s.An
AnyVal
for finite non-negativeDouble
s.Because
PosZFiniteDouble
is anAnyVal
it will usually be as efficient as anDouble
, being boxed only when aDouble
would have been boxed.The
PosZFiniteDouble.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZFiniteDouble.apply
with a literalDouble
value will either produce a validPosZFiniteDouble
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZFiniteDouble(1.1) res1: org.scalactic.anyvals.PosZFiniteDouble = PosZFiniteDouble(1.1) scala> PosZFiniteDouble(-1.1) <console>:14: error: PosZFiniteDouble.apply can only be invoked on a finite non-negative (i >= 0.0 && i != Double.PositiveInfinity) floating point literal, like PosZFiniteDouble(1.1). PosZFiniteDouble(-1.1) ^
PosZFiniteDouble.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZFiniteDouble.apply
, you'll get a compiler error that suggests you use a different factor method,PosZFiniteDouble.from
, instead:scala> val x = 1.1 x: Double = 1.1 scala> PosZFiniteDouble(x) <console>:15: error: PosZFiniteDouble.apply can only be invoked on a floating point literal, like PosZFiniteDouble(1.1). Please use PosZFiniteDouble.from instead. PosZFiniteDouble(x) ^
The
PosZFiniteDouble.from
factory method will inspect the value at runtime and return anOption[PosZFiniteDouble]
. If the value is valid,PosZFiniteDouble.from
will return aSome[PosZFiniteDouble]
, else it will return aNone
. Here's an example:scala> PosZFiniteDouble.from(x) res4: Option[org.scalactic.anyvals.PosZFiniteDouble] = Some(PosZFiniteDouble(1.1)) scala> val y = -1.1 y: Double = -1.1 scala> PosZFiniteDouble.from(y) res5: Option[org.scalactic.anyvals.PosZFiniteDouble] = None
The
PosZFiniteDouble.apply
factory method is marked implicit, so that you can pass literalDouble
s into methods that requirePosZFiniteDouble
, and get the same compile-time checking you get when callingPosZFiniteDouble.apply
explicitly. Here's an example:scala> def invert(pos: PosZFiniteDouble): Double = Double.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZFiniteDouble)Double scala> invert(1.1) res6: Double = 1.7976931348623157E308 scala> invert(Double.MaxValue) res8: Double = 0.0 scala> invert(-1.1) <console>:15: error: PosZFiniteDouble.apply can only be invoked on a finite non-negative (i >= 0.0 && i != Double.PositiveInfinity) floating point literal, like PosZFiniteDouble(1.1). invert(-1.1) ^
This example also demonstrates that the
PosZFiniteDouble
companion object also defines implicit widening conversions when a similar conversion is provided in Scala. This makes it convenient to use aPosZFiniteDouble
where aDouble
is needed. An example is the subtraction in the body of theinvert
method defined above,Double.MaxValue - pos
. AlthoughDouble.MaxValue
is aDouble
, which has no-
method that takes aPosZFiniteDouble
(the type ofpos
), you can still subtractpos
, because thePosZFiniteDouble
will be implicitly widened toDouble
. - final class PosZFiniteFloat extends AnyVal
An
AnyVal
for finite non-negativeFloat
s.An
AnyVal
for finite non-negativeFloat
s.Because
PosZFiniteFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
PosZFiniteFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZFiniteFloat.apply
with a literalFloat
value will either produce a validPosZFiniteFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZFiniteFloat(1.1fF) res0: org.scalactic.anyvals.PosZFiniteFloat = PosZFiniteFloat(1.1f) scala> PosZFiniteFloat(-1.1fF) <console>:14: error: PosZFiniteFloat.apply can only be invoked on a finite non-negative (i >= 0.0f && i != Float.PositiveInfinity) floating point literal, like PosZFiniteFloat(1.1fF). PosZFiniteFloat(1.1fF) ^
PosZFiniteFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZFiniteFloat.apply
, you'll get a compiler error that suggests you use a different factor method,PosZFiniteFloat.from
, instead:scala> val x = 1.1fF x: Float = 1.1f scala> PosZFiniteFloat(x) <console>:15: error: PosZFiniteFloat.apply can only be invoked on a floating point literal, like PosZFiniteFloat(1.1fF). Please use PosZFiniteFloat.from instead. PosZFiniteFloat(x) ^
The
PosZFiniteFloat.from
factory method will inspect the value at runtime and return anOption[PosZFiniteFloat]
. If the value is valid,PosZFiniteFloat.from
will return aSome[PosZFiniteFloat]
, else it will return aNone
. Here's an example:scala> PosZFiniteFloat.from(x) res3: Option[org.scalactic.anyvals.PosZFiniteFloat] = Some(PosZFiniteFloat(1.1f)) scala> val y = -1.1fF y: Float = -1.1f scala> PosZFiniteFloat.from(y) res4: Option[org.scalactic.anyvals.PosZFiniteFloat] = None
The
PosZFiniteFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requirePosZFiniteFloat
, and get the same compile-time checking you get when callingPosZFiniteFloat.apply
explicitly. Here's an example:scala> def invert(pos: PosZFiniteFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZFiniteFloat)Float scala> invert(1.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(-1.1fF) <console>:15: error: PosZFiniteFloat.apply can only be invoked on a finite non-negative (i >= 0.0f && i != Float.PositiveInfinity) floating point literal, like PosZFiniteFloat(1.1fF). invert(0.0F) ^ scala> invert(-1.1fF) <console>:15: error: PosZFiniteFloat.apply can only be invoked on a finite non-negative (i >= 0.0f && i != Float.PositiveInfinity) floating point literal, like PosZFiniteFloat(1.1fF). invert(-1.1fF) ^
This example also demonstrates that the
PosZFiniteFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aPosZFiniteFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aPosZFiniteFloat
(the type ofpos
), you can still subtractpos
, because thePosZFiniteFloat
will be implicitly widened toFloat
. - final class PosZFloat extends AnyVal
An
AnyVal
for non-negativeFloat
s.An
AnyVal
for non-negativeFloat
s.Because
PosZFloat
is anAnyVal
it will usually be as efficient as anFloat
, being boxed only when anFloat
would have been boxed.The
PosZFloat.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZFloat.apply
with a literalFloat
value will either produce a validPosZFloat
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZFloat(1.1fF) res0: org.scalactic.anyvals.PosZFloat = PosZFloat(1.1f) scala> PosZFloat(-1.1fF) <console>:14: error: PosZFloat.apply can only be invoked on a non-negative (i >= 0.0f) floating point literal, like PosZFloat(1.1fF). PosZFloat(1.1fF) ^
PosZFloat.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZFloat.apply
, you'll get a compiler error that suggests you use a different factor method,PosZFloat.from
, instead:scala> val x = 1.1fF x: Float = 1.1f scala> PosZFloat(x) <console>:15: error: PosZFloat.apply can only be invoked on a floating point literal, like PosZFloat(1.1fF). Please use PosZFloat.from instead. PosZFloat(x) ^
The
PosZFloat.from
factory method will inspect the value at runtime and return anOption[PosZFloat]
. If the value is valid,PosZFloat.from
will return aSome[PosZFloat]
, else it will return aNone
. Here's an example:scala> PosZFloat.from(x) res3: Option[org.scalactic.anyvals.PosZFloat] = Some(PosZFloat(1.1f)) scala> val y = -1.1fF y: Float = -1.1f scala> PosZFloat.from(y) res4: Option[org.scalactic.anyvals.PosZFloat] = None
The
PosZFloat.apply
factory method is marked implicit, so that you can pass literalFloat
s into methods that requirePosZFloat
, and get the same compile-time checking you get when callingPosZFloat.apply
explicitly. Here's an example:scala> def invert(pos: PosZFloat): Float = Float.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZFloat)Float scala> invert(1.1fF) res5: Float = 3.4028235E38 scala> invert(Float.MaxValue) res6: Float = 0.0 scala> invert(-1.1fF) <console>:15: error: PosZFloat.apply can only be invoked on a non-negative (i >= 0.0f) floating point literal, like PosZFloat(1.1fF). invert(0.0F) ^ scala> invert(-1.1fF) <console>:15: error: PosZFloat.apply can only be invoked on a non-negative (i >= 0.0f) floating point literal, like PosZFloat(1.1fF). invert(-1.1fF) ^
This example also demonstrates that the
PosZFloat
companion object also defines implicit widening conversions when no loss of precision will occur. This makes it convenient to use aPosZFloat
where aFloat
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Float.MaxValue - pos
. AlthoughFloat.MaxValue
is aFloat
, which has no-
method that takes aPosZFloat
(the type ofpos
), you can still subtractpos
, because thePosZFloat
will be implicitly widened toFloat
. - final class PosZInt extends AnyVal
An
AnyVal
for non-negativeInt
s.An
AnyVal
for non-negativeInt
s.Because
PosZInt
is anAnyVal
it will usually be as efficient as anInt
, being boxed only when anInt
would have been boxed.The
PosZInt.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZInt.apply
with a literalInt
value will either produce a validPosZInt
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZInt(42) res0: org.scalactic.anyvals.PosZInt = PosZInt(42) scala> PosZInt(-1) <console>:14: error: PosZInt.apply can only be invoked on a non-negative (i >= 0) literal, like PosZInt(42). PosZInt(-1) ^
PosZInt.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZInt.apply
, you'll get a compiler error that suggests you use a different factor method,PosZInt.from
, instead:scala> val x = 1 x: Int = 1 scala> PosZInt(x) <console>:15: error: PosZInt.apply can only be invoked on a non-negative integer literal, like PosZInt(42). Please use PosZInt.from instead. PosZInt(x) ^
The
PosZInt.from
factory method will inspect the value at runtime and return anOption[PosZInt]
. If the value is valid,PosZInt.from
will return aSome[PosZInt]
, else it will return aNone
. Here's an example:scala> PosZInt.from(x) res3: Option[org.scalactic.anyvals.PosZInt] = Some(PosZInt(1)) scala> val y = 0 y: Int = 0 scala> PosZInt.from(y) res4: Option[org.scalactic.anyvals.PosZInt] = None
The
PosZInt.apply
factory method is marked implicit, so that you can pass literalInt
s into methods that requirePosZInt
, and get the same compile-time checking you get when callingPosZInt.apply
explicitly. Here's an example:scala> def invert(pos: PosZInt): Int = Int.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZInt)Int scala> invert(1) res0: Int = 2147483646 scala> invert(Int.MaxValue) res1: Int = 0 scala> invert(0) <console>:15: error: PosZInt.apply can only be invoked on a non-negative (i >= 0) integer literal, like PosZInt(42). invert(0) ^ scala> invert(-1) <console>:15: error: PosZInt.apply can only be invoked on a non-negative (i >= 0) integer literal, like PosZInt(42). invert(-1) ^
This example also demonstrates that the
PosZInt
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromInt
to Float in Scala can lose precision.) This makes it convenient to use aPosZInt
where anInt
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Int.MaxValue - pos
. AlthoughInt.MaxValue
is anInt
, which has no-
method that takes aPosZInt
(the type ofpos
), you can still subtractpos
, because thePosZInt
will be implicitly widened toInt
. - final class PosZLong extends AnyVal
An
AnyVal
for non-negativeLong
s.An
AnyVal
for non-negativeLong
s.Because
PosZLong
is anAnyVal
it will usually be as efficient as anLong
, being boxed only when anLong
would have been boxed.The
PosZLong.apply
factory method is implemented in terms of a macro that checks literals for validity at compile time. CallingPosZLong.apply
with a literalLong
value will either produce a validPosZLong
instance at run time or an error at compile time. Here's an example:scala> import anyvals._ import anyvals._ scala> PosZLong(42) res0: org.scalactic.anyvals.PosZLong = PosZLong(42) scala> PosZLong(-1) <console>:14: error: PosZLong.apply can only be invoked on a non-negative (i >= 0L) integer literal, like PosZLong(42). PosZLong(-1) ^
PosZLong.apply
cannot be used if the value being passed is a variable (i.e., not a literal), because the macro cannot determine the validity of variables at compile time (just literals). If you try to pass a variable toPosZLong.apply
, you'll get a compiler error that suggests you use a different factor method,PosZLong.from
, instead:scala> val x = 42L x: Long = 42 scala> PosZLong(x) <console>:15: error: PosZLong.apply can only be invoked on an long literal, like PosZLong(42). Please use PosZLong.from instead. PosZLong(x) ^
The
PosZLong.from
factory method will inspect the value at runtime and return anOption[PosZLong]
. If the value is valid,PosZLong.from
will return aSome[PosZLong]
, else it will return aNone
. Here's an example:scala> PosZLong.from(x) res3: Option[org.scalactic.anyvals.PosZLong] = Some(PosZLong(42)) scala> val y = -1L y: Long = -1 scala> PosZLong.from(y) res4: Option[org.scalactic.anyvals.PosZLong] = None
The
PosZLong.apply
factory method is marked implicit, so that you can pass literalLong
s into methods that requirePosZLong
, and get the same compile-time checking you get when callingPosZLong.apply
explicitly. Here's an example:scala> def invert(pos: PosZLong): Long = Long.MaxValue - pos invert: (pos: org.scalactic.anyvals.PosZLong)Long scala> invert(1L) res5: Long = 9223372036854775806 scala> invert(Long.MaxValue) res6: Long = 0 scala> invert(-1L) <console>:15: error: PosZLong.apply can only be invoked on a non-negative (i >= 0L) integer literal, like PosZLong(42L). invert(-1L) ^
This example also demonstrates that the
PosZLong
companion object also defines implicit widening conversions when either no loss of precision will occur or a similar conversion is provided in Scala. (For example, the implicit conversion fromLong
to Double in Scala can lose precision.) This makes it convenient to use aPosZLong
where aLong
or wider type is needed. An example is the subtraction in the body of theinvert
method defined above,Long.MaxValue - pos
. AlthoughLong.MaxValue
is aLong
, which has no-
method that takes aPosZLong
(the type ofpos
), you can still subtractpos
, because thePosZLong
will be implicitly widened toLong
.
Value Members
- object CompileTimeAssertions extends CompileTimeAssertions
Companion object that facilitates the importing of
CompileTimeAssertions
members as an alternative to mixing in the trait. - object End
Object that can be used as an endpoint for
NonEmptyList
construction expressions that use the cons (::
) operator.Object that can be used as an endpoint for
NonEmptyList
construction expressions that use the cons (::
) operator.Here's an example:
scala> 1 :: 2 :: 3 :: End res0: org.scalactic.NonEmptyList[Int] = NonEmptyList(1, 2, 3)
Note that unlike
Nil
, which is an instance ofList[Nothing]
,End
is not an instance ofNonEmptyList[Nothing]
, because there is no emptyNonEmptyList
:scala> Nil.isInstanceOf[List[_]] res0: Boolean = true scala> End.isInstanceOf[NonEmptyList[_]] res1: Boolean = false
- object FiniteDouble
The companion object for
FiniteDouble
that offers factory methods that produceFiniteDouble
s, implicit widening conversions fromFiniteDouble
to other numeric types, and maximum and minimum constant values forFiniteDouble
. - object FiniteFloat
The companion object for
FiniteFloat
that offers factory methods that produceFiniteFloat
s, implicit widening conversions fromFiniteFloat
to other numeric types, and maximum and minimum constant values forFiniteFloat
. - object NegDouble
The companion object for
NegDouble
that offers factory methods that produceNegDouble
s, implicit widening conversions fromNegDouble
to other numeric types, and maximum and minimum constant values forNegDouble
. - object NegFiniteDouble
The companion object for
NegFiniteDouble
that offers factory methods that produceNegFiniteDouble
s, implicit widening conversions fromNegFiniteDouble
to other numeric types, and maximum and minimum constant values forNegFiniteDouble
. - object NegFiniteFloat
The companion object for
NegFiniteFloat
that offers factory methods that produceNegFiniteFloat
s, implicit widening conversions fromNegFiniteFloat
to other numeric types, and maximum and minimum constant values forNegFiniteFloat
. - object NegFloat
The companion object for
NegFloat
that offers factory methods that produceNegFloat
s, implicit widening conversions fromNegFloat
to other numeric types, and maximum and minimum constant values forNegFloat
. - object NegInt
The companion object for
NegInt
that offers factory methods that produceNegInt
s, implicit widening conversions fromNegInt
to other numeric types, and maximum and minimum constant values forNegInt
. - object NegLong
The companion object for
NegLong
that offers factory methods that produceNegLong
s, implicit widening conversions fromNegLong
to other numeric types, and maximum and minimum constant values forNegLong
. - object NegZDouble
The companion object for
NegZDouble
that offers factory methods that produceNegZDouble
s, implicit widening conversions fromNegZDouble
to other numeric types, and maximum and minimum constant values forNegZDouble
. - object NegZFiniteDouble
The companion object for
NegZFiniteDouble
that offers factory methods that produceNegZFiniteDouble
s, implicit widening conversions fromNegZFiniteDouble
to other numeric types, and maximum and minimum constant values forNegZFiniteDouble
. - object NegZFiniteFloat
The companion object for
NegZFiniteFloat
that offers factory methods that produceNegZFiniteFloat
s, implicit widening conversions fromNegZFiniteFloat
to other numeric types, and maximum and minimum constant values forNegZFiniteFloat
. - object NegZFloat
The companion object for
NegZFloat
that offers factory methods that produceNegZFloat
s, implicit widening conversions fromNegZFloat
to other numeric types, and maximum and minimum constant values forNegZFloat
. - object NegZInt
The companion object for
NegZInt
that offers factory methods that produceNegZInt
s, implicit widening conversions fromNegZInt
to other numeric types, and maximum and minimum constant values forNegZInt
. - object NegZLong
The companion object for
NegZLong
that offers factory methods that produceNegZLong
s, implicit widening conversions fromNegZLong
to other numeric types, and maximum and minimum constant values forNegZLong
. - object NonEmptyArray
Companion object for class
NonEmptyArray
. - object NonEmptyList
Companion object for class
NonEmptyList
. - object NonEmptyMap
Companion object for class
NonEmptyMap
. - object NonEmptySet
Companion object for class
NonEmptySet
. - object NonEmptyString
Companion object for class
NonEmptyString
. - object NonEmptyVector
Companion object for class
NonEmptyVector
. - object NonZeroDouble
The companion object for
NonZeroDouble
that offers factory methods that produceNonZeroDouble
s, implicit widening conversions fromNonZeroDouble
to other numeric types, and maximum and minimum constant values forNonZeroDouble
. - object NonZeroFiniteDouble
The companion object for
NonZeroFiniteDouble
that offers factory methods that produceNonZeroFiniteDouble
s, implicit widening conversions fromNonZeroFiniteDouble
to other numeric types, and maximum and minimum constant values forNonZeroFiniteDouble
. - object NonZeroFiniteFloat
The companion object for
NonZeroFiniteFloat
that offers factory methods that produceNonZeroFiniteFloat
s, implicit widening conversions fromNonZeroFiniteFloat
to other numeric types, and maximum and minimum constant values forNonZeroFiniteFloat
. - object NonZeroFloat
The companion object for
NonZeroFloat
that offers factory methods that produceNonZeroFloat
s, implicit widening conversions fromNonZeroFloat
to other numeric types, and maximum and minimum constant values forNonZeroFloat
. - object NonZeroInt
The companion object for
NonZeroInt
that offers factory methods that produceNonZeroInt
s, implicit widening conversions fromNonZeroInt
to other numeric types, and maximum and minimum constant values forNonZeroInt
. - object NonZeroLong
The companion object for
NonZeroLong
that offers factory methods that produceNonZeroLong
s, implicit widening conversions fromNonZeroLong
to other numeric types, and maximum and minimum constant values forNonZeroLong
. - object NumericChar
The companion object for
NumericChar
that offers factory methods that produceNumericChar
s and maximum and minimum constant values forNumericChar
. - object NumericString
The companion object for
NumericString
that offers factory methods that produceNumericString
s. - object PosDouble
The companion object for
PosDouble
that offers factory methods that producePosDouble
s, implicit widening conversions fromPosDouble
to other numeric types, and maximum and minimum constant values forPosDouble
. - object PosFiniteDouble
The companion object for
PosFiniteDouble
that offers factory methods that producePosFiniteDouble
s, implicit widening conversions fromPosFiniteDouble
to other numeric types, and maximum and minimum constant values forPosFiniteDouble
. - object PosFiniteFloat
The companion object for
PosFiniteFloat
that offers factory methods that producePosFiniteFloat
s, implicit widening conversions fromPosFiniteFloat
to other numeric types, and maximum and minimum constant values forPosFiniteFloat
. - object PosFloat
The companion object for
PosFloat
that offers factory methods that producePosFloat
s, implicit widening conversions fromPosFloat
to other numeric types, and maximum and minimum constant values forPosFloat
. - object PosInt
The companion object for
PosInt
that offers factory methods that producePosInt
s, implicit widening conversions fromPosInt
to other numeric types, and maximum and minimum constant values forPosInt
. - object PosLong
The companion object for
PosLong
that offers factory methods that producePosLong
s, implicit widening conversions fromPosLong
to other numeric types, and maximum and minimum constant values forPosLong
. - object PosZDouble
The companion object for
PosZDouble
that offers factory methods that producePosZDouble
s, implicit widening conversions fromPosZDouble
to other numeric types, and maximum and minimum constant values forPosZDouble
. - object PosZFiniteDouble
The companion object for
PosZFiniteDouble
that offers factory methods that producePosZFiniteDouble
s, implicit widening conversions fromPosZFiniteDouble
to other numeric types, and maximum and minimum constant values forPosZFiniteDouble
. - object PosZFiniteFloat
The companion object for
PosZFiniteFloat
that offers factory methods that producePosZFiniteFloat
s, implicit widening conversions fromPosZFiniteFloat
to other numeric types, and maximum and minimum constant values forPosZFiniteFloat
. - object PosZFloat
The companion object for
PosZFloat
that offers factory methods that producePosZFloat
s, implicit widening conversions fromPosZFloat
to other numeric types, and maximum and minimum constant values forPosZFloat
. - object PosZInt
The companion object for
PosZInt
that offers factory methods that producePosZInt
s, implicit widening conversions fromPosZInt
to other numeric types, and maximum and minimum constant values forPosZInt
. - object PosZLong
The companion object for
PosZLong
that offers factory methods that producePosZLong
s, implicit widening conversions fromPosZLong
to other numeric types, and maximum and minimum constant values forPosZLong
.