score:2
what happens is type ascription, and here, it is not much.
the code works just as if you had written
def p(x: int): unit = root.p(x)
as you intended. when you write x: int
in the call (not in the declaration, where it has a completely different meaning) or more generally expr: type
, it has the same value as expr
, but it tells the compiler to check that the expr is of the given type (this is a check made a compile type, sort of an upcast, not at all a runtime check such as asinstanceof[...]
) and to treat it has having that type. here, x is indeed an int and it is already treated as an int by the compiler, so the ascription changes nothing.
besides documenting a non obvious type somewhere in the code, type ascription may be used to select between overloaded method:
def f(a: any) ...
def f(i: int) ...
f(3) // calls f(i: int)
f(3: any) // calls f(a: any)
note that in the second call, with the ascription, the compiler knows that 3
is of type any
, less precise than int
, but still true. that would be an error otherwise, this is not a cast. but the ascription makes it call the other version of f
.
you can have a look at that answer for more details: https://stackoverflow.com/a/2087356/754787
score:0
are you delegating the implementation of b.p
to a.p
?
i don't see any unusual except for root.p(x:int)
, you can save typing by root.p(x)
.
trait is a way of code mixin, i think the easiest way is:
trait a {
def p(x: int) = println("a" + x)
}
case class b extends anyref with a
val b = b()
b.p(123)
Source: stackoverflow.com
Related Query
- Why does this compile and what is happening?
- Why does this list-of-futures to future-of-list transformation compile and work?
- Why does this code typecheck in Scala 2.11 and what can I do about it?
- Why does this snippet with pattern matching and higher kinded types no longer compile in Scala 2.12?
- Why does this compile under Java 7 but not under Java 8?
- Why does this Scala function compile when the argument does not conform to the type constraint?
- Why List[T] and not List[Int]? What does T mean?
- Why does flatten on nested Iterator not compile and why do I need type ascription?
- Why does this two checks for null and empty returns different results?
- Why does this type alias not compile (Scala)
- Why does SonarQube find this issue (<static initializer for >() uses a Side Effect Constructor) with case and object class files?
- Scala: What does zipped produce and why can it be mapped as a pair?
- @tailrec why does this method not compile with 'contains a recursive call not in tail position'?
- Why can Scala and sbt compile this code?
- Why does converting a Seq to a Set and then calling map on same line not compile
- Why does Scala fail to compile this function as tail recursive?
- In simplest term, what is currying and why should this approach be favored over traditional programming paradigm?
- Why does this function overload produce a compile error in Scala?
- Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?
- What does => and () => mean in Scala
- What are Scala continuations and why use them?
- Why does Scala provide both multiple parameters lists and multiple parameters per list?
- Why does a small change to this Scala code make such a huge difference to performance?
- What is Scala's Simple Build Tool (sbt) and why is it used?
- Why does sbt compile fail with StackOverflowError?
- What does Predef.locally do, and how is it different from Predef.identity
- Forward References - why does this code compile?
- Why does Scalaz use complex symbols and no in-code documentation?
- What does the @elidable annotation do in Scala, and when should I use it?
- "functions are first class values" what does this exactly mean?
More Query from same tag
- Value * is not a member of type parameter Int
- Play Project not compiling correctly in Intellij - routes_routing.scala
- Applied filter to string, not to characters
- How to use Numeric[T] to represent zero of any numeric type
- org.scalacheck and org.scalatest not found after sbt->compile
- Unable to parse JSON with GSON in Scala
- Implicit expansion with dependant types
- How am I *supposed* to use a scala.collection.immutable.Queue?
- Convert into a For Comprehension in Scala
- Spark integration with spring boot web starter
- How do I create an ordered list of [T, Int] in descending order for any orderable T?
- Readable FoldRight via FoldLeft in Scala
- How can I make more partitions in Spark without causing a shuffle
- Avro Field name beginning with Numeric/Integer
- How to convert a Array of Map object with values to a single Map
- Spark Dataframe: Representing Schema of MapType with non homogeneous data types in StructType values
- type mismatch compiler error when concatenating two maps using getOrElse
- Can't search in Lucene 6.2 using Scala
- After upgrade to play 2.5 : Error injecting constructor
- Generic Covariant class with upper type boundaries drawbacks
- Is Apache Spark appropriate for generic non-analytics OLTP tasks.
- Random dice rolls aren't random
- How to send iterables between actors or from an actor to a Future?
- Scala, Lift | SHtml.select - how to put list from database
- Scala Run Command and Capture Output
- implicits for objects in Scala
- Scala XML: test for node existence and value
- Maven: build Scala code and Java code together into a Fat JAR
- Why is sbt 0.13.9 reporting that it's 0.13.8?
- gradle publishing multi module project to AWS Artifact only publishes META-INF