score:0
First of all I would like to put your attention of the fact that having a generic parameter that cannot be inferred is weird, because that does not contain any information about the data contained in the class. If it would, then it would be inferable.
When you do not assign the return type explicitely, a parameter can be inferred. Since infix operator (unless they end with :) in Scala are left associative, in this statement:
val c:Condition[SubSub] = new Condition[Parent] and new Condition[SubSub] and
new Condition[Sub] and
new Condition[Parent]
the compiler acts like this :
((new Condition[Parent] and new Condition[SubSub]) and
new Condition[Sub]) and
new Condition[Parent])
So you end up in the following situation:
- The first and between Condition[Parent] and Condition[SubSub] returns a type that can't be inferred and that you didn't specify, so it is UnknownType1
- The second and the same: it is an and between UnknownType1 and a Condition[Sub], returning UnknownType2
- The third one takes an UnknownType2 and Condition[Parent]. Here the compiler can't check the constraint hold.
Please note that, in the case of the three operands and:
val b: Condition[SubSub] =
new Condition[Parent] and
new Condition[SubSub] and
new Condition[Sub]
The compiler used the type of b to infer the type of the intermediate result (Parent and SubSub) and could check the bounds.
In the failing case, the compiler would need to infer twice the result, and it is not able to do that.
To get back to the initial comment, please also note that in the simple case, if you do not specify the result, the compiler fails as well, and this is normally a sign of poor design of your classes
val e = new Condition[Parent] and new Condition[SubSub]
Source: stackoverflow.com
Related Query
- Scala type inference and getting the more specific of two type parameters as method result
- What are the differences between the type inference of Scala and C++11?
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- What Scala 3 syntax can match on a Type and its Type parameters in the context of a macro?
- Scala - how come using a super-type with two generic parameters cause the scala type checker to treat the child-type differently?
- Error: type inference was unable to figure out the type. Getting error on upgrading scala version
- Scala how to pass in child class and then return the same child class type while getting it generic?
- Creating UDF in spark Scala for getting specific format in the single column rather than three different columns in SPARK DATAFRAME AND SQL
- Creating UDF in spark Scala for getting specific format in the single column rather than three different columns in SPARK DATAFRAME AND SQL
- What are the differences and similarities of Scala and Haskell type systems?
- What is the difference between a class and a type in Scala (and Java)?
- Different type inference for `def` and `val` in Scala
- Instantiate a Scala class from Java, and use the default parameters of the constructor
- Why does Scala choose the type 'Product' for 'for' expressions involving Either and value definitions
- Get the specific simple name of a generic type in Scala
- Getting the string representation of a type at runtime in Scala
- Scala, getting the type parameters of a KList as an HList
- Scala getting field and type of field of a case class
- Mixing type parameters and abstract types in scala
- Difference between type inference of method and class type parameters in pattern matching
- Scala type inference for existential types and type members
- Functor Instance for Type Constructor with Two Parameters in Scala
- Scala Pickling and type parameters
- Why is my Scala function returning type Unit and not whatever is the last line?
- two type parameters with the same name
- How can I improve Scala's type inference with type parameters that don't show up in the first parameter list?
- How to make Reflection for getting the field value by its string name and its original type
- Why can't scala infer the type of the omitted parameters in partial application?
- Subtyping and type parameters in Scala
- Scala collections: transform content and type of the collection in one pass
More Query from same tag
- Unspecified value parameter rconv
- Simplify With Column in Spark
- Handling auto inc columns in Oracle via slick
- Left join on two DataFrames giving error cannot be applied to (org.apache.spark.sql.Dataset, org.apache.spark.sql.Column, String)
- NullPointerException in ShardCoordinator$LeastShardAllocationStrategy when updated to Akka 2.6.10 from Akka-2.6.9
- Regex causes StackOverflowError
- Forwarding (Downloading/Uploading) Large File via Akka HTTP / Akka Streams
- Why is this simple Spark program not utlizing multiple cores?
- Alternative way of implementing a groupBy method in Scala?
- What is the proper way to chain Akka Actor Ask calls?
- getPersistentRDDs returns Map of cached RDDs and DataFrames in Spark 2.2.0, but in Spark 2.4.7 - it returns Map of cached RDDs only
- Spray-Json - How to define field constraints
- How can I read a dataframe using spark streaming with it's schema that I specify
- How do I parse a mutableList in scala as JSON on play framework 2.0?
- Rougly (or partially) sort a list in Scala
- How do I translate Scala traits to java?
- howto verify that akka actor exists by name
- How to run transformation in parallel spark
- Scala system processes don't die after application dead
- implicit apply method in scala class
- Scala TypeTag to java.lang.reflect.Type
- Streaming data from kafka to http
- scala for if loop beginner example
- In Shapeless, given two records, how do I require that both records have the same keys and join them?
- how to get inner loop value and assign to outer variable
- DStream to Rdd in Spark-Straming
- scala pattern matching
- scala generic function `not found: type ?`
- List all classes in object using reflection
- Providing multiple instances of same implicit specialized with different type parameters