Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type variable can only be introduced in match if it's lower-case?

Tags:

generics

scala

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?

like image 417
Alexey Romanov Avatar asked Feb 03 '14 11:02

Alexey Romanov


People also ask

What is Scala match case?

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.

How do you find the type of a variable in Scala?

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.

What is pattern matching define a pattern explain with an example?

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.

What are the different types of match cases in Python?

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.

Is LFC case sensitive in PHP?

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.

How to implement switch-case like characteristics in Python?

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.

What is a match statement 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.


1 Answers

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.

like image 147
Travis Brown Avatar answered Nov 15 '22 06:11

Travis Brown