
Scala - Data Types
Scala has all the same data types as Java, with the same memory footprint and precision. Following is the table giving details about all the data types available in Scala −
Sr.No | Data Type & Description |
---|---|
1 | Byte 8 bit signed value. Range from to |
2 | Short 16 bit signed value. Range to |
3 | Int 32 bit signed value. Range to |
4 | Long 64 bit signed value. to |
5 | Float 32 bit IEEE single-precision float |
6 | Double 64 bit IEEE double-precision float |
7 | Char 16 bit unsigned Unicode character. Range from U+ to U+FFFF |
8 | String A sequence of Chars |
9 | Boolean Either the literal true or the literal false |
10 | Unit Corresponds to no value |
11 | Null null or empty reference |
12 | Nothing The subtype of every other type; includes no values |
13 | Any The supertype of any type; any object is of type Any |
14 | AnyRef The supertype of any reference type |
All the data types listed above are objects. There are no primitive types like in Java. This means that you can call methods on an Int, Long, etc.
Scala Basic Literals
The rules Scala uses for literals are simple and intuitive. This section explains all basic Scala Literals.
Integral Literals
Integer literals are usually of type Int, or of type Long when followed by a L or l suffix. Here are some integer literals −
0 21 0xFFFFFFFF LFloating Point Literal
Floating point literals are of type Float when followed by a floating point type suffix F or f, and are of type Double otherwise. Here are some floating point literals −
1e30f f e .1Boolean Literals
The Boolean literals true and false are members of type Boolean.
Symbol Literals
A symbol literal 'x is a shorthand for the expression scala.Symbol("x"). Symbol is a case class, which is defined as follows.
package scala final case class Symbol private (name: String) { override def toString: String = "'" + name }Character Literals
A character literal is a single character enclosed in quotes. The character is either a printable Unicode character or is described by an escape sequence. Here are some character literals −
'a' '\u' '\n' '\t'String Literals
A string literal is a sequence of characters in double quotes. The characters are either printable Unicode character or are described by escape sequences. Here are some string literals −
"Hello,\nWorld!" "This string contains a \" character."Multi-Line Strings
A multi-line string literal is a sequence of characters enclosed in triple quotes """ """. The sequence of characters is arbitrary, except that it may contain three or more consecutive quote characters only at the very end.
Characters must not necessarily be printable; newlines or other control characters are also permitted. Here is a multi-line string literal −
"""the present string spans three lines."""Null Values
The null value is of type scala.Null and is thus compatible with every reference type. It denotes a reference value which refers to a special "null" object.
Escape Sequences
The following escape sequences are recognized in character and string literals.
Escape Sequences | Unicode | Description |
---|---|---|
\b | \u | backspace BS |
\t | \u | horizontal tab HT |
\n | \uc | formfeed FF |
\f | \uc | formfeed FF |
\r | \ud | carriage return CR |
\" | \u | double quote " |
\' | \u | single quote . |
\\ | \uc | backslash \ |
A character with Unicode between 0 and may also be represented by an octal escape, i.e., a backslash '\' followed by a sequence of up to three octal characters. Following is the example to show few escape sequence characters −
Example
object Test { def main(args: Array[String]) { println("Hello\tWorld\n\n" ); } }When the above code is compiled and executed, it produces the following result −
Output
Hello World- Hamer usa guitar for sale
- 2016 chrysler 300 srt8 horsepower
- Tamper resistant nuts and bolts
- Lil man pt 2
"Fossies" - the Fresh Open Source Software Archive 
Member "scala/src/library/scala/Long.scala" (14 May , Bytes) of package /linux/misc/scalasrc.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Scala source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.
1 /* 2 * Scala (https://www.scala-lang.org) 3 * 4 * Copyright EPFL and Lightbend, Inc. 5 * 6 * Licensed under Apache License 7 * (http://www.apache.org/licenses/LICENSE). 8 * 9 * See the NOTICE file distributed with this work for 10 * additional information regarding copyright ownership. 11 */ 12 13 // DO NOT EDIT, CHANGES WILL BE LOST 14 // This auto-generated code can be modified in "project/GenerateAnyVals.scala". 15 // Afterwards, running "sbt generateSources" regenerates this source file. 16 17 package scala 18 19 /** `Long`, a bit signed integer (equivalent to Java's `long` primitive type) is a 20 * subtype of [[scala.AnyVal]]. Instances of `Long` are not 21 * represented by an object in the underlying runtime system. 22 * 23 * There is an implicit conversion from [[scala.Long]] => [[scala.runtime.RichLong]] 24 * which provides useful non-primitive operations. 25 */ 26 final abstract class Long private extends AnyVal { 27 def toByte: Byte 28 def toShort: Short 29 def toChar: Char 30 def toInt: Int 31 def toLong: Long 32 def toFloat: Float 33 def toDouble: Double 34 35 /** 36 * Returns the bitwise negation of this value. 37 * @example {{{ 38 * ~5 == -6 39 * // in binary: ~ == 40 * // 41 * }}} 42 */ 43 def unary_~ : Long 44 /** Returns this value, unmodified. */ 45 def unary_+ : Long 46 /** Returns the negation of this value. */ 47 def unary_- : Long 48 49 @deprecated("Adding a number and a String is deprecated. Use the string interpolation `s\"$num$str\"`","") 50 def+(x: String): String 51 52 /** 53 * Returns this value bit-shifted left by the specified number of bits, 54 * filling in the new right bits with zeroes. 55 * @example {{{ 6 << 3 == 48 // in binary: << 3 == }}} 56 */ 57 def<<(x: Int): Long 58 /** 59 * Returns this value bit-shifted left by the specified number of bits, 60 * filling in the new right bits with zeroes. 61 * @example {{{ 6 << 3 == 48 // in binary: << 3 == }}} 62 */ 63 def<<(x: Long): Long 64 /** 65 * Returns this value bit-shifted right by the specified number of bits, 66 * filling the new left bits with zeroes. 67 * @example {{{ 21 >>> 3 == 2 // in binary: >>> 3 == }}} 68 * @example {{{ 69 * >>> 3 == 70 * // in binary: >>> 3 == 71 * // 72 * }}} 73 */ 74 def>>>(x: Int): Long 75 /** 76 * Returns this value bit-shifted right by the specified number of bits, 77 * filling the new left bits with zeroes. 78 * @example {{{ 21 >>> 3 == 2 // in binary: >>> 3 == }}} 79 * @example {{{ 80 * >>> 3 == 81 * // in binary: >>> 3 == 82 * // 83 * }}} 84 */ 85 def>>>(x: Long): Long 86 /** 87 * Returns this value bit-shifted right by the specified number of bits, 88 * filling in the left bits with the same value as the left-most bit of this. 89 * The effect of this is to retain the sign of the value. 90 * @example {{{ 91 * >> 3 == -3 92 * // in binary: >> 3 == 93 * // 94 * }}} 95 */ 96 def>>(x: Int): Long 97 /** 98 * Returns this value bit-shifted right by the specified number of bits, 99 * filling in the left bits with the same value as the left-most bit of this. * The effect of this is to retain the sign of the value. * @example {{{ * >> 3 == -3 * // in binary: >> 3 == * // * }}} */ def>>(x: Long): Long /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Byte): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Short): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Char): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Int): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Long): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Float): Boolean /** Returns `true` if this value is equal to x, `false` otherwise. */ def==(x: Double): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Byte): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Short): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Char): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Int): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Long): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Float): Boolean /** Returns `true` if this value is not equal to x, `false` otherwise. */ def!=(x: Double): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Byte): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Short): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Char): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Int): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Long): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Float): Boolean /** Returns `true` if this value is less than x, `false` otherwise. */ def<(x: Double): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Byte): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Short): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Char): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Int): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Long): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Float): Boolean /** Returns `true` if this value is less than or equal to x, `false` otherwise. */ def<=(x: Double): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Byte): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Short): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Char): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Int): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Long): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Float): Boolean /** Returns `true` if this value is greater than x, `false` otherwise. */ def>(x: Double): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Byte): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Short): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Char): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Int): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Long): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Float): Boolean /** Returns `true` if this value is greater than or equal to x, `false` otherwise. */ def>=(x: Double): Boolean /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: * // | * // * // * }}} */ def|(x: Byte): Long /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: * // | * // * // * }}} */ def|(x: Short): Long /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: * // | * // * // * }}} */ def|(x: Char): Long /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: * // | * // * // * }}} */ def|(x: Int): Long /** * Returns the bitwise OR of this value and `x`. * @example {{{ * (0xf0 | 0xaa) == 0xfa * // in binary: * // | * // * // * }}} */ def|(x: Long): Long /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: * // & * // * // * }}} */ def&(x: Byte): Long /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: * // & * // * // * }}} */ def&(x: Short): Long /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: * // & * // * // * }}} */ def&(x: Char): Long /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: * // & * // * // * }}} */ def&(x: Int): Long /** * Returns the bitwise AND of this value and `x`. * @example {{{ * (0xf0 & 0xaa) == 0xa0 * // in binary: * // & * // * // * }}} */ def&(x: Long): Long /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: * // ^ * // * // * }}} */ def ^(x: Byte): Long /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: * // ^ * // * // * }}} */ def ^(x: Short): Long /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: * // ^ * // * // * }}} */ def ^(x: Char): Long /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: * // ^ * // * // * }}} */ def ^(x: Int): Long /** * Returns the bitwise XOR of this value and `x`. * @example {{{ * (0xf0 ^ 0xaa) == 0x5a * // in binary: * // ^ * // * // * }}} */ def ^(x: Long): Long /** Returns the sum of this value and `x`. */ def+(x: Byte): Long /** Returns the sum of this value and `x`. */ def+(x: Short): Long /** Returns the sum of this value and `x`. */ def+(x: Char): Long /** Returns the sum of this value and `x`. */ def+(x: Int): Long /** Returns the sum of this value and `x`. */ def+(x: Long): Long /** Returns the sum of this value and `x`. */ def+(x: Float): Float /** Returns the sum of this value and `x`. */ def+(x: Double): Double /** Returns the difference of this value and `x`. */ def-(x: Byte): Long /** Returns the difference of this value and `x`. */ def-(x: Short): Long /** Returns the difference of this value and `x`. */ def-(x: Char): Long /** Returns the difference of this value and `x`. */ def-(x: Int): Long /** Returns the difference of this value and `x`. */ def-(x: Long): Long /** Returns the difference of this value and `x`. */ def-(x: Float): Float /** Returns the difference of this value and `x`. */ def-(x: Double): Double /** Returns the product of this value and `x`. */ def*(x: Byte): Long /** Returns the product of this value and `x`. */ def*(x: Short): Long /** Returns the product of this value and `x`. */ def*(x: Char): Long /** Returns the product of this value and `x`. */ def*(x: Int): Long /** Returns the product of this value and `x`. */ def*(x: Long): Long /** Returns the product of this value and `x`. */ def*(x: Float): Float /** Returns the product of this value and `x`. */ def*(x: Double): Double /** Returns the quotient of this value and `x`. */ def/(x: Byte): Long /** Returns the quotient of this value and `x`. */ def/(x: Short): Long /** Returns the quotient of this value and `x`. */ def/(x: Char): Long /** Returns the quotient of this value and `x`. */ def/(x: Int): Long /** Returns the quotient of this value and `x`. */ def/(x: Long): Long /** Returns the quotient of this value and `x`. */ def/(x: Float): Float /** Returns the quotient of this value and `x`. */ def/(x: Double): Double /** Returns the remainder of the division of this value by `x`. */ def%(x: Byte): Long /** Returns the remainder of the division of this value by `x`. */ def%(x: Short): Long /** Returns the remainder of the division of this value by `x`. */ def%(x: Char): Long /** Returns the remainder of the division of this value by `x`. */ def%(x: Int): Long /** Returns the remainder of the division of this value by `x`. */ def%(x: Long): Long /** Returns the remainder of the division of this value by `x`. */ def%(x: Float): Float /** Returns the remainder of the division of this value by `x`. */ def%(x: Double): Double // Provide a more specific return type for Scaladoc override defgetClass(): Class[Long] = ??? } object Long extends AnyValCompanion { /** The smallest value representable as a Long. */ final val MinValue = java.lang.Long.MIN_VALUE /** The largest value representable as a Long. */ final val MaxValue = java.lang.Long.MAX_VALUE /** Transform a value type into a boxed reference type. * * Runtime implementation determined by `scala.runtime.BoxesRunTime.boxToLong`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]]. * * @param x the Long to be boxed * @return a java.lang.Long offering `x` as its underlying value. */ defbox(x: Long): java.lang.Long = ??? /** Transform a boxed type into a value type. Note that this * method is not typesafe: it accepts any Object, but will throw * an exception if the argument is not a java.lang.Long. * * Runtime implementation determined by `scala.runtime.BoxesRunTime.unboxToLong`. See [[https://github.com/scala/scala src/library/scala/runtime/BoxesRunTime.java]]. * * @param x the java.lang.Long to be unboxed. * @throws ClassCastException if the argument is not a java.lang.Long * @return the Long resulting from calling longValue() on `x` */ defunbox(x: java.lang.Object): Long = ??? /** The String representation of the scala.Long companion object. */ override def toString ="object scala.Long" /** Language mandated coercions from Long to "wider" types. */ import scala.language.implicitConversions @deprecated("Implicit conversion from Long to Float is dangerous because it loses precision. Write `.toFloat` instead.","") implicit deflong2float(x: Long): Float = x.toFloat @deprecated("Implicit conversion from Long to Double is dangerous because it loses precision. Write `.toDouble` instead.","") implicit deflong2double(x: Long): Double = x.toDouble }
Returns the number of one-bits in the two's complement binary representation of the specified value.
Returns the value of this as a after a narrowing primitive conversion.
Compares two values numerically.
Compares two objects numerically.
Compares two values numerically treating the values as unsigned.
Decodes a into a .
Returns the unsigned quotient of dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
Returns the value of this as a after a widening primitive conversion.
Compares this object to the specified object.
Returns the value of this as a after a widening primitive conversion.
Determines the value of the system property with the specified name.
Determines the value of the system property with the specified name.
Returns the value of the system property with the specified name.
Returns a hash code for this .
Returns a hash code for a value; compatible with .
Returns a value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified value.
Returns the value of this as an after a narrowing primitive conversion.
Returns the value of this as a value.
Returns a value with at most a single one-bit, in the position of the lowest-order ("rightmost") one-bit in the specified value.
Returns the greater of two values as if by calling .
Returns the smaller of two values as if by calling .
Returns the number of zero bits preceding the highest-order ("leftmost") one-bit in the two's complement binary representation of the specified value.
Returns the number of zero bits following the lowest-order ("rightmost") one-bit in the two's complement binary representation of the specified value.
Parses the string argument as a signed decimal .
Parses the string argument as a signed in the radix specified by the second argument.
Parses the string argument as an unsigned decimal .
Parses the string argument as an unsigned in the radix specified by the second argument.
Returns the unsigned remainder from dividing the first argument by the second where each argument and the result is interpreted as an unsigned value.
Returns the value obtained by reversing the order of the bits in the two's complement binary representation of the specified value.
Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified value.
Returns the value obtained by rotating the two's complement binary representation of the specified value left by the specified number of bits.
Returns the value obtained by rotating the two's complement binary representation of the specified value right by the specified number of bits.
Returns the value of this as a after a narrowing primitive conversion.
Returns the signum function of the specified value.
Adds two values together as per the + operator.
Returns a string representation of the argument as an unsigned integer in base 2.
Returns a string representation of the argument as an unsigned integer in base
Returns a string representation of the argument as an unsigned integer in base 8.
Returns a object representing this 's value.
Returns a object representing the specified .
Returns a string representation of the first argument in the radix specified by the second argument.
Returns a string representation of the argument as an unsigned decimal value.
Returns a string representation of the first argument as an unsigned integer value in the radix specified by the second argument.
Returns a instance representing the specified value.
Returns a object holding the value of the specified .
Returns a object holding the value extracted from the specified when parsed with the radix given by the second argument.
Java long scala long to
Why in Scala Long cannot in initialized to null whear as Integer can
Scala is literal data type like in Java. Same is true for . But is a Wrapper class i.e
If you want nullable Long value, you can use
and are Scala types and are derived from , which is not a reference type (i.e. not ) and hence can't be . on the other hand is an alias for , which is a reference type.
No, it's not an oversight, and are consistent, using in Scala is considered a bad thing and, as I understand it, exists merely for Java compatibility (even for ) as well as .
In Scala, type is a subtype of all reference types (all types which inherit from - which is an alias of ). That's why you can use the value (of type ) anywhere a reference type is expected.
It is not, however, a subtype of any value type (types which inherit from ). Because types such as , , (corresponding to Java's primitive types) are value types, they cannot be null.
As for the type you mention: I guess this is , which is a subtype of (a.k.a. ): it's a reference type, so you can use .
It's probably easier to understand with this diagram of the Scala type hierarchy (lifted from the official documentation):
Now discussing:
- Free train games to download
- Weber spirit 300 side controls
- September 25 zodiac sign
- List of samsung watches
- 7 piece dewalt tool set
- Motorcycle frames for sale
- Pisces zodiac sign facts
- 2008 cadillac cts coolant type
Databaseline
There are a few subtle changes between Scala and / when it comes to conversions between Java and Scala types that you may not be aware of: nullable boxed primitives, such as numbers.
Comparison
When dealing with Avro records in data pipelines in Scala (e.g. with Spotify’s open-source API for Apache Beam: Scio), you often need to convert back and forth between Java and Scala types, most commonly when reading from or writing to Avro. The interoperability between Java and Scala is fairly seamless thanks to the JVM. There are, however, differences from Scala to that may be tricky to spot if you’re not careful because it affects boxed primitive types, such as nullable boolean and numeric types.
If you want to run the following code yourself, you can either re-use the minimal sbt-based Scala REPL, or you can use Scastie in the convenience of your web browser.
Let’s now run some simple operations with these values and case classes in different Scala versions:
Snippet | () | (.8) | (M5) |
---|---|---|---|
NPE | NPE | NPE | |
NPE | NPE | NPE | |
NPE | NPE | NPE | |
NPE | NPE | NPE | |
NPE | |||
NPE | |||
NPE | |||
NPE | NPE | NPE | |
NPE | |||
NPE | |||
NPE | |||
NPE | |||
n/a | n/a | n/a |
The wildcard ‘’ is used to indicate the same expression is used for all five values , , , , and . NPE stands for . If it says ‘n/a’ it means it is not applicable because the code fails to compile due to type mismatches.
As you can see, the implicit conversions from Java types to Scala’s cause problems. Whereas in the behaviour was to throw NPEs when converting boxed values from Java to Scala, in and onwards some of these are converted to a default value (false for booleans and zero for numbers). You can easily check that the same table holds for other number types: , , , and so on.
Please note that a ‘instance’ of when passed to actually becomes even though gives an NPE.
Safe Conversions
In Scala , the following function would have allowed you to capture issues with nullable Java types and wrap these in an :
Because the argument is called by name rather than value (indicated by in the parameter list), it’s not evaluated when it’s handed to the function for further processing, but rather when it’s needed within the function itself. This means always ends up inside the monad, where it’s safe, even in the event of exceptions.
With it, you could have executed and it would have given you . In Scala and that does not work because of the implicit conversion needed to go from to Scala’s . To ensure type correctness, in is (as evidenced from the type annotation on the ), which means the argument to the function is automatically converted from to . With the table above we can see that the argument passed to the function is essentially , which is . The lack of an NPE thrown means it ends up in the first case and thus becomes .
Instead, we need a different way to handle this in Scala and I shall present three equivalent functions:
The second argument list, which reads , states that there has to be an implicit conversion in scope from to the return type . We could write this with a view bound, in which case the signature is as follows: . View bounds have been deprecated though.
A battery of ScalaTest unit tests is easily implemented:
Note that I have assumed a single method named rather than the three alternatives. So, which alternative should you pick?
Benchmarks
The third option has consistently proven to be faster with micro-benchmarks and actual production code. It also has a smaller memory footprint. The reason is obvious: has no unnecessary boxing and unboxing with the monad. That’s why I prefer that alternative, especially when running this at scale. Of course, you can rely on explicit checks and explicit type conversions, which would be even faster. However, you often want a generic method you can rely on in different situations.
If you want to do a poor person’s benchmark on your machine, here’s some code to get you started (without the need to play with JMH):