我有一个案例课
case class Doc(title:String, ....)
Run Code Online (Sandbox Code Playgroud)
我想转换为lift-json JObject所以我可以更容易地使用JObject方法合并等.
关于存在主义类型的一点混乱.
这对我有用:
def valueOf(c: Class[_], name: String) {
type C = Class[T] forSome {type T <: Enum[T]}
Enum.valueOf(c.asInstanceOf[C], name)
}
Run Code Online (Sandbox Code Playgroud)
但这不是:
def valueOf(c: Class[_], name: String) {
type T = T forSome {type T <: Enum[T]}
Enum.valueOf(c.asInstanceOf[Class[T]], name)
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这两个表达式都相当于:
Enum.valueOf(z.asInstanceOf[Class[T] forSome {type T <: Enum[T]}], name)
Run Code Online (Sandbox Code Playgroud)
但斯卡拉说,这只是我的想法:
inferred type arguments [T] do not conform to method valueOf's type parameter bounds [T <: Enum[T]]
Enum.valueOf(c.asInstanceOf[Class[T]], name)
^
Run Code Online (Sandbox Code Playgroud) 我想在application.conf一个变量中定义一个字符串列表。目前在 application.conf 中我有这样的内容:
some.env.variable = ["a", "b"]
Run Code Online (Sandbox Code Playgroud)
我试过:
some.env.variable = ${?I_AM_ENV}.split(",")
Run Code Online (Sandbox Code Playgroud)
但I_AM_ENV = a,b 它不起作用。
加载应用程序时出现错误:
Wrong value type at 'some.env.variable', expecting: list but got: string
Run Code Online (Sandbox Code Playgroud) 我正在使用SBT 1.x构建一个scala项目(在scala 2.11中编写).有一些"scala版本"让我感到困惑.
SBT 1.x => scala 2.12
SBT plugin => scala 2.x
My project => scala 2.11
Run Code Online (Sandbox Code Playgroud)
请帮我弄清楚它们之间的区别或关系.在编译或运行项目时SBT如何区分它们?
在下面的示例中,我想定义一个contains方法,如果a和b不是相同的基本类型,则不会编译。
contains1impl中,如果ais Seq[Int]和b is String,T则派生为be Any,并进行编译。这不是我想要的。contains2impl中,如果ais Seq[Int]和b是String,则它不会编译。行为就是我想要的。def contains1[T](a: Seq[T], b: T): Boolean = a.contains(b)
println(contains1(Seq(1,2,3), "four")) // false
def contains2[T: Ordering](a: Seq[T], b: T): Boolean = a.contains(b)
println(contains2(Seq(1,2,3), "four")) // compilation error
// cmd7.sc:1: No implicit Ordering defined for Any.
// val res7 = isMatched(Seq(1,2,3), "s")
^
// Compilation Failed
Run Code Online (Sandbox Code Playgroud)
但是,是否有一种更简单的方法来实现与中相同的行为contains2?Ordering上下文绑定使我感到困惑,因为该方法与排序/排序完全无关。
为什么List[scala.Int]类型擦除,以List[Object]同时Integer在List[java.lang.Integer]似乎会保留吗?例如,javap对于
object Foo {
def fooInt: List[scala.Int] = ???
def fooInteger: List[java.lang.Integer] = ???
}
Run Code Online (Sandbox Code Playgroud)
输出
public scala.collection.immutable.List<java.lang.Object> fooInt();
public scala.collection.immutable.List<java.lang.Integer> fooInteger();
Run Code Online (Sandbox Code Playgroud)
我们看到的Integer是第二种情况。文档状态
Object如果泛型类型中的所有类型参数都带有边界,或者类型参数不受限制,则将其替换。
这可能是由于“ bounds”子句引起的吗?如果是这样,此界限在哪里指定?
给定的
trait Int // proper type
trait List[A] // 1st-order-kinded type constructor
trait Functor[F[_]] // higher-order-kinded type constructor taking type constructor
trait I[H[F[_]]] // higher-order-kinded type constructor taking higher-order type constructor that takes 1st-order type constructor
Run Code Online (Sandbox Code Playgroud)
与声明的类型参数的类型相比,我们不能传递不同类型的类型参数
scala> def f[F[_[_[_]]]] = 42
def f[F[_$1]] => Int
scala> f[I]
val res5: Int = 42
scala> f[Functor]
1 |f[Functor]
| ^
| Type argument Functor does not conform to upper bound [_$1[_$2]] =>> Any
Run Code Online (Sandbox Code Playgroud)
但是我们可以通过以下方式声明类型参数是多态的 AnyKind
scala> def f[A <: AnyKind] = …Run Code Online (Sandbox Code Playgroud) Id 文件说明
身份 monad ... 是环境的,因为纯纯值是 的值
Id。
什么是环境?环境一词与纯值的含义有何关系?为什么我们可以说纯值of Id,什么时候Id是类型构造函数?再说一次,Id实际上是类型构造函数或正确类型,因为:kind命令没有输出
scala> type Id[A] = A
type Id
scala> :kind -v Id
scala>
Run Code Online (Sandbox Code Playgroud) 如何转换List[Either[String, Int]]为Either[List[String], List[Int]]使用类似于cats sequence的方法?例如, xs.sequence在以下代码中
import cats.implicits._
val xs: List[Either[String, Int]] = List(Left("error1"), Left("error2"))
xs.sequence
Run Code Online (Sandbox Code Playgroud)
返回,Left(error1)而不是required Left(List(error1, error2))。
凯文·赖特的答案表明
val lefts = xs collect {case Left(x) => x }
def rights = xs collect {case Right(x) => x}
if(lefts.isEmpty) Right(rights) else Left(lefts)
Run Code Online (Sandbox Code Playgroud)
哪个返回Left(List(error1, error2)),但是猫是否提供开箱即用的排序方式来收集所有剩余数据?
我的多项目 sbt 存储库中有 scala 格式插件。
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.2")
Run Code Online (Sandbox Code Playgroud)
所以在 sbt 控制台中如果我运行 scalafmt 它工作正常
我的 build.sbt 有:
scalafmtOnCompile := true
Run Code Online (Sandbox Code Playgroud)
如果我在 sbt 中执行 ~compile 或只是手动编译,它不会在编译期间格式化我的代码。
我的设置有什么问题吗?
另外,运行 scalafmt 可以工作,但它不会像我的 Dependency.scala 文件那样格式化 /project 中的 .scala 文件。为什么它忽略这些文件?
我也使用带有金属的 VS Code 作为我的 IDE。
scala ×10
sbt ×2
types ×2
arrays ×1
enums ×1
java ×1
json ×1
lift ×1
polymorphism ×1
sbt-plugin ×1
scala-3 ×1
scala-cats ×1
scala-metals ×1
scalafmt ×1
terminology ×1
type-erasure ×1
version ×1