在Scala中,从类中选择类型的语法与从类中选择其他任何类型的语法不同.前者使用散列作为选择运算符而不是点.这是为什么?
示例:如果我们有这样的类......
class Example {
type Foo = String
}
Run Code Online (Sandbox Code Playgroud)
为什么我们从这个类中选择类型......
val example:Example#Foo = "1"
Run Code Online (Sandbox Code Playgroud)
而不是像这样?
val example:Example.Foo = "1"
Run Code Online (Sandbox Code Playgroud) 我一直在研究路径依赖类型.我能找到的最好的描述是:
如果L是类型标签,则xL和yL是相同类型iff x和y可以显示为引用同一对象.
这有时不是人们所期望的子类型行为.我希望如果上面例子中的L确实相同,那么就足以使xL和yL变得一致.
Scala是这样设计的,有什么特别的原因吗?
Java代码
package lambda_cache_example_java;
interface Semigroup1<A> {
public A append(A a1, A a2);
}
interface Semigroup2<A> {
public A append(A a1, A a2);
public interface Foo{}
public class Bar{}
}
class Main {
static Semigroup1<Integer> intSemigroup1() {
return (a1, a2) -> a1 + a2;
}
static Semigroup2<Integer> intSemigroup2() {
return (a1, a2) -> a1 + a2;
}
public static void main(String[] args) {
Semigroup1<Integer> x1 = intSemigroup1();
Semigroup1<Integer> x2 = intSemigroup1();
System.out.println(x1);
System.out.println(x2);
System.out.println(x1 == x2); // same instance
Semigroup2<Integer> y1 …Run Code Online (Sandbox Code Playgroud) 我有一个顶级特征,包含许多类和特征,如:
trait Trees { self: Types =>
trait Tree
trait IdentifiedTree extends Tree
trait Empty extends Tree
/** The factory for [[TypeUse]] instances */
trait TypeUse extends Tree
/** AST tree to represent erroneous trees */
object BadTree extends IdentifiedTree
/** AST tree for empty statements and trees */
val Empty: Empty = new Empty {}
}
trait Types
Run Code Online (Sandbox Code Playgroud)
当我为它生成文档时,使用scaladoc我可以使用[[CLASS_NAME]]链接到内部类,但是scaladoc无法在签名和扩展中为树创建链接.
我使用sbt生成scaladoc,我使用以下标志:
scalacOptions in (Compile, doc) ++= Seq("-groups", "-implicits",
"-diagrams", "-no-prefixes", "-author", "-explaintypes",
"-language:implicitConversions,higherKinds")
Run Code Online (Sandbox Code Playgroud)
为了给你一个更好的主意,上面定义的api如下(请注意缺少的链接):

你能告诉我我做错了吗?
scala ×4
cake-pattern ×1
java-8 ×1
jvm ×1
lambda ×1
sbt ×1
scaladoc ×1
syntax ×1
type-systems ×1