报告习惯用语似乎是一个报告由一个项目列表组成,还有一些额外的数据(参数).有没有办法在报告中包含几个不相关的列表,或者这是否会违背成语,以至于应该使用不同的工具来生成输出?
例如,假设您有一个居住在建筑物中的人员列表,其中包含姓名,电话号码等.该列表将是主要的数据源.此外,在同一报告中,您希望显示有关该建筑物的各种其他信息,例如地址,楼层数等.此信息中的项目数可能因建筑物而异,因此您不能简单地将其放入静态参数中,而是需要地图或列表.这当然是一个受人尊敬的例子,但应该用来说明问题.
简而言之:您可以在报告中使用多个不相关的列表吗?
我正在尝试运行以下火箭发射应用程序:
object HelloWorld {
def main(args: Array[String]) {
println("Hello World!")
}
}
Run Code Online (Sandbox Code Playgroud)
直接从java这样:
java -cp scala-library.jar HelloWorld
Run Code Online (Sandbox Code Playgroud)
(显然在用scala编译之后)
但是得到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
at java.net.URLClassLoader$1.run(Unknown Source)
(...)
Could not find the main class: HelloWorld. Program will exit.
Run Code Online (Sandbox Code Playgroud)
我是否已经监督了我需要做的任何微不足道的工作?
在对象上定义递归方法:
object Recursive {
def recurse(maxDepth: Int = 10): Unit = {
if (maxDepth == 0) throw new Exception
recurse(maxDepth - 1)
}
}
Run Code Online (Sandbox Code Playgroud)
得到:
scala> Recursive.recurse(10)
java.lang.Exception
at Recursive$.recurse(<console>:7)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$17.apply(Interpreter.scala:988)
at scala.util.control.Exception$Catch.apply(Exception.scal...
Run Code Online (Sandbox Code Playgroud)
但是在课堂上定义它:
class Recursive {
def recurse(maxDepth: Int = 10): Unit = {
if (maxDepth == 0) throw new Exception
recurse(maxDepth - 1)
}
}
Run Code Online (Sandbox Code Playgroud)
得到: …
假设你有一个Person类,并通过扩展例如ArrayBuffer为它创建一个集合类:
class Persons extends ArrayBuffer[Person] {
// methods operation on the collection
}
Run Code Online (Sandbox Code Playgroud)
现在,使用ArrayBuffer,可以在随播对象上使用apply()方法创建一个集合,例如:
ArrayBuffer(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
您希望能够对人员做同样的事情,例如:
Persons(new Person("John", 32), new Person("Bob", 43))
Run Code Online (Sandbox Code Playgroud)
我的第一个直觉是扩展ArrayBuffer伴侣对象并免费获取apply()方法.但似乎你无法扩展对象.(我不太清楚为什么.)
接下来的想法是使用apply()方法创建一个Persons对象,该方法调用ArrayBuffer的apply方法:
object Persons {
def apply(ps: Person*) = ArrayBuffer(ps: _*)
}
Run Code Online (Sandbox Code Playgroud)
但是,这会返回一个ArrayBuffer [Person]而不是Persons.
在scaladoc和ArrayBuffer的源代码中进行了一些挖掘之后,我提出了以下内容,我认为这将使Persons对象从GenericCompanion继承apply():
编辑:
object Persons extends SeqFactory[ArrayBuffer] {
def fromArrayBuffer(ps: ArrayBuffer[Person]) = {
val persons = new Persons
persons appendAll ps
persons
}
def newBuilder[Person]: Builder[Person, Persons] = new ArrayBuffer[Person] mapResult fromArrayBuffer
}
Run Code Online (Sandbox Code Playgroud)
但是,它给出以下错误消息:
<console>:24: error: type mismatch;
found : (scala.collection.mutable.ArrayBuffer[Person]) => Persons …
Run Code Online (Sandbox Code Playgroud) 鉴于:
trait Foo
trait Bar { this: Foo => }
trait NoBar { this: Foo => }
Run Code Online (Sandbox Code Playgroud)
有没有办法可以欺骗类型系统禁止:
new Foo with Bar with NoBar {}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个Web应用程序,由于数据库访问速度慢,并非页面中的所有内容都是立即加载的,而是在用户在可选择进行选择后单击按钮时动态加载.
这很好用.但是,在动态加载内容后,如果我导航到不同的网页,然后导航回来,在Internet Explorer中,加载的内容将消失,即页面将恢复到最初检索的页面.然而,在Firefox(以及Opera)中,加载的内容仍然存在,即页面看起来就像我导航之前那样.
在我的情况下,Firefox的行为是理想的行为,因为用户会定期导航到子页面并返回主页面.因此,我的问题是,有什么方法可以强制Internet Explorer展示这种行为,还是有任何可能的解决方法来获得所需的结果?
考虑以下(简化)示例:
abstract class Bar[T] {
val f: PartialFunction[T, T]
val default: PartialFunction[T, T] = { case x => x }
val chained = f orElse default
}
class Foo extends Bar[Int] {
val f: PartialFunction[Int, Int] = { case 1 => 2 }
}
Run Code Online (Sandbox Code Playgroud)
看着它崩溃:
scala> val foo = new Foo
java.lang.NullPointerException
at Bar.<init>(<console>:8)
at Foo.<init>(<console>:6)
at .<init>(<console>:7)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:9)
at RequestResult$.<clinit>(<console>)
at RequestResult$scala_repl_result(<console>)
....
Run Code Online (Sandbox Code Playgroud)
但是,如果我们chained
输入具体的类:
abstract class Bar[T] {
val f: PartialFunction[T, T]
val default: …
Run Code Online (Sandbox Code Playgroud) 我似乎将隐式转换与通用隐式参数结合起来不起作用,如下面的示例2b所示:
object Test {
case class Foo(i: Int)
case class Bar(i: Int)
case class Zip(i: Int)
object Foo {
// 1)
implicit def toBar(foo: Foo)(implicit g: Int => Bar): Bar = g(foo.i)
// 2)
implicit def toT[T](foo: Foo)(implicit g: Int => T): T = g(foo.i)
}
// 1)
implicit val b = (i: Int) => Bar(i)
val bar: Bar = Foo(3)
// 2a)
implicit val z = (i: Int) => Zip(i)
val zip: Zip = Foo.toT(Foo(3))
// 2b)
val zip2: …
Run Code Online (Sandbox Code Playgroud) 我正在编写一个实现(包装)java.util.List的Scala类,即:
class MyList(backingList: java.util.List) extends java.util.List
Run Code Online (Sandbox Code Playgroud)
后者有一个带有Java签名的方法,如下所示:
<T> T[] toArray(T[] a)
Run Code Online (Sandbox Code Playgroud)
我天真地写道:
def toArray[T](a: Array[T]) = backingList toArray a
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨在backingList上调用toArray需要一个Array[? with java.lang.Object]
.
我想我已经尝试了各种可能的变化Array[_ >: T with Object]
(比如编译器所建议的),但没有运气.有什么建议?
您似乎必须将XML文本中的属性值显式指定为字符串:
scala> val foo = 3
foo: Int = 3
scala> <bar id={ foo } />
<console>:10: error: overloaded method constructor UnprefixedAttribute with alternatives:
(key: String,value: Option[Seq[scala.xml.Node]],next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: String,next: scala.xml.MetaData)scala.xml.UnprefixedAttribute <and>
(key: String,value: Seq[scala.xml.Node],next1: scala.xml.MetaData)scala.xml.UnprefixedAttribute
cannot be applied to (java.lang.String, Any, scala.xml.MetaData)
<bar id={ foo } />
^
scala> <bar id={ foo.toString } />
res16: scala.xml.Elem = <bar id="3"></bar>
Run Code Online (Sandbox Code Playgroud)
构造函数不能简单地接受Any参数并在其上调用toString吗?由于以下工作,
scala> <bar>{ foo }</bar>
res21: scala.xml.Elem = <bar>3</bar>
Run Code Online (Sandbox Code Playgroud)
这个API有一点点不对称.这有什么特别的原因吗?