簡體   English   中英

Scala-將泛型類型限制為特定類型

[英]scala - constrain generic type to specific types

我不時地處理具有以下內容的java:

def printDbl(d:Double) { println("dbl: " + d) }
def printInt(i:Int) { println("int: " + i) }

自然,我想將其包裝在一些scala中,最終看起來像這樣:

def print[T:Manifest] (t:T) {
  if (manifest[T] <:< manifest[Int]) { printInt(t.asInstanceOf[Int]) ; return }
  if (manifest[T] <:< manifest[Double]) { printDbl(t.asInstanceOf[Double]) ; return }

  throw new UnsupportedOperationException("not implemented: " + manifest[T])
}

但是當我運行以下命令時,出現運行時異常:

print(1)
print(2.0)
print("hello")

我似乎記得有一種方法可以在編譯時捕獲它,但是我似乎無法對其進行谷歌搜索。 也許一些聰明的暗示轉換?

為什么不僅僅利用方法重載並像這樣編寫Scala包裝器呢?:

object Printer {
  def print(d: Double) { printDbl(d) }
  def print(i: Int) { printInt(i) }
}

這非常簡單,並提供了所需的行為:

import Printer._
print(1.)          // dbl: 1.0
print(1)           // int: 1
print("hello")     // compile-time type error
scala> object SpecType {
     |   trait SpecType[T] {
     |     def is(s: String): Boolean
     |   }
     |   implicit object DoubleType extends SpecType[Double] {
     |     def is(s: String) = s == "Double"
     |   }
     |   implicit object IntType extends SpecType[Int] {
     |     def is(s: String) = s == "Int"
     |   }
     | }
defined module SpecType 


scala> import SpecType._
import SpecType._

scala> def print[T: SpecType](x: T) {
     |   if(implicitly[SpecType[T]].is("Int")) println("Int")
     |   if(implicitly[SpecType[T]].is("Double")) println("Double")
     | }
print: [T](x: T)(implicit evidence$1: SpecType.SpecType[T])Unit

scala> print(1)
Int

scala> print(1.0)
Double

scala> print("")
<console>:21: error: could not find implicit value for evidence parameter of typ
e SpecType.SpecType[String]
              print("")

這是我想出的最好的

class CanPrint[T] (t:T) { def getT = t}
implicit def canPrint(i:Int) = new CanPrint[Int](i)
implicit def canPrint(d:Double) = new CanPrint[Double](d)    

def print[T:Manifest] (t:CanPrint[T]) {
    if (manifest[T] <:< manifest[Int]) { printInt(t.getT.asInstanceOf[Int]) ; return }
    if (manifest[T] <:< manifest[Double]) { printDbl(t.getT.asInstanceOf[Double]) ; return }

    throw new UnsupportedOperationException("not implemented: " + manifest[T])
}

以下不編譯

print(1)
print(1.0)
print("hello")

以下是我期望的

print(1)
print(1.0)

但是,這是不好的代碼,因為我必須導入隱式def才能使其正常工作,並且作為此代碼的使用者,我所看到的只是方法簽名,它表示我必須傳入一個CanPrint對象,該對象可以實例化。

print(new CanPrint("hello")) // pwned

我可以將構造函數設為私有,僅允許隱式方法訪問嗎?

def printDbl(d:Double) { println("dbl: " + d) }
def printInt(i:Int) { println("int: " + i) }

trait Printer[T] { def print(t:T) }
class PD extends Printer[Double] { def print(d:Double) = printDbl(d) }
class PI extends Printer[Int] { def print(i:Int) = printInt(i) }
implicit val pd = new PD()
implicit val pi = new PI()

def print[T](t:T)(implicit printer:Printer[T]) = printer.print(t)

print(1) // 1
print(2.0) // 2.0
print("hello") // Error:(88, 7) could not find implicit value for parameter printer: A$A336.this.Printer[String]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM