23 Matching Annotations
  1. Last 7 days
  2. Mar 2018
    1. Mixin’ traits ‘statically’

      静态的 mixin a trait 是说:这个 trait 的成员(method, field) 可以有一个 default implementation. 这样你 mixin 这个 trait 的时候,可以只提供那些没有 Implementation 的抽象函数。 当然也可以都自己实现一遍。

    2. val singingPerson = new Person with Singerperson.sing

      val singingPerson = new Person with Singer 倒是很像 组合

    1. A Scala function that’s created with val is very different than a method created with def.

      这里才是我最想看到的

    2. By all accounts, creating a Scala def method creates a standard method in a Java class

      当你用 def 创建了一个函数,你其实就是创建了一个 java class 中的方法(method)

    3. if i != 0

      这个部分:

      if i != 0
      

      会被 compiler 自动解析为 PF 的 isDefinedAt() 函数,这个函数返回 boolean,用来确定 PF 的定义域。

    4. a val function, all you’re really doing with code like this is assigning a variable name to an anonymous function

      val function like

      val f = (a:Int, b:Int) => a+b
      

      就是定义了一个 literal function, 然后给他一个名字 : f

    5. A block of code with one or more case expressions is a legal way to define an anonymous function

      用 case 构建 literal function

    1. Ordered特质更像是rich版的Comparable接口

      Ordered 青出于 Comparable 而胜于 Comparable

  3. Sep 2017
    1. echo "deb https://dl.bintray.com/sbt/debian /" | sudo tee -a /etc/apt/sources.list.d/sbt.list sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2EE0EA64E40A89B84B2DF73499E82A75642AC823 sudo apt-get update sudo apt-get install sbt
  4. Jun 2017
    1. Vmay only extenduniversaltraitsTraits in Scala extendAnyRefby default, so value classes cannot extend them. Traitswhich explicitly extendAnyare called universal, they do not extendAnyRefand valueclasses can extend them. Like value classes and for the same reasons, universal traitsneed to be ephemeral.
    2. On line 2,xneeds to be boxed because the expected type of the expression isAnyValand primitives on the JVM do not have a common supertype.
  5. Aug 2016
    1. The stringSingleton() method of the Module object appears to be indistinguishable from a first-class function value. But the appearances are deceptive. The method isn’t free-standing: we could have used this in its body and it would have referred to the Module singleton object, even after the import. And it’s not the method which is passed to map — instead a transient function value is implicitly created to invoke the stringSingleton() method (this is a process known as eta-expansion) and it’s that function-value which is passed to map.
    2. Here the method subtypePolymorphic() has no type parameters, so it’s parametrically monomorphic. Nevertheless, it can be applied to values of more than one type as long as those types stand in a subtype relationship to the fixed Base type which is specified in the method signature — in other words, this method is both parametrically monomorphic and subtype polymorphic.
  6. Dec 2015
    1. Figuring out all subclasses of a class is called Class Hierarchy Analysis, and doing static CHA in a language with dynamic code loading is equivalent to solving the Halting Problem.

      Answer to question:

      Why can't the Scala compiler give pattern matching warning for nonsealed classes/traits?

      Elaboration:

      one of the goals of Scala is separate compilation and deployment of independent modules, so the compiler simply cannot know whether or not a class is subclass in another module, because it never looks at more than one module.

      Example: ??? When I try it, it seems to work, but this may be because everything I need is already loaded in the same compilation context. A subsequent answer seems to confirm this:

      It can be done (at least for all classes known at compile time), it's just expensive. You'd completely destroy incremental compilation, because everything that contains a pattern match would effectively have to be recompiled every time any other file changed.

  7. Jul 2015
  8. proquest.safaribooksonline.com proquest.safaribooksonline.com
    1. FUNCTIONS WITH SIDE EFFECTS SHOULD USE PARENTHESES

      It would be good if there was a way to do effect-tracking, similar as in ATS, so you could enforce this rather than making it style only. But, not a huge issue either.

    1. Keeping unpure functions clearly named and organized in such a way that they can be easily identified versus pure functions is a common goal of modularizing and organizing Scala applications.
    2. Even though the value given to the match expression has the type Any, the data it is storing was created as an Int. The match expression was able to match based on the actual type of the value, not just on the type that it was given. Thus, the integer 12180, even when given as type Any, could be correctly recognized as an integer and formatted as such.

      This is interesting and I believe quite different from how ATS handles types, aside from the fact that it can't match against non-algebraic datatypes. I think this is probably easier to understand as well, since it appears to not rely on constraint solving in order to determine types.

    3. To prevent errors from disrupting your match expression, use a wildcard match-all pattern or else add enough patterns to cover all possible inputs. A wildcard pattern placed as the final pattern in a match expression will match all possible input patterns and prevent a scala.MatchError from occurring.

      In ATS you can specify 'case+' to denote an exhaustive pattern match and have it type checked (though here in Scala it would look more like 'match+'). There are other variations.

    4. | case

      These vertical bars do not seem to work for me, in either the regular input mode or ":paste" mode.

    5. An alternate form of creating a 2-sized tuple is with the relation operator (->). This is a popular shortcut for representing key-value pairs in tuples:

      Also very Perl-like.

    6. val pattern(amountText) = input

      I find this to be slightly strange syntax and non-functional; it is almost as if pattern is applied as if it were pattern-inverse.