PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label unchecked. Show all posts
Showing posts with label unchecked. Show all posts

Sunday, July 17, 2022

[FIXED] Where to put the `@unchecked` to suppress "pattern match on a refinement type is unchecked"?

 July 17, 2022     annotations, scala, suppress-warnings, unchecked, warnings     No comments   

Issue

When I run the following code snippet with scala

import scala.language.reflectiveCalls

def foo(a: Option[Any]): Option[Any] = {
  a.filter {
    case x: { def bar: Boolean } => x.bar
  }
}

object Bar {
  def bar: Boolean = true
}

println(foo(Some(Bar)))

I get a warning

warning: a pattern match on a refinement type is unchecked

I've tried the following:

@unchecked case x: { def bar: Boolean } => x.bar
case (@unchecked x): { def bar: Boolean } => x.bar
case (x @unchecked): { def bar: Boolean } => x.bar
case x: @unchecked { def bar: Boolean } => x.bar
case x: { def bar: Boolean } @unchecked => x.bar
case (x: { def bar: Boolean } @unchecked) => x.bar
case x: ({ def bar: Boolean } @unchecked) => x.bar
case x: { def bar: Boolean } => (x @unchecked).bar
case x: { def bar: Boolean } => (x: { def bar: Boolean } @unchecked).bar

None of those work. This also doesn't work:

  a.filter { any => (any: @unchecked) match {
    case x: { def bar: Boolean } => x.bar
  }}

How do I suppress this warning?


Somewhat related links

This answer seems to use the @unchecked successfully inside Some(...), but I don't see how to use it with filter.


Solution

An additional pair of round parentheses around the { def ... } is required:

case x: ({ def bar: Boolean }) @unchecked => x.bar

With the additional parentheses, it compiles just fine, without any warnings.


This seems to be similar to the syntax for the "classical type-lambdas", where

({ type Lam[X] = Foo[X] })#Lam

worked, whereas

{ type Lam[X] = Foo[X] }#Lam

didn't.



Answered By - Andrey Tyukin
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home
View mobile version

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing