Reserved keyword identifier in pattern matching

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Reserved keyword identifier in pattern matching

vmatter
Hello everyone,

In one of my projects, I wanted to do some standard pattern matching and I encountered a strange thing that I simplified in the following snippet:

val `type` = Some("random text")

"test" match {
 
case `type` => println("Hi !")
}

The compiler reports a type mismatch error on the case line (found: Some[String], required: String). In my understanding, the `type` identifier in the case should be shadowing the val declaration, as in the following snippet (which compiles and run correctly):

val normalIdentifier = Some("random text")

"test" match {
 
case normalIdentifier => println("Hi !")
}

Is there a reason why the behaviour is different if it's a reserved keyword identifier ?

Thank you for your future help.

Valentin

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved keyword identifier in pattern matching

David Lewis
I believe the issue is the back ticks. I think the scala compiler has a special case for them when pattern matching. If you changed your second example to have them around you would see the same warning.

On Tuesday, January 10, 2017 at 9:36:53 AM UTC-7, [hidden email] wrote:
Hello everyone,

In one of my projects, I wanted to do some standard pattern matching and I encountered a strange thing that I simplified in the following snippet:

val `type` = Some("random text")

"test" match {
 
case `type` => println("Hi !")
}

The compiler reports a type mismatch error on the case line (found: Some[String], required: String). In my understanding, the `type` identifier in the case should be shadowing the val declaration, as in the following snippet (which compiles and run correctly):

val normalIdentifier = Some("random text")

"test" match {
 
case normalIdentifier => println("Hi !")
}

Is there a reason why the behaviour is different if it's a reserved keyword identifier ?

Thank you for your future help.

Valentin

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved keyword identifier in pattern matching

Naftoli Gugenheim
Correct. IntelliJ would flag your second example as "Suspicious shadowing by a Variable Pattern." It will match any value, not your val. To match your val you need what I think is called a Constant Pattern, which means either you surround the name with backticks or you rename it to start with a capital letter.


On Tue, Jan 10, 2017 at 1:23 PM David Lewis <[hidden email]> wrote:
I believe the issue is the back ticks. I think the scala compiler has a special case for them when pattern matching. If you changed your second example to have them around you would see the same warning.


On Tuesday, January 10, 2017 at 9:36:53 AM UTC-7, [hidden email] wrote:
Hello everyone,

In one of my projects, I wanted to do some standard pattern matching and I encountered a strange thing that I simplified in the following snippet:

val `type` = Some("random text")

"test" match {
 
case `type` => println("Hi !")
}

The compiler reports a type mismatch error on the case line (found: Some[String], required: String). In my understanding, the `type` identifier in the case should be shadowing the val declaration, as in the following snippet (which compiles and run correctly):

val normalIdentifier = Some("random text")

"test" match {
 
case normalIdentifier => println("Hi !")
}

Is there a reason why the behaviour is different if it's a reserved keyword identifier ?

Thank you for your future help.

Valentin

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Reserved keyword identifier in pattern matching

Adriaan Moors-7
or, for option number 3 ;-)

scala> val `type` = "type"
type: String = type

scala> "type" match { case _@`type` => }

scala> "tpe" match { case _@`type` => }
scala.MatchError: tpe (of class java.lang.String)
  ... 29 elided


On Tue, Jan 10, 2017 at 6:45 PM Naftoli Gugenheim <[hidden email]> wrote:
Correct. IntelliJ would flag your second example as "Suspicious shadowing by a Variable Pattern." It will match any value, not your val. To match your val you need what I think is called a Constant Pattern, which means either you surround the name with backticks or you rename it to start with a capital letter.


On Tue, Jan 10, 2017 at 1:23 PM David Lewis <[hidden email]> wrote:
I believe the issue is the back ticks. I think the scala compiler has a special case for them when pattern matching. If you changed your second example to have them around you would see the same warning.


On Tuesday, January 10, 2017 at 9:36:53 AM UTC-7, [hidden email] wrote:
Hello everyone,

In one of my projects, I wanted to do some standard pattern matching and I encountered a strange thing that I simplified in the following snippet:

val `type` = Some("random text")

"test" match {
 
case `type` => println("Hi !")
}

The compiler reports a type mismatch error on the case line (found: Some[String], required: String). In my understanding, the `type` identifier in the case should be shadowing the val declaration, as in the following snippet (which compiles and run correctly):

val normalIdentifier = Some("random text")

"test" match {
 
case normalIdentifier => println("Hi !")
}

Is there a reason why the behaviour is different if it's a reserved keyword identifier ?

Thank you for your future help.

Valentin

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...