相关疑难解决方法(0)

使用上下文边界"消极地"以确保范围内不存在类型类实例

tl; dr:我如何做类似下面的编写代码:

def notFunctor[M[_] : Not[Functor]](m: M[_]) = s"$m is not a functor"
Run Code Online (Sandbox Code Playgroud)

' Not[Functor]',是这里的组成部分.
当'm'提供的不是Functor时,我希望它成功,否则编译器会失败.

解决:跳过问题的其余部分,然后直接回答下面的答案.


我粗略地说,我想要完成的是"负面证据".

伪代码看起来像这样:

// type class for obtaining serialization size in bytes.
trait SizeOf[A] { def sizeOf(a: A): Long }

// type class specialized for types whose size may vary between instances
trait VarSizeOf[A] extends SizeOf[A]

// type class specialized for types whose elements share the same size (e.g. Int)
trait FixedSizeOf[A] extends SizeOf[A] {
  def fixedSize: Long
  def sizeOf(a: A) …
Run Code Online (Sandbox Code Playgroud)

scala implicit typeclass higher-kinded-types scalaz

16
推荐指数
1
解决办法
1146
查看次数

标签 统计

higher-kinded-types ×1

implicit ×1

scala ×1

scalaz ×1

typeclass ×1