Trait providing assertion methods that can be called at compile time from macros to validate literals in source code.
An AnyVal
for negative Double
s.
An AnyVal
for negative Double
s.
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 Double
s
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
.
An AnyVal
for finite negative Double
s.
An AnyVal
for finite negative Double
s.
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 Double
s
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
.
An AnyVal
for finite negative Float
s.
An AnyVal
for finite negative Float
s.
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 Float
s
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
.
An AnyVal
for megative Float
s.
An AnyVal
for megative Float
s.
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 Float
s
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
.
An AnyVal
for negative Int
s.
An AnyVal
for negative Int
s.
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 Int
s
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
.
An AnyVal
for negative Long
s.
An AnyVal
for negative Long
s.
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 Long
s
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.MaxValue
is a
Long
, which has no -
method that
takes a NegLong
(the type of pos
),
you can still subtract pos
, because the
NegLong
will be implicitly widened to
Long
.
An AnyVal
for non-positive Double
s.
An AnyVal
for non-positive Double
s.
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 Double
s
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
.
An AnyVal
for finite non-positive Double
s.
An AnyVal
for finite non-positive Double
s.
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 Double
s
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
.
An AnyVal
for finite non-positive Float
s.
An AnyVal
for finite non-positive Float
s.
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 Float
s
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
.
An AnyVal
for non-positive Float
s.
An AnyVal
for non-positive Float
s.
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 Float
s
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
.
An AnyVal
for non-positive Int
s.
An AnyVal
for non-positive Int
s.
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 Int
s
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
.
An AnyVal
for non-positive Long
s.
An AnyVal
for non-positive Long
s.
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 Long
s
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.MaxValue
is a
Long
, which has no -
method that
takes a NegZLong
(the type of pos
),
you can still subtract pos
, because the
NegZLong
will be implicitly widened to
Long
.
An AnyVal
for non-zero Double
s.
An AnyVal
for non-zero Double
s.
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 Double
s
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
.
An AnyVal
for finite non-zero Double
s.
An AnyVal
for finite non-zero Double
s.
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 Double
s
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
.
An AnyVal
for finite non-zero Float
s.
An AnyVal
for finite non-zero Float
s.
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 Float
s
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
.
An AnyVal
for non-zero Float
s.
An AnyVal
for non-zero Float
s.
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 Float
s
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
.
An AnyVal
for non-zero Int
s.
An AnyVal
for non-zero Int
s.
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 Int
s
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
.
An AnyVal
for non-zero Long
s.
An AnyVal
for non-zero Long
s.
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 Long
s
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.MaxValue
is a
Long
, which has no -
method that
takes a NonZeroLong
(the type of pos
),
you can still subtract pos
, because the
NonZeroLong
will be implicitly widened to
Long
.
An AnyVal
for positive Double
s.
An AnyVal
for positive Double
s.
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 Double
s
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
.
An AnyVal
for finite positive Double
s.
An AnyVal
for finite positive Double
s.
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 Double
s
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
.
An AnyVal
for finite positive Float
s.
An AnyVal
for finite positive Float
s.
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 Float
s
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
.
An AnyVal
for positive Float
s.
An AnyVal
for positive Float
s.
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 Float
s
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
.
An AnyVal
for positive Int
s.
An AnyVal
for positive Int
s.
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 Int
s
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
.
An AnyVal
for positive Long
s.
An AnyVal
for positive Long
s.
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 Long
s
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.MaxValue
is a
Long
, which has no -
method that
takes a PosLong
(the type of pos
),
you can still subtract pos
, because the
PosLong
will be implicitly widened to
Long
.
An AnyVal
for non-negative Double
s.
An AnyVal
for non-negative Double
s.
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 Double
s
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
.
An AnyVal
for finite non-negative Double
s.
An AnyVal
for finite non-negative Double
s.
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 Double
s
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
.
An AnyVal
for finite non-negative Float
s.
An AnyVal
for finite non-negative Float
s.
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 Float
s
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
.
An AnyVal
for non-negative Float
s.
An AnyVal
for non-negative Float
s.
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 Float
s
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
.
An AnyVal
for non-negative Int
s.
An AnyVal
for non-negative Int
s.
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 Int
s
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
.
An AnyVal
for non-negative Long
s.
An AnyVal
for non-negative Long
s.
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 Long
s
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.MaxValue
is a
Long
, which has no -
method that
takes a PosZLong
(the type of pos
),
you can still subtract pos
, because the
PosZLong
will be implicitly widened to
Long
.
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.
The companion object for NegDouble
that offers
factory methods that produce NegDouble
s,
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 NegDouble
s,
implicit widening conversions from NegDouble
to
other numeric types, and maximum and minimum constant values
for NegDouble
.
The companion object for NegFiniteDouble
that offers
factory methods that produce NegFiniteDouble
s,
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 NegFiniteDouble
s,
implicit widening conversions from NegFiniteDouble
to
other numeric types, and maximum and minimum constant values
for NegFiniteDouble
.
The companion object for NegFiniteFloat
that offers
factory methods that produce NegFiniteFloat
s,
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 NegFiniteFloat
s,
implicit widening conversions from NegFiniteFloat
to
other numeric types, and maximum and minimum constant values
for NegFiniteFloat
.
The companion object for NegFloat
that offers
factory methods that produce NegFloat
s,
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 NegFloat
s,
implicit widening conversions from NegFloat
to
other numeric types, and maximum and minimum constant values
for NegFloat
.
The companion object for NegInt
that offers factory methods that
produce NegInt
s, 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 NegInt
s, implicit widening conversions from NegInt
to other numeric types, and maximum and minimum constant values for NegInt
.
The companion object for NegLong
that offers
factory methods that produce NegLong
s, 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 NegLong
s, implicit
widening conversions from NegLong
to other
numeric types, and maximum and minimum constant values for
NegLong
.
The companion object for NegZDouble
that offers
factory methods that produce NegZDouble
s,
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 NegZDouble
s,
implicit widening conversions from NegZDouble
to
other numeric types, and maximum and minimum constant values
for NegZDouble
.
The companion object for NegZFiniteDouble
that offers
factory methods that produce NegZFiniteDouble
s,
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 NegZFiniteDouble
s,
implicit widening conversions from NegZFiniteDouble
to
other numeric types, and maximum and minimum constant values
for NegZFiniteDouble
.
The companion object for NegZFiniteFloat
that offers
factory methods that produce NegZFiniteFloat
s,
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 NegZFiniteFloat
s,
implicit widening conversions from NegZFiniteFloat
to
other numeric types, and maximum and minimum constant values
for NegZFiniteFloat
.
The companion object for NegZFloat
that offers
factory methods that produce NegZFloat
s,
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 NegZFloat
s,
implicit widening conversions from NegZFloat
to
other numeric types, and maximum and minimum constant values
for NegZFloat
.
The companion object for NegZInt
that offers factory methods that
produce NegZInt
s, 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 NegZInt
s, implicit widening conversions from NegZInt
to other numeric types, and maximum and minimum constant values for NegZInt
.
The companion object for NegZLong
that offers
factory methods that produce NegZLong
s, 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 NegZLong
s, implicit
widening conversions from NegZLong
to other
numeric types, and maximum and minimum constant values for
NegZLong
.
The companion object for NonZeroDouble
that offers
factory methods that produce NonZeroDouble
s,
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 NonZeroDouble
s,
implicit widening conversions from NonZeroDouble
to
other numeric types, and maximum and minimum constant values
for NonZeroDouble
.
The companion object for NonZeroFiniteDouble
that offers
factory methods that produce NonZeroFiniteDouble
s,
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 NonZeroFiniteDouble
s,
implicit widening conversions from NonZeroFiniteDouble
to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteDouble
.
The companion object for NonZeroFiniteFloat
that offers
factory methods that produce NonZeroFiniteFloat
s,
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 NonZeroFiniteFloat
s,
implicit widening conversions from NonZeroFiniteFloat
to
other numeric types, and maximum and minimum constant values
for NonZeroFiniteFloat
.
The companion object for NonZeroFloat
that offers
factory methods that produce NonZeroFloat
s,
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 NonZeroFloat
s,
implicit widening conversions from NonZeroFloat
to
other numeric types, and maximum and minimum constant values
for NonZeroFloat
.
The companion object for NonZeroInt
that offers factory methods that
produce NonZeroInt
s, 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 NonZeroInt
s, implicit widening conversions from NonZeroInt
to other numeric types, and maximum and minimum constant values for NonZeroInt
.
The companion object for NonZeroLong
that offers
factory methods that produce NonZeroLong
s, 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 NonZeroLong
s, implicit
widening conversions from NonZeroLong
to other
numeric types, and maximum and minimum constant values for
NonZeroLong
.
The companion object for PosDouble
that offers
factory methods that produce PosDouble
s,
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 PosDouble
s,
implicit widening conversions from PosDouble
to
other numeric types, and maximum and minimum constant values
for PosDouble
.
The companion object for PosFiniteDouble
that offers
factory methods that produce PosFiniteDouble
s,
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 PosFiniteDouble
s,
implicit widening conversions from PosFiniteDouble
to
other numeric types, and maximum and minimum constant values
for PosFiniteDouble
.
The companion object for PosFiniteFloat
that offers
factory methods that produce PosFiniteFloat
s,
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 PosFiniteFloat
s,
implicit widening conversions from PosFiniteFloat
to
other numeric types, and maximum and minimum constant values
for PosFiniteFloat
.
The companion object for PosFloat
that offers
factory methods that produce PosFloat
s,
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 PosFloat
s,
implicit widening conversions from PosFloat
to
other numeric types, and maximum and minimum constant values
for PosFloat
.
The companion object for PosInt
that offers factory methods that
produce PosInt
s, 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 PosInt
s, implicit widening conversions from PosInt
to other numeric types, and maximum and minimum constant values for PosInt
.
The companion object for PosLong
that offers
factory methods that produce PosLong
s, 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 PosLong
s, implicit
widening conversions from PosLong
to other
numeric types, and maximum and minimum constant values for
PosLong
.
The companion object for PosZDouble
that offers
factory methods that produce PosZDouble
s,
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 PosZDouble
s,
implicit widening conversions from PosZDouble
to
other numeric types, and maximum and minimum constant values
for PosZDouble
.
The companion object for PosZFiniteDouble
that offers
factory methods that produce PosZFiniteDouble
s,
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 PosZFiniteDouble
s,
implicit widening conversions from PosZFiniteDouble
to
other numeric types, and maximum and minimum constant values
for PosZFiniteDouble
.
The companion object for PosZFiniteFloat
that offers
factory methods that produce PosZFiniteFloat
s,
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 PosZFiniteFloat
s,
implicit widening conversions from PosZFiniteFloat
to
other numeric types, and maximum and minimum constant values
for PosZFiniteFloat
.
The companion object for PosZFloat
that offers
factory methods that produce PosZFloat
s,
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 PosZFloat
s,
implicit widening conversions from PosZFloat
to
other numeric types, and maximum and minimum constant values
for PosZFloat
.
The companion object for PosZInt
that offers factory methods that
produce PosZInt
s, 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 PosZInt
s, implicit widening conversions from PosZInt
to other numeric types, and maximum and minimum constant values for PosZInt
.
The companion object for PosZLong
that offers
factory methods that produce PosZLong
s, 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 PosZLong
s, implicit
widening conversions from PosZLong
to other
numeric types, and maximum and minimum constant values for
PosZLong
.
Trait providing assertion methods that can be called at compile time from macros to validate literals in source code.
The intent of
CompileTimeAssertions
is to make it easier to createAnyVal
s that restrict the values of types for which Scala supports literals:Int
,Long
,Float
,Double
,Char
, andString
. For example, if you are using odd integers in many places in your code, you might have validity checks scattered throughout your code. Here's an example of a method that both requires an oddInt
is passed (as a precondition, and ensures an odd *Int
is returned (as a postcondition):In either the precondition or postcondition check fails, an exception will be thrown at runtime. If you have many methods like this you may want to create a type to represent an odd
Int
, so that the checking for validity errors is isolated in just one place. By using anAnyVal
you can avoid boxing theInt
, which may be more efficient. This might look like:An
AnyVal
cannot have any constructor code, so to ensure that anyInt
passed to theOddInt
constructor is actually odd, the constructor must be private. That way the only way to construct a newOddInt
is via theapply
factory method in theOddInt
companion object, which can require that the value be odd. This design eliminates the need for placingrequire
andensuring
clauses anywhere else that oddInt
s are needed, because the type promises the constraint. ThenextOdd
method could, therefore, be rewritten as:Using the compile-time assertions provided by this trait, you can construct a factory method implemented via a macro that causes a compile failure if
OddInt.apply
is passed anything besides an oddInt
literal. ClassOddInt
would look exactly the same as before:In the companion object, however, the
apply
method would be implemented in terms of a macro. Because theapply
method will only work with literals, you'll need a second method that can work an any expression of typeInt
. We recommend afrom
method that returns anOption[OddInt]
that returnsSome[OddInt}
if the passedInt
is odd, else returnsNone
, and anensuringValid
method that returns anOddInt
if the passedInt
is valid, else throwsAssertionError
.The
apply
method refers to a macro implementation method in classPosIntMacro
. The macro implementation of any such method can look very similar to this one. The only changes you'd need to make is theisValid
method implementation and the text of the error messages.The
isValid
method just takes the underlying type and returnstrue
if it is valid, elsefalse
. This method is placed here so the same valiation code can be used both in thefrom
method at runtime and theapply
macro at compile time. Theapply
actually does just two things. It calls aensureValidIntLiteral
, performing a compile-time assertion that value passed toapply
is anInt
literal that is valid (in this case, odd). If the assertion fails,ensureValidIntLiteral
will complete abruptly with an exception that will contain an appropriate error message (one of the two you passed in) and cause a compiler error with that message. If the assertion succeeds,ensureValidIntLiteral
will just return normally. The next line of code will then execute. This line of code must construct an AST (abstract syntax tree) of code that will replace theOddInt.apply
invocation. We invoke the other factory method that either returns anOddInt
or throws anAssertionError
, since we've proven at compile time that the call will succeed.You may wish to use quasi-quotes instead of reify. The reason we use reify is that this also works on 2.10 without any additional plugin (i.e., you don't need macro paradise), and Scalactic supports 2.10.