我正在尝试创建一个函数,它采用高级类型的元组并将函数应用于高级类型中的类型。
在下面的例子中,有一个trait Get[A]是我们的高级类型。还有一个 Get's: 元组(Get[String],Get[Int])以及来自(String,Int) => Person.
Scala-3 有一个名为 InverseMap 的匹配类型,它将类型 (Get[String], Get[Int]) 转换为本质上的类型 (String,Int)。
所以最终目标是编写一个函数,它可以接受一个任意数量Get[_]类型的元组和一个输入与 InserveMap 类型匹配的函数,并最终返回 a Get[_],其中包装的类型是函数的结果。
我试图创建一个genericF在下面调用的函数来显示所需的行为,尽管它可能不正确——但我认为它至少显示了正确的意图。
case class Person(name: String, age: Int)
trait Get[A] {
def get: A
}
case class Put[A](get: A) extends Get[A]
val t: (Get[String], Get[Int]) = (Put("Bob"), Put(42))
val fPerson: (String,Int) => Person = Person.apply _
def genericF[T<:Tuple,I<:Tuple.InverseMap[T,Get],B](f: I => B, t: T): Get[B] = ???
val person: Get[Person] = …Run Code Online (Sandbox Code Playgroud) 我有一个 scala3.0.0-RC1项目,我正在尝试升级到 scala 3.0.0。
我将 sbt 版本设置为1.5.2,scalaVersion := "3.0.0"但出现此错误:
[warn] Note: Unresolved dependencies path:
[error] sbt.librarymanagement.ResolveException: Error downloading org.scala-lang:scala3-library_3.0.0:3.0.
Run Code Online (Sandbox Code Playgroud) 学习 Scala3 扩展和 CanEqual 概念,但发现扩展 Int 的某些特性有困难。
在以下示例中,我可以轻松地向 Int 添加 >= 功能以将其与 RationalNumber 案例类进行比较,但无法修改 == 的行为。(注 1~2 与 RationalNumber(1,2) 相同)。
这个问题似乎与基本的 AnyVal 类型以及 Scala 如何传递给 Java 来处理 equals 和 == 相关。
case class RationalNumber(val n: Int, val d: Int):
def >=(that:RationalNumber) = this.num * that.den >= that.num * this.den
//... other comparisons hidden (note not using Ordered for clarity)
private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
val sign = …Run Code Online (Sandbox Code Playgroud) 在学习 Scala 3 时,我看到了一种新的编写方式main:
@main def main1 =
println("main1 printed something")
Run Code Online (Sandbox Code Playgroud)
我检查了来源@main,它只是
class main extends scala.annotation.Annotation {}
Run Code Online (Sandbox Code Playgroud)
使用@main这里发生了什么?
我正在阅读 Scala 3 文档。他们引入了given关键字,被认为是 Scala 2 的替代品implicit。代码在这里
trait Ord[T] {
def compare(x: T, y: T): Int
def (x: T) < (y: T) = compare(x, y) < 0
def (x: T) > (y: T) = compare(x, y) > 0
}
given intOrd: Ord[Int] {
def compare(x: Int, y: Int) =
if (x < y) -1 else if (x > y) +1 else 0
}
given listOrd[T]: (ord: Ord[T]) => Ord[List[T]] {
def compare(xs: List[T], ys: List[T]): Int …Run Code Online (Sandbox Code Playgroud) Scala 3 提供了类似于 shapeless 的多态函数和元组HList:
scala> 1 *: "foo" *: Tuple()
val res0: (Int, String) = (1,foo)
scala> val f: ([T] => T => Option[T]) = [T] => (v: T) => Some(v)
val f: PolyFunction{apply: [T](x$1: T): Option[T]} = <function1>
scala> res0.map(f)
val res1: Option[Int] *: Option[String] *: EmptyTuple = (Some(1),Some(foo))
Run Code Online (Sandbox Code Playgroud)
我们如何使用 Scala 3 功能重新实现以下无形示例?
import poly._
object choose extends (Set ~> Option) {
def apply[T](s : Set[T]) = s.headOption
}
scala> val …Run Code Online (Sandbox Code Playgroud) 我尝试在 WriterT 上使用 flatMap 并且成功了。
所以问题可能出在我的类型上,但我找不到它有什么问题。
import cats.Monad
import cats.syntax.flatMap._
object Main extends App {
type Optional[A] = A | Null
val maybeInt1: Optional[Int] = 1
val maybeInt2: Optional[Int] = null
given Monad[Optional] with {
def pure[A](x: A): Optional[A] = x
def flatMap[A, B](fa: Optional[A])(f: A => Optional[B]): Optional[B] = {
fa match {
case null => null
case a: A => f(a)
}
}
def tailRecM[A, B](a: A)(f: A => Optional[Either[A, B]]): Optional[B] = {
f(a) match {
case …Run Code Online (Sandbox Code Playgroud) TypeTest我在理解scala3 中的 s 如何替代scala 2 中的 s 时遇到问题。TypeTag用例能够匹配像 x: List[Int] 这样的通用参数。
我试图解决的具体例子:
enum Foo :
case Bar()
case Baz()
case class Mod[T <: Foo](modFn: T => T)
def modifyBarsOrBaz(mod: Mod[_]) = mod match
case barMod: Mod[Foo.Bar] => ???
case bazMod: Mod[Foo.Baz] => ???
Run Code Online (Sandbox Code Playgroud)
编译器警告中的编译结果(如预期)
the type test for Mod[Foo.Bar] cannot be checked at runtime以及一个无法触及的案例。
现在我的问题是:这在 scala3 中可以做到吗?
我的印象是,我必须以某种方式TypeTest[Any, Mod[Foo.X]]为所有作为枚举子类的 X 提供一个Foo。
但我什至很难实现这些测试,以及了解什么using参数modifyBarsOrBaz其工作所需的
因此我想出了以下(不起作用)解决方案:
def modifyBarsOrBaz[T <: Foo](mod: Mod[T])(using …Run Code Online (Sandbox Code Playgroud) 我正在使用 scala3 进行编码,利用编程结构类型。结构类型恰好模仿了现有的案例类:它们的定义是纯粹的样板,因此很容易通过元编程来制作它们。
我了解如何制作函数实现,通常是通过类型类派生。但在这里我们正在尝试制作一个(结构)类型。
这在 scala2 中是可能的,通过类宏注释,但这些在 scala3 中已经消失了。有办法吗?如果是这样怎么办?
下面的代码是我想获得的结果:
// Library part
trait View extends scala.Selectable :
def selectDynamic(key:String) =
println(s"$key is being looked up")
???
// DSL Definition part
case class SomeDefWithInt ( i : Int )
case class SomeDefWithString( s : String )
// Boiler-plate code
type ViewOf[M] = M match
case SomeDefWithInt => View { def i : Int }
case SomeDefWithString => View { def s : …Run Code Online (Sandbox Code Playgroud) 尝试掌握 Scala 3 类型系统。问题:
def curry(f: ???) = ...接受f任意数量并返回柯里化 fn 的通用函数?没有编译器插件,没有外部花哨的库,只是用普通 Scala 3 表达的 N 元函数?(这个问题的目的不是使用任何外部库 - 目的是学习使用 Scala 3 作为工具的函数式编程概念。有一种感觉,这可能与将 args 作为元组处理或将 fn 转换为元组 fn 相关?我觉得 fn args 和元组的概念之间存在一些对称性?)
scala ×10
scala-3 ×10
dotty ×4
currying ×1
equals ×1
hlist ×1
implicit ×1
reflection ×1
sbt ×1
scala-cats ×1
scala-macros ×1
tuples ×1
typeclass ×1