考虑一个案例类Person
case class Person(firstName: String, lastName: String, middleName: Option[String])
object Person {
def apply(firstName: String, lastName: String): Person = new Person(firstName, lastName, None)
def unapply(arg: Person): Option[(String, String)] = Some(arg.firstName, arg.lastName)
}
val person = Person("firstName", "lastName")
person match {
case Person(firstName, lastName) => Console.println(firstName + " " + lastName)
}
Run Code Online (Sandbox Code Playgroud)
在与case类编译器的模式匹配中给出了一个错误:模式匹配中的参数数量错误,但是当我使用类而不是case类时它的工作.
class Person(val firstName: String, val lastName: String, middleName: Option[String])
Run Code Online (Sandbox Code Playgroud)
我在这里理解的是,我们不能为case类使用自己的自定义提取器,但可以使用自己的构造函数(apply).请解释我这种奇怪的行为.
这是一个测试的ScalaFiddle:https://scalafiddle.io/sf/HvxvdAZ/0
现在,我无法使用“播放配置”启动我的“播放项目”-它给了我这个:
[error] Not a valid project ID: myProject
[error] project myProject
[error] ^
Run Code Online (Sandbox Code Playgroud)
我尝试了所有这些建议:
普通的SBT配置有效。
我想开发一个sbt 插件,该插件具有仅适用于Scala 2.13 的依赖项。
我在https://github.com/sbt/sbt/issues/5032 上找到了这个列表:
- SBT 0.x 仅在 Scala 2.10.x 上运行
- SBT 1.x 仅在 Scala 2.12.x 上运行
- SBT 2.x 只能在 Scala 2.13.x 或 3.0.x 上运行
- SBT 3.x 只能在 Scala 3.0.x 或 Scala 3.1.x 上运行
最后两行是作者mr-git的猜测。
但是,如果实际上有Scala 2.13版本,我没有找到任何其他胶水。
有人知道吗?
更新:在此处添加:https : //discuss.lightbend.com/t/when-is-there-a-sbt-version-for-2-13/5623
I could not find anything on how to ignore a Suite or a Test with ZIO Test.
Whether in an example nor in the documentation (https://zio.dev/docs/usecases/usecases_testing)
There is an ignored in the test package object:
/**
* Creates an ignored test result.
*/
final val ignored: ZTest[Any, Nothing, Nothing] =
ZIO.succeed(TestSuccess.Ignored)
Run Code Online (Sandbox Code Playgroud)
But how can it be used in your code?
I tried different things, but with no success.
我有一个核心库,将其配置设置为系统属性。
mail.from = mail@example.com
Run Code Online (Sandbox Code Playgroud)
我想用它们覆盖我的配置,例如:
my.mail.from = "me@example.com"
my.mail.from = ${?mail.from}
Run Code Online (Sandbox Code Playgroud)
这适用于单元测试。在我的 Play (2.6) 应用程序中,它没有。
PropertiesConfiguration.init() // this inits the system properties
info("mail.from: " + sys.props.get("mail.from")) // >> 'mail@example.com' as expected
val config = ConfigFactory.load()
info("my.mail.from: " + config.getString("my.mail.from")) // >> 'me@example.com' instead of 'mail@example.com'
Run Code Online (Sandbox Code Playgroud)
这是不可能的还是我错过了什么?
我在一台新机器上导入了一个现有的工作项目,并试图让它编译。我收到以下错误
unresolved dependency: com.atlassian.jwt#jwt-core;1.6.1: not found
Run Code Online (Sandbox Code Playgroud)
我读到这个库用于Silhouette. 我Silhouette在我的项目中使用。
我的plugins.sbt是
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
resolvers += "Atlassian Releases" at "https://maven.atlassian.com/public/"
Run Code Online (Sandbox Code Playgroud)
我注意到 IntelliJ 试图在以下位置找到该库,但它无法找到。
==== local: tried
[warn] C:\Users\UserName\.ivy2\local\com.atlassian.jwt\jwt-core\1.6.1\ivys\ivy.xml
[warn] ==== public: tried
[warn] https://repo1.maven.org/maven2/com/atlassian/jwt/jwt-core/1.6.1/jwt-core-1.6.1.pom
[warn] ==== local-preloaded-ivy: tried
[warn] C:\Users\UserName\AppData\Local\Temp\sbt-global-pluginstub\preloaded\com.atlassian.jwt\jwt-core\1.6.1\ivys\ivy.xml
[warn] ==== local-preloaded: tried
[warn] file:/C:/Users/UserName/AppData/Local/Temp/sbt-global-pluginstub/preloaded/com/atlassian/jwt/jwt-core/1.6.1/jwt-core-1.6.1.pom
Run Code Online (Sandbox Code Playgroud) 我对这两个功能之间的区别有疑问:
def getFunction(checkpointPath: String,
sparkConf: SparkConf,
creatingFunc: () => StreamingContext): StreamingContext = {
function body
}
def getFunction(checkpointPath: String,
sparkConf: SparkConf,
creatingFunc: => StreamingContext): StreamingContext = {
function body
}
Run Code Online (Sandbox Code Playgroud)
因此,按名称调用的参数是相同的:
creatingFunc: => StreamingContext
Run Code Online (Sandbox Code Playgroud)
和
creatingFunc: () => StreamingContext
Run Code Online (Sandbox Code Playgroud)
或者没有 ?
我在Ubuntu 18.04上安装并sbt使用常规安装apt-get install sbt,到目前为止,它可用于所有项目,不知道它获得的版本。然后,我克隆了项目https://github.com/aishfenton/Argus,无论我做什么sbt都会崩溃。
我下载并安装了最新版本,sbt但仍然遇到以下崩溃:
skywalker@Zeus:~/code$ java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
skywalker@Zeus:~/code$ which sbt
/opt/dev/sbt/1.2.8/bin/sbt
skywalker@Zeus:~/code$ git clone https://github.com/aishfenton/Argus
Cloning into 'Argus'...
remote: Enumerating objects: 622, done.
remote: Total 622 (delta 0), reused 0 (delta 0), pack-reused 622
Receiving objects: 100% (622/622), 140.72 KiB | 602.00 KiB/s, done.
Resolving deltas: 100% (225/225), done.
skywalker@Zeus:~/code$ cd Argus
skywalker@Zeus:~/code/Argus$ …Run Code Online (Sandbox Code Playgroud) 我正在尝试ZIO。
我不明白为什么Live将添加为Trait,然后object提供,例如:
object Live extends Live
Run Code Online (Sandbox Code Playgroud)
例如,可以在不同的地方找到此模式zio.console.Console。
是否有原因,或者在某些情况下有意义?
有Futures一种简单的方法可以转换Seq[Future]为Future[Seq]:
Future.sequence(seqOfFutures)
我找不到与 . 类似的东西Try。
它适用于foldLeft但我真正喜欢的会有类似的东西Try.sequence(seqOfTry)。
不提供这样的功能有什么原因吗?
如何正确完成此操作?
语义:
成功的价值观列表:Success(Seq(1,2,3,4))
失败有两种可能:
拳头失败Failure并返回。这是通过这个问题处理的:listtryt-to-trylistt-in-scala
收集所有内容Failures并返回“复合”失败。
对于“复合”故障还有解决方案吗?
我需要从一个配置文件中读取并将配置映射到一个案例类。如果我有一个如下表,它工作正常
配置
mapping {
target {
oracle = {
type = "oracle"
schema = "orcl"
tableName = "my_table"
query = "select key from my_table where dob='2020-01-01'
}
}
Run Code Online (Sandbox Code Playgroud)
SCALA 代码片段
val targetConfig:Map[String,QueryEngine] = config.getObject("mapping.target")
.entrySet()
.asScala
.foldLeft(Map.empty[String , QueryEngine]) { case ( acc , entry ) =>
val target = entry.getKey
val targetConfig = entry.getValue match {
case validElement if validElement.valueType() == ConfigValueType.OBJECT => validElement.asInstanceOf[ConfigObject].toConfig
case invalidElement => sys.error("illegal syntax at $invalidElement")
}
targetConfig.getString("type") match {
case "oracle" => acc …Run Code Online (Sandbox Code Playgroud) 我有以下内容case class:
case class Example[T](
obj: Option[T] | T = None,
)
Run Code Online (Sandbox Code Playgroud)
Example(myObject)这使我能够像而不是那样构建它Example(Some(myObject))。
要使用 obj,我需要将其标准化为Option[T]:
lazy val maybeIn = obj match
case o: Option[T] => o
case o: T => Some(o)
Run Code Online (Sandbox Code Playgroud)
the type test for Option[T] cannot be checked at runtime
我尝试过TypeTest,但也收到警告 - 或者我发现的解决方案看起来非常复杂 - 请参阅/sf/answers/4872566401/
在Scala 3中是否有更好的方法来实现这种模式?