我知道这个问题以不同的方式出现过很多次.但我现在还不清楚.有没有办法实现以下目标.
def foo(a:Int, b:Int) = {}
foo(a,b) //right way to invoke foo
foo(getParams) // is there a way to get this working without explicitly unpacking the tuple??
def getParams = {
//Some calculations
(a,b) //where a & b are Int
}
Run Code Online (Sandbox Code Playgroud) 任何人都可以<:<在scala中提供有关运算符的一些细节.我认为:
if(apple <:< fruit) //checks if apple is a subclass of fruit.
Run Code Online (Sandbox Code Playgroud)
还有其他解释吗?我在scala源文件中看到了很多定义.
def foo(x:Int, f:Unit=>Int) = println(f())
foo(2, {Unit => 3+4}
//case1
def loop:Int = 7
foo(2, loop) //does not compile
changing loop to
//case 2
def loop():Int = 7
foo(2, loop) // does not compile
changing loop to
//case 3
def loop(x:Unit): Int = 7 //changing according to Don's Comments
foo(2,loop) // compiles and works fine
Run Code Online (Sandbox Code Playgroud)
案件1和案例2也不行吗?他们为什么不工作?
将foo定义为
def foo(x:Int, y:()=>Int)
Run Code Online (Sandbox Code Playgroud)
案例2有效但案例1无效.
他们都不应该工作,无论如何定义功能.
//也是我认为()=>在foo中的Int是一个糟糕的样式,y:=> Int不起作用,评论??
I am using mockito and trying to mock a scala object.
object Sample { }
//test
class SomeTest extends Specification with ScalaTest with Mockito {
"mocking should succeed" in {
val mockedSample = mock[Sample]
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我两个编译错误.
error: Not found type Sample
error: could not find implicit value for parameter m:
scala.reflect.ClassManifest[<error>]
Run Code Online (Sandbox Code Playgroud)
如果我将Sample从对象更改为类,则可以正常工作.有可能用mockito模拟scala对象吗?如果有,怎么样?
假设我有一个类似的案例类
case class Person(fname:String, lname:String, nickName:Option[String] = None)
Run Code Online (Sandbox Code Playgroud)
在创建像Person("John","Doe")这样的实例时,我希望将nickName自动分配给fname,如果没有给出.例如:
val p1 = Person("John", "Doe")
p1.nickName.get == "John"
val p2 = Person("Jane", "Doe", "Joe")
p2.nickName.get == "Joe"
Run Code Online (Sandbox Code Playgroud)
如何实现从另一个字段自动分配一个字段?
在repl中尝试以下解决方案.我认为这与repl有关
scala> case class Person(fname: String, lname:String, nickName:Option[String])
defined class Person
scala> object Person { def apply(fname:String, lname:String):Person = {Person(fname, lname, Some(fname))}}
console:9: error: too many arguments for method apply: (fname: String, lname: String)Person in object Person
object Person { def apply(fname:String, lname:String):Person = {Person(fname, lname, Some(fname))}}
Run Code Online (Sandbox Code Playgroud) 我使用以下代码生成scala AST:
val setting = new Settings(error)
val reporter = new ConsoleReporter(setting, in, out) {
override def displayPrompt = ()
}
val compiler = new Global(setting, reporter) with ASTExtractor{
override def onlyPresentation = true
}
//setting.PhasesSetting("parser", "parserPhase")
val run = new compiler.Run
val sourceFiles:List[String] = List("Test.scala")
run.compile(sourceFiles.toList)
Run Code Online (Sandbox Code Playgroud)
我想这是用于在代码中运行编译器并生成AST的标准代码.上面的代码适用于Test.scala中的任何有效scala代码,直到现在.当我在Test.scala中使用递归函数时,就像
def xMethod(x:Int):Int = if(x == 0)-1 else xMethod(x-1)
它给了我一个java.lang.NullPointerException.堆栈跟踪的前几行看起来像这样
at scala.tools.nsc.typechecker.Typers$Typer.checkNoDoubleDefsAndAddSynthetics$1(Typers.scala:2170)
at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:2196)
at scala.tools.nsc.typechecker.Typers$Typer.typedBlock(Typers.scala:1951)
at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:3815)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4124)
at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4177)
at scala.tools.nsc.transform.TailCalls$TailCallElimination.transform(TailCalls.scala:199)
Run Code Online (Sandbox Code Playgroud)
该代码适用于类似的方法
def aMethod(c:Int):Int = { bMethod(c) }
def bMethod(x:Int):Int …Run Code Online (Sandbox Code Playgroud) 有什么方法可以在scala中实现以下功能
with new Car() {
examineColor
bargain(300)
buy
}
Run Code Online (Sandbox Code Playgroud)
代替
val c = new Car()
c.examineColor
c.bargain(300)
c.buy
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的单端点 API 提供 Swagger/Open Api 文档。
我的单一端点看起来像
帖子正文决定逻辑路径和响应模式
Body1: {"jsonClass": "AnimalsRankedByLifeSpan"}
Response1: schema-1
Body2: {"jsonClass": "AnimalsInRegion", "region":"Africa", "type":"lions"}
Response2: schema-2
文档的期望:每个 jsonClass 都被列为 swagger(或任何其他)中的单独调用,我可以使用规范来获取所有受支持的 jsonClass。
看起来不像,swagger支持这种设计。如果是的话,请给我举一些例子。
是否有任何其他 Api 文档框架可以用来为支持的每种 jsonClass 提供请求-响应文档?
def mainCaller() = {
val name = "xyz"
someList.foreach { u:Map => foo(name, u) }
}
def foo(name:String)(map:Map): Unit = {
//match case....
//recursive call to foo in each case where name remains same, but map changes
}
Run Code Online (Sandbox Code Playgroud)
我如何编写foo作为部分应用函数,我不必在每次递归调用中传递名称而只是调用foo(map1)?