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 1077313
In Process

The Archive Base Latest Questions

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

I am wondering why the two type parameters (named A) with the same name

  • 0

I am wondering why the two type parameters (named “A”) with the same name (“A”) is allowed as per the example below. I know this is a POOR naming of type parameters, don’t do this.

(My guess is that they are on a on a different scope level, e.g. class level and function level, and the compiler is using some kind of name mangling)

class  MyTest[A](){
    type MyType  = A

    def checkString[A](value:A, x:MyType):A = { 
       value match {
         case x:String => println("Value is a String")
         case _ => println("Value is not a String")
       }

       x match {
          case x:String => println("x is a String")
          case _ => println("x is not a String")
       }

       value
   }
}

Example output from 2.8.0

scala> val test = new MyTest[Int]
test: MyTest[Int] = MyTest@308ff65f

scala> test.checkString("String",1)
Value is a String
x is not a String
res7: java.lang.String = String

scala> test.checkString(1,1)
Value is not a String
x is not a String
res8: Int = 1
  • 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-16T21:33:23+00:00Added an answer on May 16, 2026 at 9:33 pm

    Nested scopes in Scala are free to shadow each others’ symbol tables. Types are not the only things you can do this with. For example:

    class X[A](a: A) {
      def X[A](a: A) {
        if (a==this.a) {
          val X = Some(this.a)
          X match {
            case Some(a) => "Confused much yet?"
            case _ => "Just because you can do this doesn't mean you should."
          }
        }
      }
    }
    

    The principle is that a scope has control over its namespace. This has dangers, if you use it foolishly (e.g. I have used X and a for each of three different things, and A for two–in fact, you could replace every identifier with X except for the one in the Some which has to be lower case). But it also has benefits when writing functional code–you don’t have to worry about having to rename some iterating variable or type or whatever just because you happen to place it in a different context.

    def example = {
      val a = Array(1,2,3,4,5)
      val sumsq = a.map(i => i*i).sum
      a.map(i => {
        val a = Array.range(1,i)
        val sumsq = a.map(i => i*i).sum  // Cut and paste from above, and works!
        sumsq + i
      }).sum
    }
    

    So be aware that you have the power to confuse yourself, and wisely choose to use that power non-confusingly.

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

Sidebar

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.