Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6390669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:34:33+00:00 2026-05-25T03:34:33+00:00

Take a look at this: scala> class Container(val rows: Iterable[Iterable[Option[Any]]]) {} defined class Container

  • 0

Take a look at this:

scala> class Container(val rows: Iterable[Iterable[Option[Any]]]) {} 
defined class Container

scala> val row1 = Array(Some("test"),Some(1234))
row1: Array[Some[Any]] = Array(Some(test), Some(1234))

scala> val row2 = Array(Some("test2"), Some(12345))
row2: Array[Some[Any]] = Array(Some(test2), Some(12345))

scala> val listtest = List(row1, row2)
listtest: List[Array[Some[Any]]] = List(Array(Some(test), Some(1234)), Array(Some(test2), Some(12345)))

scala> val test = new Container(listtest)
<console>:11: error: type mismatch;
 found   : List[Array[Some[Any]]]
 required: Iterable[Iterable[Option[Any]]]
       val test = new Container(listtest)
                                ^

scala> val test = new Container(List(row1,row2))
test: Container= Container@600a08

How come defining the Container the second way works, but the first one doesn’t? Aren’t the types the same?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:34:34+00:00Added an answer on May 25, 2026 at 3:34 am

    This is no bug. Array is not covariant. B being a subtype of B does not make Array[B] a subtype of Array[A]. This is contrary to java, where B[] is a subtype of A[], which is unsound:

    A[] b = new B[1];
    b[0] = new (A);
    -> ArrayStoreException
    

    So your Array[Some[Any]] is not an Array[Option[Any]]. You must make sure you have an Array[Option], which you may do with

    val row2 = Array[Option[Any]](Some(test2), Some(12345))
    

    You can also use a type abscription on one of the item:

    val row2 = Array(Some(test2): Option[String], Some(12345))
    

    Or if you know your values are non-null,

    val row2 = Array(Option(test2), Option(12345))
    

    (It’s enough to do that on one of the value)

    List, on the other hand is covariant, which is why it works.

    It is actually unfortunate that the more precise type Some is inferred, it is quite uncommon that you want the type of something to be known as Some (or None) rather than Option.

    Edit Sorry, looks like I completely missed the point, about why there are different. Array is not covariant, but Iterable is. So it would seems that Array[B] while not being an Array[A], should be an Iterable[A], then everything should work. It would be so if Array was a subtype of Iterable. It is not, it comes with the JVM, cannot be made to extend Iterable. What there is is an implicit conversion to WrappedArray, which is an Iterable.

    When you write val l = List(row1, row2), it has no reason to apply this conversion. It types list as precisely as it can. Then the fact that List is covariant (a List[B] is a List[A] if B is an A) will not kick in when we have not B is an A, but B has an implicit conversion to A.

    On the other hand, when you write val l: List[Iterable[A]] = List(x,y) then the List(…) function expects Iterable[A] arguments, and at this point it looks for implicit conversions.

    Still not a bug, but trickier than I thought. Maybe you could do a

    class Container[T <% Iterable[Option[Any]]](val rows: Iterable[T])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

take a look at this example code: public class Comment { private Comment() {
Take a look at this code: public class Test { public static void main(String...
Take a look at this table: <table cellpadding=0 cellspacing=0 class=order_form> <tr> <th>Amount</th> <th>Desc</th> <th>Price</th>
Take a look at this. Note that class B overrides A 's a() method.
Take a look at this code: $GET = array(); $key = 'one=1'; $rule =
Take a look at this page in FireFox. Feel free to navigate to any
take a look of this: http://jsfiddle.net/PFQke/ I want to add some icons to that
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
Take a look at this snazy plugin: http://remysharp.com/2007/12/28/jquery-tag-suggestion/ ** it's real small Source: http://remysharp.com/downloads/tag.js

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.