While trying to understand some code, I've run into strange behaviour and reduced it to this:
Introducing type parameter in a match doesn't work:
scala> Some(0) match { case _: Some[A] => 0 }
<console>:8: error: not found: type A
Some(0) match { case _: Some[A] => 0 }
^
However, if I make it lower-case, it does:
scala> Some(0) match { case _: Some[a] => 0 }
res2: Int = 0
Is this a bug in Scala or is there an explanation I am missing?
It is similar to the switch statement of Java and C. Here, “match” keyword is used instead of switch statement. “match” is always defined in Scala's root class to make its availability to the all objects. This can contain a sequence of alternatives. Each alternative will start from case keyword.
Use the getClass Method in Scala The getClass method in Scala is used to get the class of the Scala object. We can use this method to get the type of a variable.
When pattern matching, we assert that a certain piece of data is equal to a certain pattern. For example, in the function: head (element:list) = element. We assert that the first element of head 's argument is called element, and the function returns this.
The match case in python allows a number, string, ‘None’ value, ‘True’ value, and ‘False’ value as the pattern. Thus, different types of patterns are possible, and we will be looking into some of the possible patterns. We can have a wildcard pattern too.
Explanation: PHP variables are case-sensitive, i.e., $lfc and $LFC are treated differently. 6. How many variable scope are there in php? Explanation: Depending on the scopes, PHP has three variable scopes: Local, Global and static variable scope.
To implement switch-case like characteristics and if-else functionalities, we use a match case in python. A match statement will compare a given variable’s value to different shapes, also referred to as the pattern. The main idea is to keep on comparing the variable with all the present patterns until it fits into one.
A match statement will compare a given variable’s value to different shapes, also referred to as the pattern. The main idea is to keep on comparing the variable with all the present patterns until it fits into one.
You can see the same thing with value variables in patterns:
scala> Some(0) match { case A => 0 }
<console>:8: error: not found: value A
Some(0) match { case A => 0 }
^
scala> Some(0) match { case a => 0 }
res1: Int = 0
If you want to introduce a variable (either at the value or type level) in a pattern, you have to use a lower case identifier—there's simply no way to introduce an upper case variable. Note that going the other direction is possible—if you want to match against the value of a lower case variable, you can surround it with back quotes.
From the language specification (discussing a change introduced in 2.3):
The syntax of types in patterns has been refined (§8.2). Scala now distinguishes be- tween type variables (starting with a lower case letter) and types as type arguments in patterns. Type variables are bound in the pattern. Other type arguments are, as in previous versions, erased.
So no, not a bug, although it's arguably a pretty confusing language design decision.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With