小编Mar*_*lic的帖子

如何将case类转换为lift-json jobject类?

我有一个案例课

case class Doc(title:String, ....)
Run Code Online (Sandbox Code Playgroud)

我想转换为lift-json JObject所以我可以更容易地使用JObject方法合并等.

json scala lift

9
推荐指数
1
解决办法
1726
查看次数

Scala中的Existensial类型

关于存在主义类型的一点混乱.

这对我有用:

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)

enums types scala existential-type

9
推荐指数
1
解决办法
519
查看次数

如何读取application.conf中的数组环境变量?

我想在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)

arrays scala environment-variables typesafe-config

9
推荐指数
2
解决办法
4677
查看次数

当我使用sbt构建scala项目时,scala版本的关系是什么?

我正在使用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如何区分它们?

scala version sbt sbt-plugin

9
推荐指数
1
解决办法
2889
查看次数

如何使用不能为Any的参数类型定义Scala方法

在下面的示例中,我想定义一个contains方法,如果ab不是相同的基本类型,则不会编译。

  • contains1impl中,如果ais Seq[Int]和b is StringT则派生为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)

但是,是否有一种更简单的方法来实现与中相同的行为contains2Ordering上下文绑定使我感到困惑,因为该方法与排序/排序完全无关。

types scala type-constraints

9
推荐指数
1
解决办法
111
查看次数

List [Int]和List [Integer]的类型擦除的差异

为什么List[scala.Int]类型擦除,以List[Object]同时IntegerList[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”子句引起的吗?如果是这样,此界限在哪里指定?

java scala type-erasure scala-java-interop

9
推荐指数
1
解决办法
114
查看次数

AnyKind 的种类多态性用例

给定的

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)

polymorphism scala scala-3

9
推荐指数
0
解决办法
126
查看次数

环境 monad 中术语环境的含义

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)

functional-programming scala terminology

8
推荐指数
1
解决办法
140
查看次数

将List [Either [A,B]]转换为Either [List [A],List [B]]

如何转换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)),但是猫是否提供开箱即用的排序方式来收集所有剩余数据?

scala scala-cats

7
推荐指数
1
解决办法
227
查看次数

使用 vs code,如何让 scala 格式工作并格式化我的代码?

我的多项目 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 sbt visual-studio-code scalafmt scala-metals

7
推荐指数
1
解决办法
4624
查看次数