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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:41:06+00:00 2026-05-25T12:41:06+00:00

I found out reading the spec that scala supports binding type variables when doing

  • 0

I found out reading the spec that scala supports binding type variables when doing a type pattern match:

Map(1 -> "one", 2 -> "two") match {
  case l: Map[k, v] =>
    // binds k to Int and v to String
    // k and v are types as shown here:
    val i: Iterator[Tuple2[k, v]] = l.iterator
    println(i.mkString(", "))
}

Are there any fancy things or practical things I can do with this? Or binding type variables is only useful for type documentation purpose?

It occurred to me that Scala sometimes needs type annotations, such as defining function, so I tried:

def prepender(obj: Any) = obj match {
  case xs: List[a] => (x: a) => x :: xs
  case opt: Some[a] => (x: a) => x :: Nil
}

But then the type of the return function is weird:

prepender: (obj: Any)a with a => List[Any] forSome { type a; type a }

scala> val p = prepender(List(1,2))
p: a with a => List[Any] forSome { type a; type a } = <function1>

scala> p(1)
<console>:10: error: type mismatch;
 found   : Int(1)
 required: a(in value res7) with (some other)a(in value res7) where 
   type (some other)a(in value res7), type a(in value res7)
  • 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-25T12:41:07+00:00Added an answer on May 25, 2026 at 12:41 pm

    I hope this won’t get too long, but I seriously doubt it, that’s why I’m gonna try to provide a quick answer first: “When you name (abstract) something, the main use case is referring to it later”. Well that wasn’t helpful now, was it?

    Consider this simple Scala function:

    val sum = (a: Int, b: Int) => a + b
    

    The compiler does not need to know that a is an a and b is a b. All it needs to know that a as well as b are of type Int and that a comes before b (which wouldn’t matter in this case since addition is commutative, but the compiler cares anyway!). Scala offers a (don’t get me wrong I also love it) compiler friendly placeholder syntax, which acts as a proof of this “hypothesis”.

    val sum: (Int, Int) => Int = _ + _ // where the 1st _ differs from the 2nd _
    

    Now take a look at this:

    case x: SomeTypeParameterizedWith[AnotherType] // AnotherType is erased anyway
    case x: SomeParameterizedType[_] // Existential type
    case x: SomeParameterizedType[kind] // Existential type which you can reference
    

    When you don’t care about the type argument use the placeholder syntax. When you do (for whatever reason) care you should name the type argument with a lower case so the compiler knows you want to treat it as an identifier.

    Back to your question.

    The primary use for existential types is working around Java’s wildcard types.
    This is taken from Programming in Scala – Existential Types and was slightly modified by yours truly.

    // This is a Java class with wildcards
    public class Wild {
      public java.util.Collection<?> contents() {
        java.util.Collection<String> stuff = new Vector<String>();
        stuff.add("a");
        stuff.add("b");
        stuff.add("see");
        return stuff;
      }
    }
    
    // This is the problem
    import scala.collection.mutable.Set
    val iter = (new Wild).contents.iterator
    val set = Set.empty[???] // what type goes here?
    while (iter.hasMore)
      set += iter.next()
    
    // This is the solution
    def javaSet2ScalaSet[T](jset: java.util.Collection[T]): Set[T] = {
      val sset = Set.empty[T] // now T can be named!
      val iter = jset.iterator
      while (iter.hasNext)
        sset += iter.next()
      sset
    }
    

    Ok, so what just happened? Simple generics, no magic there?! If you are dealing with generics on a day to day basis this looks normal to you, but you are forgetting, that the ultra super concept of introducing type arguments into scope works only on classes and methods. What if you are outside of a class or a method, just in some random scope in the middle of nowhere (like REPL)? Or what if you are in a class or a method but the type arguments have not been introduced into their scopes? This is where your question and this answer come in play.

    val set = new Wild().contents match {
      case jset: java.util.Collection[kind] => {
        val sset = Set.empty[kind]
        val iter = jset.iterator
        while (iter.hasNext)
          sset += iter.next()
        sset
      }
    }
    

    The identifier kind is required so the compiler can verify that you are referring to the same thing.

    Note, that you can’t just add strings into the set since the type of the set is Set[_].

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

Sidebar

Related Questions

I have been doing some reading and have found out that the Java Swing
Reading Kohana's documentation, I found out that the main difference in 3.0 version is
I found out that HTML form supports only two enctype types. They are application/x-www-form-urlencoded
I was reading the lua manual (looking for something) and I found out that
i'm reading liferay source code and found out that 2 xml files using same
I am reading articles about css. I found out that many of writers suggest
I was reading about WCF security implementations and found out that there are 2
I have been doing some unsafe bitmap operations and have found out that increasing
I was reading a part of the LuaInterface Tutorial (here) and found out that
While I was reading the reference about fopen function, I found out that FOPEN_MAX

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.