简体   繁体   中英

Keep deduced higher-kinded type in scala

I have a higher-order type and work to build some DSL with it. And I'm searching for a way to define function that can accept the type without explicit specifying this type.

Self-describing example:

class Wrap[T] (val data : T)

class DSL {
  def doSomething[T](x : Wrap[T]) =
    println(x.data)
  def <<=[T,W <: Wrap[T]](arg : W) : W = {
    doSomething(arg)
    arg
  }
  def <<-[T](arg : Wrap[T]) : Wrap[T] = {
    doSomething(arg)
    arg
  }
  def <<+[W <: Wrap[_]](arg : W) = {
    doSomething(arg)
    arg
  }
  def <<~(arg : Wrap[_]) = {
    doSomething(arg)
    arg
  }
}

class ExtendedInt(x : Int) extends Wrap[Int](x) {
  def expose() = println(data)
}

object Test {
  val dsl = new DSL
  val exi = new ExtendedInt(3)

  val x1 = dsl <<= exi
  val x2 = dsl <<- exi
  val x3 = dsl <<+ exi
  val x4 = dsl <<~ exi

  x1.expose()
  x2.expose()
  x3.expose()
  x4.expose()
}

I've tried 4 different methods and got 4 different errors:

Casting.scala:15: error: no type parameters for method doSomething: (x: Wrap[T])Unit exist so that it can be applied to arguments (W)
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : W
 required: Wrap[?T]

    doSomething(arg)
    ^
Casting.scala:32: error: inferred type arguments [Nothing,ExtendedInt] do not conform to method <<='s type parameter bounds [T,W <: Wrap[T]]
  val x1 = dsl <<= exi
               ^
Casting.scala:38: error: value expose is not a member of Wrap[Int]
  x2.expose()
     ^
Casting.scala:40: error: value expose is not a member of Wrap[_$2]
  x4.expose()
     ^
four errors found

All errors are very descriptive. I have no objection towards scala's awkward type system and the limitations. But I'm still far from my object and I'm positive about searching for another hacks to implement desired functionality.

Is there any other methods that I've overlooked?

See my answer here (and this other answer linked there) for a discussion of the type inference limitations that are causing you problems here. Your best bet is probably the view bound approach:

class Wrap[T](val data: T)

class DSL {
  def doSomething[T](x : Wrap[T]) { println(x.data) }

  def <<=[T, W <% Wrap[T]](arg : W): W = {
    doSomething(arg)
    arg
  }
}

Note that your <: has been replaced by <% . This will work as expected.

@Travis-Brown suggested a solution that I was unable to incorporate in my code since I need to keep all typing information.

The renewed example incorporate new requirements:

class DSL {
  def doSomething[T](x : Wrap[T]) =
    println(x.data)
  /** Should really receive both types */
  def realWork[T, W <: Wrap[T]]( arg : W ) : W = {
    doSomething(arg)
    arg
  }

  /** syntax sugar for calling realWork*/
  def <<=[T, W <: Wrap[T]](arg : W) : W = realWork[T,W](arg) // This doesn't work
}

I need to replace <<= method with something working. View bounds could not supply me with enough type information for calling realWork.

I've discovered that just doubling arguments works fine and sends to the compiler needed type information.

  def <<=[T, W <: Wrap[T]](arg : W, trash : Wrap[T]) : W = realWork[T,W](arg)

The usage is inconvenient but may be easily improved:

implicit def doubleArg[T](arg : T) : (T,T) = (arg,arg)
...
def <<=[T, W <: Wrap[T]]( args : (W, Wrap[T]) ) : W = realWork[T,W](args._1)

There is one drawback in the implicit conversion: Tuple2 is a very popular type and casting any single to the tuple is dangerous.

So I introduced my own type for implicit conversion only, that doesn't pollute the entire type system.

class SugarPair[+T1 <: T2, +T2] (val s1 : T1) {
  def apply() : T1 = s1
}
implicit def toSugarPair[T](sugar : T) : SugarPair[T,T] = new SugarPair[T,T](sugar)
...
def <<=[T, W <: Wrap[T]](arg : SugarPair[W,Wrap[T]]) : W = realWork[T,W](arg() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM