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

  • Home
  • SEARCH
  • 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 1065075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:45:22+00:00 2026-05-16T19:45:22+00:00

When I’m writing this code, I’ve got a Compile error in Scala var s:

  • 0

When I’m writing this code, I’ve got a Compile error in Scala

var s: Stack[_ <: Number] = new Stack[Integer]; 
s.push(new Integer(1)); //compile-error: type mismatch; found :Integer required: _$bqjyh where type _$bqjyh <: Number
s.push(null); //compile-error: type mismatch; found   : Null(null) required: _$2 where type _$2 <: Objects.Vehicle

This is equivalent to covariant collection in Java due the wildcard; it exact type in unknown, so we cannot added something to the stack.

But with lists I won’t get the same error:

   var list: List[_ <: Number] = Nil;
   var intList : List[Integer] = new Integer(1) :: Nil;
   list = intList ; //no error
   list = new Float(2) :: vehicles;  //even float allowed

Now I can added even a float, but in fact I would believe the list is a List of Integers, so not Floats allowed.

1) Why is this allowed with lists, and not with Stacks? Is this due to the cons (::) operator?

2) What is the type of list? Is it dynamic?

3) Why is this allowed in Scala and not in Java?

4) Can I add something to the stack? (null is not working, in Java does because generic types only allows reference types)

  • 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-16T19:45:23+00:00Added an answer on May 16, 2026 at 7:45 pm

    :: isn’t a mutating operation. This means that x :: xs will return a List of type List[ commonSupertypeOf[ typeOf[x], elementTypeOf[xs] ] ] (this isn’t actual scala code, but I hope my point comes across), but it will not change the type of xs. If xs has type List[Float] and x has type Integer, then the expression x :: xs will have type List[Numeric], but the type of xs is still List[Float], so nothing breaks.

    add however is a mutating operation. xs.add(x) will add an Integer to a Stack whose type is Stack<Float>, which is clearly an error.

    This explains why doing x :: xs is not dangerous. Now to explain why it typeckecks:

    The singnature of :: on a List[A] is: def :: [B >: A] (x: B) : List[B].

    What this means is that for any types A and B where B is a supertype of A, :: given a value of type B and a list of type A will produce a list of type B. So when you do some someInteger :: someFloats, the compiler infers that B is Numeric and A is Float and everything works.

    In java terms that would be <B supertypeOf A> List<B> prepend(B item) except that supertypeOf isn’t legal java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I know there's a lot of other questions out there that deal with this

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.