Scala assumes wrong type when using foldLeft


I am trying to create a cross product function in Scala, where k is the number of times I build the cross product.

val l = List(List(1), List(2), List(3)) (1 to k).foldLeft[List[List[Int]]](l) { (acc: List[List[Int]], _) => for (x <- acc; y <- l) yield x ::: l }

However, this code does not compile:

test.scala:9: error: type mismatch; found : List[List[Any]] required: List[List[Int]] for (x <- acc; y <- l) ^

Why does it ever think I have a List[Any]'s there? Clearly everything I am dealing with is Lists of Ints.


Your for comprehension is effectively yielding List[List[Int or List[Int]]] hence the inferred type is List[List[Any]]. Here's an example from the repl:

scala> val l = List(List(1), List(2), List(3)) l: List[List[Int]] = List(List(1), List(2), List(3)) val x = for { | x <- l | y <- l | } yield x ::: l x: List[List[Any]] = List(List(1, List(1), List(2), List(3)), List(1, List(1), List(2), List(3)), List(1, List(1), List(2), List(3)), List(2, List(1), List(2), List(3)), List(2, List(1), List(2), List(3)), List(2, List(1), List(2), List(3)), List(3, List(1), List(2), List(3)), List(3, List(1), List(2), List(3)), List(3, List(1), List(2), List(3)))


  • Scala: How to sum a list of tuples
  • How to fix the exception: java.lang.OutOfMemoryError: GC overhead limit exceeded even though enough
  • Multiple-columns operations in Spark
  • Manipulating a dataframe within a Spark UDF
  • F#: Recursive Functions: Concatenating the common values between two lists
  • Write a function that calculate the sum of integers in a list in Erlang
  • How to access list of email accounts with cPanel API?
  • Optional parameter in UriTemplate in WCF
  • Cassandra: What is a subcolumn
  • How to change placeholder text in an autocomplete activity of android google place?
  • How to convert integer to string and get length of string
  • Magento get URL before current
  • Group list of tuples by item
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • How can I extract results of aggregate queries in slick?
  • jQuery .attr() and value
  • MongoDB in PHP using aggregate to group by _id is null not working
  • XCode can't find symbols for a specific iOS library/framework project
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Add sale price programmatically to product variations
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?