// Start writing your ScalaFiddle code here
sealed trait DSL[A]{
// def run(): A ={
// this match {
// case GetLength(something) =>
// something.length
// case ShowResult(number) =>
// s"the length is $number"
// }
// }
}
case class GetLength(something: String) extends DSL[Int]
case class ShowResult(number: Int) extends DSL[String]
def run[A](fa:DSL[A]): A ={
fa match {
case GetLength(something) =>
something.length
case ShowResult(number) =>
s"the length is $number"
}
}
val dslGetLength = GetLength("123456789")
val length = run(dslGetLength)
val …Run Code Online (Sandbox Code Playgroud) 如何TypeRepr在更高级的类型上正确地进行模式匹配?存在成功匹配类,但是当我尝试使用类型时,出现编译器错误unreducible application of higher-kinded type writetype.Foo to wildcard arguments
import scala.quoted.*
type Foo[X]
class Bar[X]
inline def writeType[T]: String = ${writeTypeImpl[T]}
def writeTypeImpl[T](using Type[T], Quotes): Expr[String] =
import quotes.reflect.*
val tpe = TypeRepr.of[T]
val s = tpe.asType match
//case '[Foo[?]] => "FooSuccess"
case '[Bar[?]] => "BarSuccess"
case _ => "Fail"
Expr(s)
Run Code Online (Sandbox Code Playgroud) 这是一个简单的例子:
trait Proposition[T] {
type Repr = T
}
trait Scope {
type OUT
type Consequent = Proposition[_ <: OUT]
abstract class Proof[-I, +P <: Consequent] {
final def instanceFor(v: I): P#Repr = ???
final def apply(v: I): P#Repr = instanceFor(v)
}
}
Run Code Online (Sandbox Code Playgroud)
这给出了编译错误:
[Error] ....scala:44: type mismatch;
found : _$1(in type Consequent)
required: (some other)_$1(in type Consequent)
Run Code Online (Sandbox Code Playgroud)
这是从哪里来(some other)的?是否是由明确的类型选择规则引起的编译器错误(理论上应该在scala 3中解决)?
更新 1抱歉,我刚刚意识到P#Repr不应该称为类型选择,它应该仅指val p: P;p.Repr,现在它增加了更多混乱,因为:
我什至不知道这个语法的名字,但我一直使用它很长一段时间
它甚至没有在 DOT 演算中定义。所以 scala 3 支持值得怀疑
scala existential-type path-dependent-type dependent-type scala-2.13
我一直在深入研究FP及其周围的一切,我发现了某种类型的投影仪的概念,没有细节也没有解释.
我发现的唯一一件事是这个github项目,我开始考虑它是指这个特定项目,还是FP中的一些通用概念?
那么,什么是投影仪?为什么有用?(如果可能的话,你能提供例子,资源等吗?)
functional-programming scala lambda-calculus higher-kinded-types kind-projector
给定一个图,我需要生成所有拓扑顺序。例如,给定下图:
我想生成所有拓扑顺序,这些顺序是:
因为可能存在许多拓扑顺序,所以我需要延迟生成它们。当前,我有一个递归工作的实现,并且可以在scala-graph库的顶部进行工作:
import scalax.collection.Graph
import scalax.collection.GraphPredef._
import scalax.collection.GraphEdge._
import scala.collection.mutable.ArrayStack
import scala.collection.Set
def allTopologicalSorts[T](graph: Graph[T, DiEdge]): Stream[List[graph.NodeT]] = {
val indegree: Map[graph.NodeT, Int] = graph.nodes.map(node => (node, node.inDegree)).toMap
def isSource(node: graph.NodeT): Boolean = indegree.get(node).get == 0
def getSources(): Set[graph.NodeT] = graph.nodes.filter(node => isSource(node))
def processSources(sources: Set[graph.NodeT], indegrees: Map[graph.NodeT, Int], topOrder: List[graph.NodeT], cnt: Int): Stream[List[graph.NodeT]] = {
if (sources.nonEmpty) {
// `sources` contain all …Run Code Online (Sandbox Code Playgroud) 我正在寻找某种通用参数的上限,以T确保这T是一个特征。
class Foo
trait Bar
def f[A ??? IsATrait] = ???
// f[Foo] Won't compile
f[Bar] // this is fine
Run Code Online (Sandbox Code Playgroud) 我正在试验它的一个库中提供的 scala 的精炼类型特性:
https://github.com/fthomas/refined
下面的代码代表一个简单的案例:
import eu.timepit.refined.auto._
import shapeless.{Witness => W}
type Vec5 = List[Int] Refined Size[Equal[W.`5`.T]]
val v1: Vec5 = List(1, 2, 3, 4, 5)
val v2: Vec5 = List(1 to 5: _*)
Run Code Online (Sandbox Code Playgroud)
尝试编译它时,我收到以下错误:
[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/refined_spike/Example.scala:32: compile-time refinement only works with literals
[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/refined_spike/Example.scala:34: compile-time refinement only works with literals
[Error] /home/peng/git/scalaspike/common/src/test/scala/com/tribbloids/spike/singleton_ops_spike/Example.scala:32: Cannot prove requirement Require[...]
three errors found
Run Code Online (Sandbox Code Playgroud)
应该注意的是,v1 和 v2 都可以在编译时轻松评估和内联,但是 Scala 编译器似乎拒绝这样做,并且对于Listtype 似乎没有办法建议这一点。
那么这个功能怎么会有用呢?
在 Cats 2.1.x 中,类型类实例被引入范围内import cats.implicits._
scala> import cats.Show
import cats.Show
scala> Show[Int].show(42)
<console>:13: error: could not find implicit value for parameter instance: cats.Show[Int]
Show[Int].show(42)
^
scala> import cats.implicits._
import cats.implicits._
scala> Show[Int].show(42)
res1: String = 42
Run Code Online (Sandbox Code Playgroud)
然而在 Cats 2.2.0中,它无需import cats.implicits._例如
scala> import cats.Show
import cats.Show
scala> Show[Int].show(42)
val res0: String = 42
Run Code Online (Sandbox Code Playgroud)
发生了什么变化以及从现在开始我们应该如何使用导入?
在讨论 F 有界多态性之前,我已经很难理解支撑它的结构了。
trait Container[A]
trait Contained extends Container[Contained]
Run Code Online (Sandbox Code Playgroud)
这种构造似乎是一个微不足道的面向对象的事情,因为它也存在于java中,已经让我有点困惑了。
问题是,当我看到这个时, trait Contained extends Container[Contained]我感觉就像是无限类型递归。
当我们定义类型 List 时,即使我们有Cons[A](a:A, tail:List[A]),我们也有case object Nil。所以递归可以以 Nil 结束。
但在这里,我不明白我们如何不处于无限递归中?以及为什么它有效。
有人可以帮我解答一下吗?或者是否有任何文档、博客或任何可以解释其工作原理或实现方式的内容。
trait Base
trait Plugin { base: Base =>
def asBase: Base & Plugin = this
}
class Mix extends Base, Plugin
val plug: Plugin = new Mix
val baseA: Base= plug.asBase
val baseB: Base = plug // snorts with "Found: Plugin. Required: Base
Run Code Online (Sandbox Code Playgroud)
为什么?如果我是正确的,则遵守里氏替换原则,因为 的所有实例Plugin都是具体类型,该类型是包含 的子类型的混合Base。因此,Base可以用type的对象替换type的对象Plugin,而不影响程序的正确性。
scala ×10
scala-macros ×3
scala-3 ×2
scala-cats ×2
generics ×1
graph ×1
implicit ×1
import ×1
refined ×1
scala-2.13 ×1
type-bounds ×1
typeclass ×1