org.scalactic.anyvals
Type members
Classlikes
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 create
AnyVals that restrict the values of types for which Scala supports
literals: Int, Long, Float, Double, Char,
and String. 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 odd Int 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 an AnyVal
you can avoid boxing the Int, 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
any Int passed to the OddInt constructor is actually
odd, the constructor must be private. That way the only way to construct a
new OddInt is via the apply factory method in the
OddInt companion object, which can require that the value be
odd. This design eliminates the need for placing require and
ensuring clauses anywhere else that odd Ints are
needed, because the type promises the constraint. The nextOdd
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 odd
Int literal. Class OddInt 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 the apply method
will only work with literals, you'll need a second method that can work
an any expression of type Int. We recommend a from method
that returns an Option[OddInt] that returns Some[OddInt} if the passed Int is odd,
else returns None, and an ensuringValid method that returns an OddInt
if the passed Int is valid, else throws AssertionError.
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 class
PosIntMacro. The macro implementation of any such method can look
very similar to this one. The only changes you'd need to make is the
isValid 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 returns true if it is valid,
else false. This method is placed here so the same valiation code can be used both in
the from method at runtime and the apply macro at compile time. The apply
actually does just two things. It calls a ensureValidIntLiteral, performing a compile-time assertion
that value passed to apply is an Int 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
the OddInt.apply invocation. We invoke the other factory method that either returns an OddInt
or throws an AssertionError, 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.
- Companion:
- object
- Source:
- CompileTimeAssertions.scala
Companion object that facilitates the importing of CompileTimeAssertions members as
an alternative to mixing in the trait.
Companion object that facilitates the importing of CompileTimeAssertions members as
an alternative to mixing in the trait.
- Companion:
- class
- Source:
- CompileTimeAssertions.scala
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 of List[Nothing],
End is not an instance of NonEmptyList[Nothing], because there is
no empty NonEmptyList:
scala> Nil.isInstanceOf[List[_]] res0: Boolean = true scala> End.isInstanceOf[NonEmptyList[_]] res1: Boolean = false
- Source:
- End.scala
An AnyVal for finite Doubles.
An AnyVal for finite Doubles.
Because FiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The FiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
FiniteDouble.apply with a literal
Double value will either produce a valid
FiniteDouble 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 to FiniteDouble.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 an
Option[FiniteDouble]. If the value is valid,
FiniteDouble.from will return a
Some[FiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require FiniteDouble, and get the
same compile-time checking you get when calling
FiniteDouble.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 a
FiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
FiniteDouble (the type of pos), you
can still subtract pos, because the
FiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisFiniteDouble.
- Companion:
- object
- Source:
- FiniteDouble.scala
The companion object for FiniteDouble that offers
factory methods that produce FiniteDoubles,
implicit widening conversions from FiniteDouble to
other numeric types, and maximum and minimum constant values
for FiniteDouble.
The companion object for FiniteDouble that offers
factory methods that produce FiniteDoubles,
implicit widening conversions from FiniteDouble to
other numeric types, and maximum and minimum constant values
for FiniteDouble.
- Companion:
- class
- Source:
- FiniteDouble.scala
An AnyVal for finite Floats.
An AnyVal for finite Floats.
Because FiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The FiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling FiniteFloat.apply with a
literal Float value will either produce a valid
FiniteFloat 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
to FiniteFloat.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 an
Option[FiniteFloat]. If the value is valid,
FiniteFloat.from will return a
Some[FiniteFloat], else it will return a
None. 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 literal Floats
into methods that require FiniteFloat, and get the
same compile-time checking you get when calling
FiniteFloat.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 a
FiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
FiniteFloat (the type of pos), you can
still subtract pos, because the
FiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisFiniteFloat.
- Companion:
- object
- Source:
- FiniteFloat.scala
The companion object for FiniteFloat that offers
factory methods that produce FiniteFloats,
implicit widening conversions from FiniteFloat to
other numeric types, and maximum and minimum constant values
for FiniteFloat.
The companion object for FiniteFloat that offers
factory methods that produce FiniteFloats,
implicit widening conversions from FiniteFloat to
other numeric types, and maximum and minimum constant values
for FiniteFloat.
- Companion:
- class
- Source:
- FiniteFloat.scala
An AnyVal for negative Doubles.
An AnyVal for negative Doubles.
Because NegDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NegDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NegDouble.apply with a literal
Double value will either produce a valid
NegDouble 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 to NegDouble.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 an
Option[NegDouble]. If the value is valid,
NegDouble.from will return a
Some[NegDouble], else it will return a
None. 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 literal Doubles
into methods that require NegDouble, and get the
same compile-time checking you get when calling
NegDouble.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 a
NegDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NegDouble (the type of pos), you
can still subtract pos, because the
NegDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNegDouble.
- Companion:
- object
- Source:
- NegDouble.scala
The companion object for NegDouble that offers
factory methods that produce NegDoubles,
implicit widening conversions from NegDouble to
other numeric types, and maximum and minimum constant values
for NegDouble.
The companion object for NegDouble that offers
factory methods that produce NegDoubles,
implicit widening conversions from NegDouble to
other numeric types, and maximum and minimum constant values
for NegDouble.
- Companion:
- class
- Source:
- NegDouble.scala
An AnyVal for finite negative Doubles.
An AnyVal for finite negative Doubles.
Because NegFiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NegFiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NegFiniteDouble.apply with a literal
Double value will either produce a valid
NegFiniteDouble 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 to NegFiniteDouble.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 an
Option[NegFiniteDouble]. If the value is valid,
NegFiniteDouble.from will return a
Some[NegFiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require NegFiniteDouble, and get the
same compile-time checking you get when calling
NegFiniteDouble.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 a
NegFiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NegFiniteDouble (the type of pos), you
can still subtract pos, because the
NegFiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNegFiniteDouble.
- Companion:
- object
- Source:
- NegFiniteDouble.scala
The companion object for NegFiniteDouble that offers
factory methods that produce NegFiniteDoubles,
implicit widening conversions from NegFiniteDouble to
other numeric types, and maximum and minimum constant values
for NegFiniteDouble.
The companion object for NegFiniteDouble that offers
factory methods that produce NegFiniteDoubles,
implicit widening conversions from NegFiniteDouble to
other numeric types, and maximum and minimum constant values
for NegFiniteDouble.
- Companion:
- class
- Source:
- NegFiniteDouble.scala
An AnyVal for finite negative Floats.
An AnyVal for finite negative Floats.
Note: a NegFiniteFloat may not equal 0.0. If you want negative number or 0, use NegZFiniteFloat.
Because NegFiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NegFiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegFiniteFloat.apply with a
literal Float value will either produce a valid
NegFiniteFloat 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
to NegFiniteFloat.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 an
Option[NegFiniteFloat]. If the value is valid,
NegFiniteFloat.from will return a
Some[NegFiniteFloat], else it will return a
None. 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 literal Floats
into methods that require NegFiniteFloat, and get the
same compile-time checking you get when calling
NegFiniteFloat.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 a
NegFiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NegFiniteFloat (the type of pos), you can
still subtract pos, because the
NegFiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNegFiniteFloat.
- Companion:
- object
- Source:
- NegFiniteFloat.scala
The companion object for NegFiniteFloat that offers
factory methods that produce NegFiniteFloats,
implicit widening conversions from NegFiniteFloat to
other numeric types, and maximum and minimum constant values
for NegFiniteFloat.
The companion object for NegFiniteFloat that offers
factory methods that produce NegFiniteFloats,
implicit widening conversions from NegFiniteFloat to
other numeric types, and maximum and minimum constant values
for NegFiniteFloat.
- Companion:
- class
- Source:
- NegFiniteFloat.scala
An AnyVal for megative Floats.
An AnyVal for megative Floats.
Note: a NegFloat may not equal 0.0. If you want negative number or 0, use NegZFloat.
Because NegFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NegFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegFloat.apply with a
literal Float value will either produce a valid
NegFloat 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
to NegFloat.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 an
Option[NegFloat]. If the value is valid,
NegFloat.from will return a
Some[NegFloat], else it will return a
None. 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 literal Floats
into methods that require NegFloat, and get the
same compile-time checking you get when calling
NegFloat.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 a
NegFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NegFloat (the type of pos), you can
still subtract pos, because the
NegFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNegFloat.
- Companion:
- object
- Source:
- NegFloat.scala
The companion object for NegFloat that offers
factory methods that produce NegFloats,
implicit widening conversions from NegFloat to
other numeric types, and maximum and minimum constant values
for NegFloat.
The companion object for NegFloat that offers
factory methods that produce NegFloats,
implicit widening conversions from NegFloat to
other numeric types, and maximum and minimum constant values
for NegFloat.
- Companion:
- class
- Source:
- NegFloat.scala
An AnyVal for negative Ints.
An AnyVal for negative Ints.
Note: a NegInt may not equal 0. If you want negative number or 0, use NegZInt.
Because NegInt is an AnyVal it will usually be
as efficient as an Int, being boxed only when an Int
would have been boxed.
The NegInt.apply factory method is implemented in terms of a macro that
checks literals for validity at compile time. Calling NegInt.apply with
a literal Int value will either produce a valid NegInt 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
to NegInt.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 an Option[NegInt]. If
the value is valid, NegInt.from will return a Some[NegInt], else it will return a None.
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 literal Ints
into methods that require NegInt, and get the same compile-time checking you get when calling
NegInt.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 from Int to Float in Scala can lose precision.) This makes it convenient to
use a NegInt where an Int or wider type is needed. An example is the subtraction in the body
of the invert method defined above, Int.MaxValue - pos. Although Int.MaxValue is
an Int, which has no - method that takes a NegInt (the type of pos),
you can still subtract pos, because the NegInt will be implicitly widened to Int.
- Value parameters:
- value
The
Intvalue underlying thisNegInt.
- Companion:
- object
- Source:
- NegInt.scala
The companion object for NegInt that offers factory methods that
produce NegInts, implicit widening conversions from NegInt
to other numeric types, and maximum and minimum constant values for NegInt.
The companion object for NegInt that offers factory methods that
produce NegInts, implicit widening conversions from NegInt
to other numeric types, and maximum and minimum constant values for NegInt.
- Companion:
- class
- Source:
- NegInt.scala
An AnyVal for negative Longs.
An AnyVal for negative Longs.
Note: a NegLong may not equal 0. If you want negative number or 0, use NegZLong.
Because NegLong is an AnyVal it
will usually be as efficient as an Long, being
boxed only when an Long would have been boxed.
The NegLong.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegLong.apply with a
literal Long value will either produce a valid
NegLong 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
to NegLong.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 an
Option[NegLong]. If the value is valid,
NegLong.from will return a
Some[NegLong], else it will return a
None. 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 literal Longs
into methods that require NegLong, and get the
same compile-time checking you get when calling
NegLong.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 from Long to Double in
Scala can lose precision.) This makes it convenient to use a
NegLong where a Long or wider type
is needed. An example is the subtraction in the body of the
invert method defined above, Long.MaxValue
- pos. Although
Long.MaxValueis aLong, which has no-method that takes aNegLong(the type ofpos), you can still subtractpos, because theNegLongwill be implicitly widened toLong.
- Value parameters:
- value
The
Longvalue underlying thisNegLong.
- Companion:
- object
- Source:
- NegLong.scala
The companion object for NegLong that offers
factory methods that produce NegLongs, implicit
widening conversions from NegLong to other
numeric types, and maximum and minimum constant values for
NegLong.
The companion object for NegLong that offers
factory methods that produce NegLongs, implicit
widening conversions from NegLong to other
numeric types, and maximum and minimum constant values for
NegLong.
- Companion:
- class
- Source:
- NegLong.scala
An AnyVal for non-positive Doubles.
An AnyVal for non-positive Doubles.
Because NegZDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NegZDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NegZDouble.apply with a literal
Double value will either produce a valid
NegZDouble 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 to NegZDouble.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 an
Option[NegZDouble]. If the value is valid,
NegZDouble.from will return a
Some[NegZDouble], else it will return a
None. 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 literal Doubles
into methods that require NegZDouble, and get the
same compile-time checking you get when calling
NegZDouble.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 a
NegZDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NegZDouble (the type of pos), you
can still subtract pos, because the
NegZDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNegZDouble.
- Companion:
- object
- Source:
- NegZDouble.scala
The companion object for NegZDouble that offers
factory methods that produce NegZDoubles,
implicit widening conversions from NegZDouble to
other numeric types, and maximum and minimum constant values
for NegZDouble.
The companion object for NegZDouble that offers
factory methods that produce NegZDoubles,
implicit widening conversions from NegZDouble to
other numeric types, and maximum and minimum constant values
for NegZDouble.
- Companion:
- class
- Source:
- NegZDouble.scala
An AnyVal for finite non-positive Doubles.
An AnyVal for finite non-positive Doubles.
Because NegZFiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NegZFiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NegZFiniteDouble.apply with a literal
Double value will either produce a valid
NegZFiniteDouble 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 to NegZFiniteDouble.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 an
Option[NegZFiniteDouble]. If the value is valid,
NegZFiniteDouble.from will return a
Some[NegZFiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require NegZFiniteDouble, and get the
same compile-time checking you get when calling
NegZFiniteDouble.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 a
NegZFiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NegZFiniteDouble (the type of pos), you
can still subtract pos, because the
NegZFiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNegZFiniteDouble.
- Companion:
- object
- Source:
- NegZFiniteDouble.scala
The companion object for NegZFiniteDouble that offers
factory methods that produce NegZFiniteDoubles,
implicit widening conversions from NegZFiniteDouble to
other numeric types, and maximum and minimum constant values
for NegZFiniteDouble.
The companion object for NegZFiniteDouble that offers
factory methods that produce NegZFiniteDoubles,
implicit widening conversions from NegZFiniteDouble to
other numeric types, and maximum and minimum constant values
for NegZFiniteDouble.
- Companion:
- class
- Source:
- NegZFiniteDouble.scala
An AnyVal for finite non-positive Floats.
An AnyVal for finite non-positive Floats.
Because NegZFiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NegZFiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegZFiniteFloat.apply with a
literal Float value will either produce a valid
NegZFiniteFloat 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
to NegZFiniteFloat.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 an
Option[NegZFiniteFloat]. If the value is valid,
NegZFiniteFloat.from will return a
Some[NegZFiniteFloat], else it will return a
None. 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 literal Floats
into methods that require NegZFiniteFloat, and get the
same compile-time checking you get when calling
NegZFiniteFloat.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 a
NegZFiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NegZFiniteFloat (the type of pos), you can
still subtract pos, because the
NegZFiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNegZFiniteFloat.
- Companion:
- object
- Source:
- NegZFiniteFloat.scala
The companion object for NegZFiniteFloat that offers
factory methods that produce NegZFiniteFloats,
implicit widening conversions from NegZFiniteFloat to
other numeric types, and maximum and minimum constant values
for NegZFiniteFloat.
The companion object for NegZFiniteFloat that offers
factory methods that produce NegZFiniteFloats,
implicit widening conversions from NegZFiniteFloat to
other numeric types, and maximum and minimum constant values
for NegZFiniteFloat.
- Companion:
- class
- Source:
- NegZFiniteFloat.scala
An AnyVal for non-positive Floats.
An AnyVal for non-positive Floats.
Because NegZFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NegZFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegZFloat.apply with a
literal Float value will either produce a valid
NegZFloat 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
to NegZFloat.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 an
Option[NegZFloat]. If the value is valid,
NegZFloat.from will return a
Some[NegZFloat], else it will return a
None. 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 literal Floats
into methods that require NegZFloat, and get the
same compile-time checking you get when calling
NegZFloat.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 a
NegZFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NegZFloat (the type of pos), you can
still subtract pos, because the
NegZFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNegZFloat.
- Companion:
- object
- Source:
- NegZFloat.scala
The companion object for NegZFloat that offers
factory methods that produce NegZFloats,
implicit widening conversions from NegZFloat to
other numeric types, and maximum and minimum constant values
for NegZFloat.
The companion object for NegZFloat that offers
factory methods that produce NegZFloats,
implicit widening conversions from NegZFloat to
other numeric types, and maximum and minimum constant values
for NegZFloat.
- Companion:
- class
- Source:
- NegZFloat.scala
An AnyVal for non-positive Ints.
An AnyVal for non-positive Ints.
Because NegZInt is an AnyVal it will usually be
as efficient as an Int, being boxed only when an Int
would have been boxed.
The NegZInt.apply factory method is implemented in terms of a macro that
checks literals for validity at compile time. Calling NegZInt.apply with
a literal Int value will either produce a valid NegZInt 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
to NegZInt.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 an Option[NegZInt]. If
the value is valid, NegZInt.from will return a Some[NegZInt], else it will return a None.
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 literal Ints
into methods that require NegZInt, and get the same compile-time checking you get when calling
NegZInt.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 from Int to Float in Scala can lose precision.) This makes it convenient to
use a NegZInt where an Int or wider type is needed. An example is the subtraction in the body
of the invert method defined above, Int.MaxValue - pos. Although Int.MaxValue is
an Int, which has no - method that takes a NegZInt (the type of pos),
you can still subtract pos, because the NegZInt will be implicitly widened to Int.
- Value parameters:
- value
The
Intvalue underlying thisNegZInt.
- Companion:
- object
- Source:
- NegZInt.scala
The companion object for NegZInt that offers factory methods that
produce NegZInts, implicit widening conversions from NegZInt
to other numeric types, and maximum and minimum constant values for NegZInt.
The companion object for NegZInt that offers factory methods that
produce NegZInts, implicit widening conversions from NegZInt
to other numeric types, and maximum and minimum constant values for NegZInt.
- Companion:
- class
- Source:
- NegZInt.scala
An AnyVal for non-positive Longs.
An AnyVal for non-positive Longs.
Because NegZLong is an AnyVal it
will usually be as efficient as an Long, being
boxed only when an Long would have been boxed.
The NegZLong.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NegZLong.apply with a
literal Long value will either produce a valid
NegZLong 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
to NegZLong.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 an
Option[NegZLong]. If the value is valid,
NegZLong.from will return a
Some[NegZLong], else it will return a
None. 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 literal Longs
into methods that require NegZLong, and get the
same compile-time checking you get when calling
NegZLong.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 from Long to Double in
Scala can lose precision.) This makes it convenient to use a
NegZLong where a Long or wider type
is needed. An example is the subtraction in the body of the
invert method defined above, Long.MaxValue
- pos. Although
Long.MaxValueis aLong, which has no-method that takes aNegZLong(the type ofpos), you can still subtractpos, because theNegZLongwill be implicitly widened toLong.
- Value parameters:
- value
The
Longvalue underlying thisNegZLong.
- Companion:
- object
- Source:
- NegZLong.scala
The companion object for NegZLong that offers
factory methods that produce NegZLongs, implicit
widening conversions from NegZLong to other
numeric types, and maximum and minimum constant values for
NegZLong.
The companion object for NegZLong that offers
factory methods that produce NegZLongs, implicit
widening conversions from NegZLong to other
numeric types, and maximum and minimum constant values for
NegZLong.
- Companion:
- class
- Source:
- NegZLong.scala
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 an Array 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 with IndexedSeq
performance, see Every.
== Constructing NonEmptyArrays ==
You can construct a NonEmptyArray by passing one or more elements to the NonEmptyArray.apply factory method:
scala> NonEmptyArray(1, 2, 3) res0: org.scalactic.anyvals.NonEmptyArray[Int] = NonEmptyArray(1, 2, 3)
== Working with NonEmptyArrays ==
NonEmptyArray does not extend Scala's Seq or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
scala> Array(1).tail res6: Array[Int] = Array()
On the other hand, many useful methods exist on Seq that when invoked on a non-empty Seq are guaranteed
to not result in an empty Seq. For convenience, NonEmptyArray defines a method corresponding to every such Seq
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 to Seq methods that could result in
an empty Seq. However, an implicit converison from NonEmptyArray to Array
is defined in the NonEmptyArray companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptyArray, even though filter could result
in an empty sequence—but the result type will be Array instead of NonEmptyArray:
NonEmptyArray(1, 2, 3).filter(_ < 10) // Result: Array(1, 2, 3) NonEmptyArray(1, 2, 3).filter(_ > 10) // Result: Array()
You can use NonEmptyArrays in for expressions. The result will be an NonEmptyArray unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a Array 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))
- Type parameters:
- T
the type of elements contained in this
NonEmptyArray
- Companion:
- object
- Source:
- NonEmptyArray.scala
Companion object for class NonEmptyArray.
Companion object for class NonEmptyArray.
- Companion:
- class
- Source:
- NonEmptyArray.scala
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 a List is non-empty, thereby eliminating the
need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence with IndexedSeq
performance, see Every.
== Constructing NonEmptyLists ==
You can construct a NonEmptyList by passing one or more elements to the NonEmptyList.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 a List starting with Nil:
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 a List[Nothing], End is
not a NonEmptyList[Nothing], because no empty NonEmptyList 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 NonEmptyLists ==
NonEmptyList does not extend Scala's Seq or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
scala> List(1).tail res6: List[Int] = List()
On the other hand, many useful methods exist on Seq that when invoked on a non-empty Seq are guaranteed
to not result in an empty Seq. For convenience, NonEmptyList defines a method corresponding to every such Seq
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 to Seq methods that could result in
an empty Seq. However, an implicit converison from NonEmptyList to List
is defined in the NonEmptyList companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptyList, even though filter could result
in an empty sequence—but the result type will be List instead of NonEmptyList:
NonEmptyList(1, 2, 3).filter(_ < 10) // Result: List(1, 2, 3) NonEmptyList(1, 2, 3).filter(_ > 10) // Result: List()
You can use NonEmptyLists in for expressions. The result will be an NonEmptyList unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a List 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))
- Type parameters:
- T
the type of elements contained in this
NonEmptyList
- Companion:
- object
- Source:
- NonEmptyList.scala
Companion object for class NonEmptyList.
Companion object for class NonEmptyList.
- Companion:
- class
- Source:
- NonEmptyList.scala
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 a Map is non-empty, thereby eliminating the
need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence with IndexedSeq
performance, see Every.
== Constructing NonEmptyMaps ==
You can construct a NonEmptyMap by passing one or more elements to the NonEmptyMap.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 NonEmptyMaps ==
NonEmptyMap does not extend Scala's Map or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
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-empty Seq are guaranteed
to not result in an empty Map. For convenience, NonEmptyMap defines a method corresponding to every such Map
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 to Map methods that could result in
an empty Map. However, an implicit converison from NonEmptyMap to Map
is defined in the NonEmptyMap companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptyMap, even though filter could result
in an empty map—but the result type will be Map instead of NonEmptyMap:
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 NonEmptyMaps in for expressions. The result will be an NonEmptyMap unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a Map 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")
- Type parameters:
- K
the type of key contained in this
NonEmptyMap- V
the type of value contained in this
NonEmptyMap
- Companion:
- object
- Source:
- NonEmptyMap.scala
Companion object for class NonEmptyMap.
Companion object for class NonEmptyMap.
- Companion:
- class
- Source:
- NonEmptyMap.scala
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 a Set is non-empty, thereby eliminating the
need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence with IndexedSeq
performance, see Every.
== Constructing NonEmptySets ==
You can construct a NonEmptySet by passing one or more elements to the NonEmptySet.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 a Set starting with Nil:
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 a Set[Nothing], End is
not a NonEmptySet[Nothing], because no empty NonEmptySet 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 NonEmptySets ==
NonEmptySet does not extend Scala's Seq or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
scala> Set(1).tail res6: Set[Int] = Set()
On the other hand, many useful methods exist on Seq that when invoked on a non-empty Seq are guaranteed
to not result in an empty Seq. For convenience, NonEmptySet defines a method corresponding to every such Seq
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 to Seq methods that could result in
an empty Seq. However, an implicit converison from NonEmptySet to Set
is defined in the NonEmptySet companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptySet, even though filter could result
in an empty sequence—but the result type will be Set instead of NonEmptySet:
NonEmptySet(1, 2, 3).filter(_ < 10) // Result: Set(1, 2, 3) NonEmptySet(1, 2, 3).filter(_ > 10) // Result: Set()
You can use NonEmptySets in for expressions. The result will be an NonEmptySet unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a Set 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))
- Type parameters:
- T
the type of elements contained in this
NonEmptySet
- Companion:
- object
- Source:
- NonEmptySet.scala
Companion object for class NonEmptySet.
Companion object for class NonEmptySet.
- Companion:
- class
- Source:
- NonEmptySet.scala
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 a String is non-empty, thereby eliminating the
need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence with IndexedSeq
performance, see Every.
== Constructing NonEmptyStrings ==
You can construct a NonEmptyString by passing one or more elements to the NonEmptyString.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 a String starting with Nil:
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 a String[Nothing], End is
not a NonEmptyString[Nothing], because no empty NonEmptyString 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 NonEmptyStrings ==
NonEmptyString does not extend Scala's Seq or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
scala> String(1).tail res6: String[Int] = String()
On the other hand, many useful methods exist on Seq that when invoked on a non-empty Seq are guaranteed
to not result in an empty Seq. For convenience, NonEmptyString defines a method corresponding to every such Seq
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 to Seq methods that could result in
an empty Seq. However, an implicit converison from NonEmptyString to String
is defined in the NonEmptyString companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptyString, even though filter could result
in an empty sequence—but the result type will be String instead of NonEmptyString:
NonEmptyString(1, 2, 3).filter(_ < 10) // Result: String(1, 2, 3) NonEmptyString(1, 2, 3).filter(_ > 10) // Result: String()
You can use NonEmptyStrings in for expressions. The result will be an NonEmptyString unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a String 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))
- Companion:
- object
- Source:
- NonEmptyString.scala
Companion object for class NonEmptyString.
Companion object for class NonEmptyString.
- Companion:
- class
- Source:
- NonEmptyString.scala
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 a Vector is non-empty, thereby eliminating the
need for (and potential exception from) a run-time check for non-emptiness. For a non-empty sequence with IndexedSeq
performance, see Every.
== Constructing NonEmptyVectors ==
You can construct a NonEmptyVector by passing one or more elements to the NonEmptyVector.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 a Vector starting with Nil:
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 a Vector[Nothing], End is
not a NonEmptyVector[Nothing], because no empty NonEmptyVector 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 NonEmptyVectors ==
NonEmptyVector does not extend Scala's Seq or Traversable traits because these require that
implementations may be empty. For example, if you invoke tail on a Seq that contains just one element,
you'll get an empty Seq:
scala> Vector(1).tail res6: Vector[Int] = Vector()
On the other hand, many useful methods exist on Seq that when invoked on a non-empty Seq are guaranteed
to not result in an empty Seq. For convenience, NonEmptyVector defines a method corresponding to every such Seq
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 to Seq methods that could result in
an empty Seq. However, an implicit converison from NonEmptyVector to Vector
is defined in the NonEmptyVector companion object that will be applied if you attempt to call one of the missing methods. As a
result, you can invoke filter on an NonEmptyVector, even though filter could result
in an empty sequence—but the result type will be Vector instead of NonEmptyVector:
NonEmptyVector(1, 2, 3).filter(_ < 10) // Result: Vector(1, 2, 3) NonEmptyVector(1, 2, 3).filter(_ > 10) // Result: Vector()
You can use NonEmptyVectors in for expressions. The result will be an NonEmptyVector unless
you use a filter (an if clause). Because filters are desugared to invocations of filter, the
result type will switch to a Vector 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))
- Type parameters:
- T
the type of elements contained in this
NonEmptyVector
- Companion:
- object
- Source:
- NonEmptyVector.scala
Companion object for class NonEmptyVector.
Companion object for class NonEmptyVector.
- Companion:
- class
- Source:
- NonEmptyVector.scala
An AnyVal for non-zero Doubles.
An AnyVal for non-zero Doubles.
Note: a NonZeroDouble may not equal 0.0.
Because NonZeroDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NonZeroDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NonZeroDouble.apply with a literal
Double value will either produce a valid
NonZeroDouble 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 to NonZeroDouble.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 an
Option[NonZeroDouble]. If the value is valid,
NonZeroDouble.from will return a
Some[NonZeroDouble], else it will return a
None. 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 literal Doubles
into methods that require NonZeroDouble, and get the
same compile-time checking you get when calling
NonZeroDouble.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 a
NonZeroDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NonZeroDouble (the type of pos), you
can still subtract pos, because the
NonZeroDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNonZeroDouble.
- Companion:
- object
- Source:
- NonZeroDouble.scala
The companion object for NonZeroDouble that offers
factory methods that produce NonZeroDoubles,
implicit widening conversions from NonZeroDouble to
other numeric types, and maximum and minimum constant values
for NonZeroDouble.
The companion object for NonZeroDouble that offers
factory methods that produce NonZeroDoubles,
implicit widening conversions from NonZeroDouble to
other numeric types, and maximum and minimum constant values
for NonZeroDouble.
- Companion:
- class
- Source:
- NonZeroDouble.scala
An AnyVal for finite non-zero Doubles.
An AnyVal for finite non-zero Doubles.
Note: a NonZeroFiniteDouble may not equal 0.0.
Because NonZeroFiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The NonZeroFiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
NonZeroFiniteDouble.apply with a literal
Double value will either produce a valid
NonZeroFiniteDouble 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 to NonZeroFiniteDouble.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 an
Option[NonZeroFiniteDouble]. If the value is valid,
NonZeroFiniteDouble.from will return a
Some[NonZeroFiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require NonZeroFiniteDouble, and get the
same compile-time checking you get when calling
NonZeroFiniteDouble.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 a
NonZeroFiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
NonZeroFiniteDouble (the type of pos), you
can still subtract pos, because the
NonZeroFiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisNonZeroFiniteDouble.
- Companion:
- object
- Source:
- NonZeroFiniteDouble.scala
The companion object for NonZeroFiniteDouble that offers
factory methods that produce NonZeroFiniteDoubles,
implicit widening conversions from NonZeroFiniteDouble to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteDouble.
The companion object for NonZeroFiniteDouble that offers
factory methods that produce NonZeroFiniteDoubles,
implicit widening conversions from NonZeroFiniteDouble to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteDouble.
- Companion:
- class
- Source:
- NonZeroFiniteDouble.scala
An AnyVal for finite non-zero Floats.
An AnyVal for finite non-zero Floats.
Note: a NonZeroFiniteFloat may not equal 0.0.
Because NonZeroFiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NonZeroFiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NonZeroFiniteFloat.apply with a
literal Float value will either produce a valid
NonZeroFiniteFloat 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
to NonZeroFiniteFloat.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 an
Option[NonZeroFiniteFloat]. If the value is valid,
NonZeroFiniteFloat.from will return a
Some[NonZeroFiniteFloat], else it will return a
None. 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 literal Floats
into methods that require NonZeroFiniteFloat, and get the
same compile-time checking you get when calling
NonZeroFiniteFloat.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 a
NonZeroFiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NonZeroFiniteFloat (the type of pos), you can
still subtract pos, because the
NonZeroFiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNonZeroFiniteFloat.
- Companion:
- object
- Source:
- NonZeroFiniteFloat.scala
The companion object for NonZeroFiniteFloat that offers
factory methods that produce NonZeroFiniteFloats,
implicit widening conversions from NonZeroFiniteFloat to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteFloat.
The companion object for NonZeroFiniteFloat that offers
factory methods that produce NonZeroFiniteFloats,
implicit widening conversions from NonZeroFiniteFloat to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteFloat.
- Companion:
- class
- Source:
- NonZeroFiniteFloat.scala
An AnyVal for non-zero Floats.
An AnyVal for non-zero Floats.
Note: a NonZeroFloat may not equal 0.0.
Because NonZeroFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The NonZeroFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NonZeroFloat.apply with a
literal Float value will either produce a valid
NonZeroFloat 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
to NonZeroFloat.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 an
Option[NonZeroFloat]. If the value is valid,
NonZeroFloat.from will return a
Some[NonZeroFloat], else it will return a
None. 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 literal Floats
into methods that require NonZeroFloat, and get the
same compile-time checking you get when calling
NonZeroFloat.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 a
NonZeroFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
NonZeroFloat (the type of pos), you can
still subtract pos, because the
NonZeroFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisNonZeroFloat.
- Companion:
- object
- Source:
- NonZeroFloat.scala
The companion object for NonZeroFloat that offers
factory methods that produce NonZeroFloats,
implicit widening conversions from NonZeroFloat to
other numeric types, and maximum and minimum constant values
for NonZeroFloat.
The companion object for NonZeroFloat that offers
factory methods that produce NonZeroFloats,
implicit widening conversions from NonZeroFloat to
other numeric types, and maximum and minimum constant values
for NonZeroFloat.
- Companion:
- class
- Source:
- NonZeroFloat.scala
An AnyVal for non-zero Ints.
An AnyVal for non-zero Ints.
Note: a NonZeroInt may not equal 0.
Because NonZeroInt is an AnyVal it will usually be
as efficient as an Int, being boxed only when an Int
would have been boxed.
The NonZeroInt.apply factory method is implemented in terms of a macro that
checks literals for validity at compile time. Calling NonZeroInt.apply with
a literal Int value will either produce a valid NonZeroInt 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
to NonZeroInt.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 an Option[NonZeroInt]. If
the value is valid, NonZeroInt.from will return a Some[NonZeroInt], else it will return a None.
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 literal Ints
into methods that require NonZeroInt, and get the same compile-time checking you get when calling
NonZeroInt.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 from Int to Float in Scala can lose precision.) This makes it convenient to
use a NonZeroInt where an Int or wider type is needed. An example is the subtraction in the body
of the invert method defined above, Int.MaxValue - pos. Although Int.MaxValue is
an Int, which has no - method that takes a NonZeroInt (the type of pos),
you can still subtract pos, because the NonZeroInt will be implicitly widened to Int.
- Value parameters:
- value
The
Intvalue underlying thisNonZeroInt.
- Companion:
- object
- Source:
- NonZeroInt.scala
The companion object for NonZeroInt that offers factory methods that
produce NonZeroInts, implicit widening conversions from NonZeroInt
to other numeric types, and maximum and minimum constant values for NonZeroInt.
The companion object for NonZeroInt that offers factory methods that
produce NonZeroInts, implicit widening conversions from NonZeroInt
to other numeric types, and maximum and minimum constant values for NonZeroInt.
- Companion:
- class
- Source:
- NonZeroInt.scala
An AnyVal for non-zero Longs.
An AnyVal for non-zero Longs.
Note: a NonZeroLong may not equal 0.
Because NonZeroLong is an AnyVal it
will usually be as efficient as an Long, being
boxed only when an Long would have been boxed.
The NonZeroLong.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling NonZeroLong.apply with a
literal Long value will either produce a valid
NonZeroLong 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
to NonZeroLong.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 an
Option[NonZeroLong]. If the value is valid,
NonZeroLong.from will return a
Some[NonZeroLong], else it will return a
None. 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 literal Longs
into methods that require NonZeroLong, and get the
same compile-time checking you get when calling
NonZeroLong.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 from Long to Double in
Scala can lose precision.) This makes it convenient to use a
NonZeroLong where a Long or wider type
is needed. An example is the subtraction in the body of the
invert method defined above, Long.MaxValue
- pos. Although
Long.MaxValueis aLong, which has no-method that takes aNonZeroLong(the type ofpos), you can still subtractpos, because theNonZeroLongwill be implicitly widened toLong.
- Value parameters:
- value
The
Longvalue underlying thisNonZeroLong.
- Companion:
- object
- Source:
- NonZeroLong.scala
The companion object for NonZeroLong that offers
factory methods that produce NonZeroLongs, implicit
widening conversions from NonZeroLong to other
numeric types, and maximum and minimum constant values for
NonZeroLong.
The companion object for NonZeroLong that offers
factory methods that produce NonZeroLongs, implicit
widening conversions from NonZeroLong to other
numeric types, and maximum and minimum constant values for
NonZeroLong.
- Companion:
- class
- Source:
- NonZeroLong.scala
An AnyVal for numeric Chars.
An AnyVal for numeric Chars.
Note: a NumericChar has a value between '0' and '9'.
Because NumericChar is an AnyVal it will usually
be as efficient as a Char, being boxed only when a
Char would have been boxed.
The NumericChar.apply factory method is implemented in terms
of a macro that checks literals for validity at compile time. Calling
NumericChar.apply with a literal Char value will
either produce a valid NumericChar 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 to NumericChar.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 an Option[NumericChar]. If the value is
valid, NumericChar.from will return a
Some[NumericChar], else it will return a None.
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 literal Chars into methods that require
NumericChar, and get the same compile-time checking you get
when calling NumericChar.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')
^
- Value parameters:
- value
The
Charvalue underlying thisNumericChar.
- Companion:
- object
- Source:
- NumericChar.scala
The companion object for NumericChar that offers factory
methods that produce NumericChars and maximum and minimum
constant values for NumericChar.
The companion object for NumericChar that offers factory
methods that produce NumericChars and maximum and minimum
constant values for NumericChar.
- Companion:
- class
- Source:
- NumericChar.scala
An AnyVal for numeric Strings.
An AnyVal for numeric Strings.
Note: a NumericString contains only numeric digit characters.
Because NumericString is an AnyVal
it will usually be as efficient as a String, being
boxed only when a String would have been boxed.
The NumericString.apply factory method is implemented in
terms of a macro that checks literals for validity at compile time. Calling
NumericString.apply with a literal String value
will either produce a valid NumericString 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 to NumericString.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 an Option[NumericString]. If
the value is valid, NumericString.from will return a
Some[NumericString], else it will return a None.
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
- Value parameters:
- value
The
Stringvalue underlying thisNumericString.
- Companion:
- object
- Source:
- NumericString.scala
The companion object for NumericString that offers factory
methods that produce NumericStrings.
The companion object for NumericString that offers factory
methods that produce NumericStrings.
- Companion:
- class
- Source:
- NumericString.scala
An AnyVal for positive Doubles.
An AnyVal for positive Doubles.
Because PosDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The PosDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
PosDouble.apply with a literal
Double value will either produce a valid
PosDouble 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 to PosDouble.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 an
Option[PosDouble]. If the value is valid,
PosDouble.from will return a
Some[PosDouble], else it will return a
None. 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 literal Doubles
into methods that require PosDouble, and get the
same compile-time checking you get when calling
PosDouble.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 a
PosDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
PosDouble (the type of pos), you
can still subtract pos, because the
PosDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisPosDouble.
- Companion:
- object
- Source:
- PosDouble.scala
The companion object for PosDouble that offers
factory methods that produce PosDoubles,
implicit widening conversions from PosDouble to
other numeric types, and maximum and minimum constant values
for PosDouble.
The companion object for PosDouble that offers
factory methods that produce PosDoubles,
implicit widening conversions from PosDouble to
other numeric types, and maximum and minimum constant values
for PosDouble.
- Companion:
- class
- Source:
- PosDouble.scala
An AnyVal for finite positive Doubles.
An AnyVal for finite positive Doubles.
Because PosFiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The PosFiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
PosFiniteDouble.apply with a literal
Double value will either produce a valid
PosFiniteDouble 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 to PosFiniteDouble.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 an
Option[PosFiniteDouble]. If the value is valid,
PosFiniteDouble.from will return a
Some[PosFiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require PosFiniteDouble, and get the
same compile-time checking you get when calling
PosFiniteDouble.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 a
PosFiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
PosFiniteDouble (the type of pos), you
can still subtract pos, because the
PosFiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisPosFiniteDouble.
- Companion:
- object
- Source:
- PosFiniteDouble.scala
The companion object for PosFiniteDouble that offers
factory methods that produce PosFiniteDoubles,
implicit widening conversions from PosFiniteDouble to
other numeric types, and maximum and minimum constant values
for PosFiniteDouble.
The companion object for PosFiniteDouble that offers
factory methods that produce PosFiniteDoubles,
implicit widening conversions from PosFiniteDouble to
other numeric types, and maximum and minimum constant values
for PosFiniteDouble.
- Companion:
- class
- Source:
- PosFiniteDouble.scala
An AnyVal for finite positive Floats.
An AnyVal for finite positive Floats.
Note: a PosFiniteFloat may not equal 0.0. If you want positive number or 0, use PosZFiniteFloat.
Because PosFiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The PosFiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosFiniteFloat.apply with a
literal Float value will either produce a valid
PosFiniteFloat 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
to PosFiniteFloat.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 an
Option[PosFiniteFloat]. If the value is valid,
PosFiniteFloat.from will return a
Some[PosFiniteFloat], else it will return a
None. 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 literal Floats
into methods that require PosFiniteFloat, and get the
same compile-time checking you get when calling
PosFiniteFloat.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 a
PosFiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
PosFiniteFloat (the type of pos), you can
still subtract pos, because the
PosFiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisPosFiniteFloat.
- Companion:
- object
- Source:
- PosFiniteFloat.scala
The companion object for PosFiniteFloat that offers
factory methods that produce PosFiniteFloats,
implicit widening conversions from PosFiniteFloat to
other numeric types, and maximum and minimum constant values
for PosFiniteFloat.
The companion object for PosFiniteFloat that offers
factory methods that produce PosFiniteFloats,
implicit widening conversions from PosFiniteFloat to
other numeric types, and maximum and minimum constant values
for PosFiniteFloat.
- Companion:
- class
- Source:
- PosFiniteFloat.scala
An AnyVal for positive Floats.
An AnyVal for positive Floats.
Note: a PosFloat may not equal 0.0. If you want positive number or 0, use PosZFloat.
Because PosFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The PosFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosFloat.apply with a
literal Float value will either produce a valid
PosFloat 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
to PosFloat.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 an
Option[PosFloat]. If the value is valid,
PosFloat.from will return a
Some[PosFloat], else it will return a
None. 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 literal Floats
into methods that require PosFloat, and get the
same compile-time checking you get when calling
PosFloat.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 a
PosFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
PosFloat (the type of pos), you can
still subtract pos, because the
PosFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisPosFloat.
- Companion:
- object
- Source:
- PosFloat.scala
The companion object for PosFloat that offers
factory methods that produce PosFloats,
implicit widening conversions from PosFloat to
other numeric types, and maximum and minimum constant values
for PosFloat.
The companion object for PosFloat that offers
factory methods that produce PosFloats,
implicit widening conversions from PosFloat to
other numeric types, and maximum and minimum constant values
for PosFloat.
- Companion:
- class
- Source:
- PosFloat.scala
An AnyVal for positive Ints.
An AnyVal for positive Ints.
Note: a PosInt may not equal 0. If you want positive number or 0, use PosZInt.
Because PosInt is an AnyVal it will usually be
as efficient as an Int, being boxed only when an Int
would have been boxed.
The PosInt.apply factory method is implemented in terms of a macro that
checks literals for validity at compile time. Calling PosInt.apply with
a literal Int value will either produce a valid PosInt 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
to PosInt.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 an Option[PosInt]. If
the value is valid, PosInt.from will return a Some[PosInt], else it will return a None.
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 literal Ints
into methods that require PosInt, and get the same compile-time checking you get when calling
PosInt.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 from Int to Float in Scala can lose precision.) This makes it convenient to
use a PosInt where an Int or wider type is needed. An example is the subtraction in the body
of the invert method defined above, Int.MaxValue - pos. Although Int.MaxValue is
an Int, which has no - method that takes a PosInt (the type of pos),
you can still subtract pos, because the PosInt will be implicitly widened to Int.
- Value parameters:
- value
The
Intvalue underlying thisPosInt.
- Companion:
- object
- Source:
- PosInt.scala
The companion object for PosInt that offers factory methods that
produce PosInts, implicit widening conversions from PosInt
to other numeric types, and maximum and minimum constant values for PosInt.
The companion object for PosInt that offers factory methods that
produce PosInts, implicit widening conversions from PosInt
to other numeric types, and maximum and minimum constant values for PosInt.
- Companion:
- class
- Source:
- PosInt.scala
An AnyVal for positive Longs.
An AnyVal for positive Longs.
Note: a PosLong may not equal 0. If you want positive number or 0, use PosZLong.
Because PosLong is an AnyVal it
will usually be as efficient as an Long, being
boxed only when an Long would have been boxed.
The PosLong.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosLong.apply with a
literal Long value will either produce a valid
PosLong 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
to PosLong.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 an
Option[PosLong]. If the value is valid,
PosLong.from will return a
Some[PosLong], else it will return a
None. 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 literal Longs
into methods that require PosLong, and get the
same compile-time checking you get when calling
PosLong.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 from Long to Double in
Scala can lose precision.) This makes it convenient to use a
PosLong where a Long or wider type
is needed. An example is the subtraction in the body of the
invert method defined above, Long.MaxValue
- pos. Although
Long.MaxValueis aLong, which has no-method that takes aPosLong(the type ofpos), you can still subtractpos, because thePosLongwill be implicitly widened toLong.
- Value parameters:
- value
The
Longvalue underlying thisPosLong.
- Companion:
- object
- Source:
- PosLong.scala
The companion object for PosLong that offers
factory methods that produce PosLongs, implicit
widening conversions from PosLong to other
numeric types, and maximum and minimum constant values for
PosLong.
The companion object for PosLong that offers
factory methods that produce PosLongs, implicit
widening conversions from PosLong to other
numeric types, and maximum and minimum constant values for
PosLong.
- Companion:
- class
- Source:
- PosLong.scala
An AnyVal for non-negative Doubles.
An AnyVal for non-negative Doubles.
Because PosZDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The PosZDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
PosZDouble.apply with a literal
Double value will either produce a valid
PosZDouble 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 to PosZDouble.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 an
Option[PosZDouble]. If the value is valid,
PosZDouble.from will return a
Some[PosZDouble], else it will return a
None. 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 literal Doubles
into methods that require PosZDouble, and get the
same compile-time checking you get when calling
PosZDouble.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 a
PosZDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
PosZDouble (the type of pos), you
can still subtract pos, because the
PosZDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisPosZDouble.
- Companion:
- object
- Source:
- PosZDouble.scala
The companion object for PosZDouble that offers
factory methods that produce PosZDoubles,
implicit widening conversions from PosZDouble to
other numeric types, and maximum and minimum constant values
for PosZDouble.
The companion object for PosZDouble that offers
factory methods that produce PosZDoubles,
implicit widening conversions from PosZDouble to
other numeric types, and maximum and minimum constant values
for PosZDouble.
- Companion:
- class
- Source:
- PosZDouble.scala
An AnyVal for finite non-negative Doubles.
An AnyVal for finite non-negative Doubles.
Because PosZFiniteDouble is an AnyVal it
will usually be as efficient as an Double, being
boxed only when a Double would have been boxed.
The PosZFiniteDouble.apply factory method is
implemented in terms of a macro that checks literals for
validity at compile time. Calling
PosZFiniteDouble.apply with a literal
Double value will either produce a valid
PosZFiniteDouble 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 to PosZFiniteDouble.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 an
Option[PosZFiniteDouble]. If the value is valid,
PosZFiniteDouble.from will return a
Some[PosZFiniteDouble], else it will return a
None. 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 literal Doubles
into methods that require PosZFiniteDouble, and get the
same compile-time checking you get when calling
PosZFiniteDouble.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 a
PosZFiniteDouble where a Double is
needed. An example is the subtraction in the body of the
invert method defined above,
Double.MaxValue - pos. Although
Double.MaxValue is a Double, which
has no - method that takes a
PosZFiniteDouble (the type of pos), you
can still subtract pos, because the
PosZFiniteDouble will be implicitly widened to
Double.
- Value parameters:
- value
The
Doublevalue underlying thisPosZFiniteDouble.
- Companion:
- object
- Source:
- PosZFiniteDouble.scala
The companion object for PosZFiniteDouble that offers
factory methods that produce PosZFiniteDoubles,
implicit widening conversions from PosZFiniteDouble to
other numeric types, and maximum and minimum constant values
for PosZFiniteDouble.
The companion object for PosZFiniteDouble that offers
factory methods that produce PosZFiniteDoubles,
implicit widening conversions from PosZFiniteDouble to
other numeric types, and maximum and minimum constant values
for PosZFiniteDouble.
- Companion:
- class
- Source:
- PosZFiniteDouble.scala
An AnyVal for finite non-negative Floats.
An AnyVal for finite non-negative Floats.
Because PosZFiniteFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The PosZFiniteFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosZFiniteFloat.apply with a
literal Float value will either produce a valid
PosZFiniteFloat 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
to PosZFiniteFloat.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 an
Option[PosZFiniteFloat]. If the value is valid,
PosZFiniteFloat.from will return a
Some[PosZFiniteFloat], else it will return a
None. 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 literal Floats
into methods that require PosZFiniteFloat, and get the
same compile-time checking you get when calling
PosZFiniteFloat.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 a
PosZFiniteFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
PosZFiniteFloat (the type of pos), you can
still subtract pos, because the
PosZFiniteFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisPosZFiniteFloat.
- Companion:
- object
- Source:
- PosZFiniteFloat.scala
The companion object for PosZFiniteFloat that offers
factory methods that produce PosZFiniteFloats,
implicit widening conversions from PosZFiniteFloat to
other numeric types, and maximum and minimum constant values
for PosZFiniteFloat.
The companion object for PosZFiniteFloat that offers
factory methods that produce PosZFiniteFloats,
implicit widening conversions from PosZFiniteFloat to
other numeric types, and maximum and minimum constant values
for PosZFiniteFloat.
- Companion:
- class
- Source:
- PosZFiniteFloat.scala
An AnyVal for non-negative Floats.
An AnyVal for non-negative Floats.
Because PosZFloat is an AnyVal it
will usually be as efficient as an Float, being
boxed only when an Float would have been boxed.
The PosZFloat.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosZFloat.apply with a
literal Float value will either produce a valid
PosZFloat 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
to PosZFloat.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 an
Option[PosZFloat]. If the value is valid,
PosZFloat.from will return a
Some[PosZFloat], else it will return a
None. 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 literal Floats
into methods that require PosZFloat, and get the
same compile-time checking you get when calling
PosZFloat.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 a
PosZFloat where a Float or wider
type is needed. An example is the subtraction in the body of
the invert method defined above,
Float.MaxValue - pos. Although
Float.MaxValue is a Float, which
has no - method that takes a
PosZFloat (the type of pos), you can
still subtract pos, because the
PosZFloat will be implicitly widened to
Float.
- Value parameters:
- value
The
Floatvalue underlying thisPosZFloat.
- Companion:
- object
- Source:
- PosZFloat.scala
The companion object for PosZFloat that offers
factory methods that produce PosZFloats,
implicit widening conversions from PosZFloat to
other numeric types, and maximum and minimum constant values
for PosZFloat.
The companion object for PosZFloat that offers
factory methods that produce PosZFloats,
implicit widening conversions from PosZFloat to
other numeric types, and maximum and minimum constant values
for PosZFloat.
- Companion:
- class
- Source:
- PosZFloat.scala
An AnyVal for non-negative Ints.
An AnyVal for non-negative Ints.
Because PosZInt is an AnyVal it will usually be
as efficient as an Int, being boxed only when an Int
would have been boxed.
The PosZInt.apply factory method is implemented in terms of a macro that
checks literals for validity at compile time. Calling PosZInt.apply with
a literal Int value will either produce a valid PosZInt 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
to PosZInt.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 an Option[PosZInt]. If
the value is valid, PosZInt.from will return a Some[PosZInt], else it will return a None.
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 literal Ints
into methods that require PosZInt, and get the same compile-time checking you get when calling
PosZInt.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 from Int to Float in Scala can lose precision.) This makes it convenient to
use a PosZInt where an Int or wider type is needed. An example is the subtraction in the body
of the invert method defined above, Int.MaxValue - pos. Although Int.MaxValue is
an Int, which has no - method that takes a PosZInt (the type of pos),
you can still subtract pos, because the PosZInt will be implicitly widened to Int.
- Value parameters:
- value
The
Intvalue underlying thisPosZInt.
- Companion:
- object
- Source:
- PosZInt.scala
The companion object for PosZInt that offers factory methods that
produce PosZInts, implicit widening conversions from PosZInt
to other numeric types, and maximum and minimum constant values for PosZInt.
The companion object for PosZInt that offers factory methods that
produce PosZInts, implicit widening conversions from PosZInt
to other numeric types, and maximum and minimum constant values for PosZInt.
- Companion:
- class
- Source:
- PosZInt.scala
An AnyVal for non-negative Longs.
An AnyVal for non-negative Longs.
Because PosZLong is an AnyVal it
will usually be as efficient as an Long, being
boxed only when an Long would have been boxed.
The PosZLong.apply factory method is implemented
in terms of a macro that checks literals for validity at
compile time. Calling PosZLong.apply with a
literal Long value will either produce a valid
PosZLong 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
to PosZLong.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 an
Option[PosZLong]. If the value is valid,
PosZLong.from will return a
Some[PosZLong], else it will return a
None. 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 literal Longs
into methods that require PosZLong, and get the
same compile-time checking you get when calling
PosZLong.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 from Long to Double in
Scala can lose precision.) This makes it convenient to use a
PosZLong where a Long or wider type
is needed. An example is the subtraction in the body of the
invert method defined above, Long.MaxValue
- pos. Although
Long.MaxValueis aLong, which has no-method that takes aPosZLong(the type ofpos), you can still subtractpos, because thePosZLongwill be implicitly widened toLong.
- Value parameters:
- value
The
Longvalue underlying thisPosZLong.
- Companion:
- object
- Source:
- PosZLong.scala
The companion object for PosZLong that offers
factory methods that produce PosZLongs, implicit
widening conversions from PosZLong to other
numeric types, and maximum and minimum constant values for
PosZLong.
The companion object for PosZLong that offers
factory methods that produce PosZLongs, implicit
widening conversions from PosZLong to other
numeric types, and maximum and minimum constant values for
PosZLong.
- Companion:
- class
- Source:
- PosZLong.scala