使用Scala特征建模C ++概念

时间:2013-02-21 11:29:00

标签: scala c++-concepts

在纸«Type Classes as Objects and Implicits»中有一些使用Scala特征的例子,如C ++概念和Haskell类型类。我尝试在Scala中编写类似InputIterator概念和find函数的内容:

concept InputIterator<typename Iter> {
  typename value_type;
  value_type operator*(Iter);
  ...
};

template<typename Iter, typename V>
requires InputIterator<Iter> && EqualityComparable<Iter::value_type, V>
Iter find(Iter first, Iter last, V v) { 
    while (first < last && *first != v)
        ++first;
    return first;
}

我不确定我是否正确理解了特征。但仍然......用Scala编写InputIterator特征(或者更准确地说 - 它是用find函数中使用的方法简化的模拟):

trait InputIterator[Iter] {
    type value_type

    def <(a: Iter, b: Iter): Boolean
    def ++(it: Iter): Unit
    def *(it: Iter): value_type
}

EqualityComparable很清楚:

trait EqualityComparable[S, T] {
    def ==(s: S, t: T): Boolean
    def !=(s: S, t: T): Boolean = !(s == t)
}

但我们应该如何处理find?我想写这样的东西:

def find[Iter, V](first: Iter, last: Iter, x: V)(implicit iterator: InputIterator[Iter], 
    cmp: EqualityComparable[iterator.value_type, V]): Iter =
{
    while (iterator.<(first, last) && cmp.!=(iterator.*(first), x))
        iterator.++(first)
    first
}

但它会导致错误«非法依赖方法类型»。我不知道如何以其他方式“提取”抽象类型value_type。因此,我得到了这段代码:

trait EqualityComparable[S, T] {
    def ==(s: S, t: T): Boolean
    def !=(s: S, t: T): Boolean = !(s == t)
}

trait InputIterator[Iter] {
    type value_type

    def <(a: Iter, b: Iter): Boolean
    def ++(it: Iter): Unit
    def *(it: Iter): value_type
}

trait VTInputIterator[Iter, VT] extends InputIterator[Iter] {
    type value_type = VT
}

class ArrayListIterator[T](a: ArrayList[T], i: Int) {
    val arr: ArrayList[T] = a
    var ind: Int = i

    def curr(): T = arr.get(ind)
    def ++(): Unit = { ind += 1 }

    override def toString() = "[" + ind.toString() + "]"
}

class InputIterArrList[T] extends VTInputIterator[ArrayListIterator[T], T]{ 
    def <(a: ArrayListIterator[T], b: ArrayListIterator[T]) = {
      if (a.arr == b.arr) a.ind < b.ind
      else throw new IllegalArgumentException()
    }

    def ++(it: ArrayListIterator[T]): Unit = it.++()
    def *(it: ArrayListIterator[T]) = it.curr()
}

object TestInputIterator extends Application{   

    def find[Iter, VT, V](first: Iter, last: Iter, x: V)(implicit iterator: VTInputIterator[Iter, VT], 
        cmp: EqualityComparable[VT, V]): Iter = 
    {
      while (iterator.<(first, last) && cmp.!=(iterator.*(first), x))
        iterator.++(first)
      first
    }

    implicit object EqIntInt extends EqualityComparable[Int, Int] {
      def ==(a: Int, b: Int): Boolean = { a == b }
    }

    implicit object inputIterArrListInt extends InputIterArrList[Int]{}

    val len = 10;
    var arr: ArrayList[Int] = new ArrayList(len);
    for (i: Int <- 1 to len)
      arr.add(i)
    var arrFirst = new ArrayListIterator(arr, 0)
    var arrLast = new ArrayListIterator(arr, len)
    var r = find(arrFirst, arrLast, 7)
    println(r)
}

我们在VT中使用了类型参数def find[Iter, VT, V],而不是抽象类型。

所以问题是:如何做得更好?是否可以使用抽象类型value_type而无需其他类型参数VT

1 个答案:

答案 0 :(得分:4)

将find的签名更改为:

def find[Iter, V, II <: InputIterator[Iter]](first: Iter, last: Iter, x: V)(
  implicit iterator: II, cmp: EqualityComparable[II#value_type, V]): Iter

这可能是你想表达的。

请注意,您的Scala代码与C ++代码实际上并不相同。在C ++ find中使用Iter::value_type,但在Scala中使用InputIterator[Iter]#value_type