gpt4 book ai didi

scala - 是否可以使用适用于 Everywhere 的非对象 shapeless.Poly1?

转载 作者:行者123 更新时间:2023-12-01 09:13:23 26 4
gpt4 key购买 nike

我的以下尝试不起作用:

class Absolute(rootDir: File) extends Poly1 {
implicit def caseFile[F <: File]: Case[F] { type Result = File } = at[F] { f =>
if (f.isAbsolute)
f
else
val parts =
file.getPath.split(Pattern.quote(File.separator)).toList.filterNot(Set("", "."))
parts match {
case ":root-dir" :: tail => tail.foldLeft(rootDir)(new File(_, _))
case ":home" :: tail => tail.foldLeft(new File(sys.props("user.home")))(new File(_, _))
case _ => f
}
}
}

object Absolutised {
def from(rootDir: File) = new Runner(new Absolute(rootDir))

final class Runner(absolute: Absolute) {
def apply[T](t: T)(implicit everywhere: Everywhere[absolute.type, T]) = everywhere(t)
}
}

最佳答案

不理想,但在 scala 2.11.8 和 shapeless 2.3.0 中有一个解决方法:

object Absolutised {
def from(rootDir: File) = new Runner(rootDir)

final class Runner(rootDir: File) {

object ab2 extends Absolute(rootDir)

def apply[T](t: T)(implicit everywhere: Everywhere[ab2.type, T]) = everywhere(t)
}
}

关于scala - 是否可以使用适用于 Everywhere 的非对象 shapeless.Poly1?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36520012/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com