理解高阶键函数

时间:2016-01-03 15:23:43

标签: scala

给定ParentResult代数数据类型:

sealed trait Parent
case object Kid extends Parent

sealed trait Result[A <: Parent]
case class ResultImpl[A <: Parent](x: A) extends Result[A]
然后我写道:

def general[A <: Parent, F[_]](x: A, f: A => F[A]): F[A] = 
  f(x)

但是,我不确定如何调用general来获取类型ResultImpl[Kid.type]的输出。

我试过了:

scala> general(Kid, ResultImpl.apply)
<console>:19: error: inferred kinds of the type arguments (Kid.type,ResultImpl) do not conform to the expected kinds of the type parameters (type A,type F).
ResultImpl's type parameters do not match type F's expected parameters:
type A's bounds <: Parent are stricter than type _'s declared bounds >: Nothing <: Any
       general(Kid, ResultImpl.apply)
       ^
<console>:19: error: type mismatch;
 found   : Kid.type
 required: A
       general(Kid, ResultImpl.apply)
               ^
<console>:19: error: type mismatch;
 found   : Nothing => ResultImpl[Nothing]
 required: A => F[A]
       general(Kid, ResultImpl.apply)
                               ^

如何使用general功能执行此操作?

1 个答案:

答案 0 :(得分:4)

您需要指定_也是<: Parent,因为A是。A。此外,您可能希望将参数拆分为两组,以便编译器正确推断def general[A <: Parent, F[_ <: Parent]](x: A)(f: A => F[A]): F[A] = f(x)

general(Kid)(ResultImpl(_))

然后下一个按预期工作:

'meta_key'   =>  'event-start-date',
'meta_query' => array (
            array(
            'key' => 'start_time',
            'value' => date('Ymd',strtotime($newdate)),
            'compare' => '>=',
            'type' => 'date' 
            )
        )