Pattern matching around type erasure

You may have encountered this warning: "scala non-variable type argument in type pattern is unchecked since it is eliminated by erasure". If you google for it, you quickly find several decent Stack Overflow and forum replies that will suggest you to structure your code differently or use Type Manifests. They may even mention that there is an option to write custom extractor, but most of the time without transparent code sample. I decided to write this post to give such an easy-to-grasp code snippet.

Let's prepare the scene. Assume we have some generic class, which actual instance type will be erased.

class Container[T](data: T) {
  def get = data
}
// Let's simulate type erasure, otherwise Scala compiler will
// figure out the type. It is really that smart :)
def stored: Container[_] = new Container("hello")

Next, our straightforward pattern match may work in some cases, but will generate a warning and will not be able to distinguish for the type of the generic:

stored match {
  case c: Container[String] => println(c.get)
  // The second condition will never be executed
  case c: Container[Int] => println("Int " + c.get)
}

But it is very easy to create an extractor object:

object Container {
  def unapply[T](x: Container[T]) = Some(x.get)
}

Now you can use it hassle-free:

stored match {
  case Container(data: String) => println(data)
  case Container(data: Int) => println("Int " + data)
}

But what if you want to match on a type of generic, but still have the entire container object, not its content? That is simple as well:

stored match {
  // "x" will be the entire container
  case x @ Container(_: String) => println(x)
  case x @ Container(_: Int) => println("Int " + x)
}

This functionality is well documented and is safe to be used.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 该错误提示表示在SSH连接时没有找到匹配的主机密钥类型。这可能是由于SSH客户端和服务器之间的协议不匹配或服务器上没有正确配置主机密钥所致。建议检查SSH客户端和服务器之间的协议版本,并确保服务器上正确配置了主机密钥。 ### 回答2: "No matching host key type found" 是一个常见的 SSH 错误消息,意味着客户端和服务器之间无法找到匹配的主机密钥类型。 SSH (Secure Shell) 是一种加密协议,用于在网络安全地传输数据和管理远程服务器。每台 SSH 服务器都有一个主机密钥,用于验证服务器身份和加密通信。当客户端(例如电脑或终端)尝试连接到 SSH 服务器时,会检查服务器的主机密钥以确保安全通信。 出现"No matching host key type found"错误的原因可能是客户端和服务器之间的 SSH 配置不兼容,导致无法找到匹配的主机密钥类型。这可能是由于以下原因引起的: 1. 客户端和服务器使用的 SSH 版本不兼容。 2. 客户端和服务器配置存在不受支持的主机密钥类型。 3. 客户端和服务器之间存在网络连接问题,导致密钥交换失败。 要解决这个问题,可以尝试以下方法: 1. 确保客户端和服务器使用相同版本的 SSH 协议。如果有可能,尽量升级到最新版本的 SSH。 2. 检查客户端和服务器的 SSH 配置文件,确保正确配置支持的主机密钥类型。可以尝试使用更常见的密钥类型,例如 RSA 或 DSA。 3. 检查网络连接是否正常,确保客户端可以与服务器建立可靠的连接。可以尝试通过其他网络连接或重新启动网络设备来解决连接问题。 总之,"No matching host key type found" 错误表示客户端无法找到合适的主机密钥类型与服务器进行身份验证和加密通信。通过检查 SSH 配置、升级 SSH 版本以及确保网络连接正常,通常可以解决这个问题。 ### 回答3: "No matching host key type found" 是一个SSH连接错误信息,表示SSH客户端无法找到与主机密钥类型匹配的密钥。 当用户尝试使用SSH客户端连接到远程主机时,通常需要验证主机的身份。这个验证过程基于主机的密钥。然而,如果客户端无法找到与远程主机提供的密钥类型匹配的密钥,就会出现"No matching host key type found"错误。 这个错误通常出现在以下几种情况下: 1. 客户端和远程主机之间的SSH版本不兼容。在远程主机上使用的密钥类型可能是较新的,而客户端的SSH版本不支持这种密钥类型。 解决方法:更新客户端SSH软件到最新版本,以支持更多类型的密钥。 2. 主机密钥和客户端预期的密钥类型不匹配。客户端可能预期使用某种特定类型的密钥进行验证,但远程主机提供的密钥类型与之不匹配。 解决方法:通过检查远程主机的SSH配置文件,确认主机是否提供了预期类型的密钥。如果不匹配,可以尝试使用其他类型的密钥重新连接。 3. 客户端配置的密钥类型限制。有时,客户端的SSH配置文件可能限制了可接受的密钥类型,导致无法找到与远程主机提供的密钥类型匹配的密钥。 解决方法:检查客户端的SSH配置文件,查看是否限制了可接受的密钥类型。如果限制存在,可以考虑更新配置以接受更多类型的密钥。 总之,当出现"No matching host key type found"错误时,我们需要检查SSH客户端的版本、远程主机提供的密钥类型以及客户端的配置,以解决连接问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值