通过搜索,我理解将数组转换为List的方式(或方法)是这样的:
val l = Array(1, 2, 3).toList
Run Code Online (Sandbox Code Playgroud)
但是我不仅无法在Array的API文档中找到toList方法,我无法在任何似乎是Array的祖先或继承特征的东西中找到它.
使用较新的2.9 API文档,我看到toList存在于以下内容中:
ImmutableMapAdaptor ImmutableSetAdaptor IntMap List ListBuffer LongMap
MutableList Option ParIterableLike PriorityQueue Stack StackProxy
StreamIterator SynchronizedSet SynchronizedStack TraversableForwarder
TraversableOnce TraversableOnceMethods TraversableProxyLike
Run Code Online (Sandbox Code Playgroud)
但是我无法理解toList如何从其中一个获取成为Array的一部分.有谁能解释一下?
考虑这个模糊类型推断的简单示例:
#! /usr/bin/env stack
{- stack runghc -}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
main :: IO ()
main = do
-- This will fail to compile without additional type info
-- let w = read "22"
-- print w
-- My go-to for this is type signatures in the expressions
let x = read "33" :: Integer
print x
-- Another possibility is ScopedtypeVariables
let y :: Integer = read "44"
print y
-- How does TypeApplications …Run Code Online (Sandbox Code Playgroud) 我一直在使用Scala中的包装类和导入包时遇到困惑.让我从一对简单的源文件开始:
package a
// Which of these imports should be used? They both seem to work.
//import a.b._
import b._
class A {
val fieldB = new B
}
Run Code Online (Sandbox Code Playgroud)
package a.b
class B
Run Code Online (Sandbox Code Playgroud)
使用scalac进行编译时无需投诉A.scala中的任何导入
尝试在REPL中加载这些文件的方式不同:
$ scala
Welcome to Scala version 2.8.0.r0-b20100714201327 (Java HotSpot(TM) Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :l a/b/B.scala
Loading a/b/B.scala...
<console>:1: error: illegal start of definition
package a.b
^
defined class B …Run Code Online (Sandbox Code Playgroud)