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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:12:34+00:00 2026-06-10T14:12:34+00:00

What works (Part A) Suppose I have a trait with a type parameter: trait

  • 0

What works (Part A)

Suppose I have a trait with a type parameter:

trait A[T]

I can use an existential type to write a method that will take a collection of As that all have the same T:

def foo(as: Seq[A[X]] forSome { type X }) = true

Note that this is different from the following:

def otherFoo(as: Seq[A[X] forSome { type X }]) = true

Or the equivalent:

def otherFoo(as: Seq[A[_]]) = true

In these cases the scope of the existential is inside the Seq, so the As can have different Ts. With my original foo (with the existential scoping over the Seq), the following is fine:

foo(Seq(new A[Int] {}, new A[Int] {}))

But make the type parameters different and it doesn’t compile:

scala> foo(Seq(new A[Int] {}, new A[String] {}))
<console>:10: error: type mismatch;
 found   : Seq[A[_ >: java.lang.String with Int]]
 required: Seq[A[X]] forSome { type X }

              foo(Seq(new A[Int] {}, new A[String] {}))
                     ^

This is all pretty straightforward.

What works (Part B)

Now suppose I have a similar trait with a type member instead of a type parameter:

trait B { type T }

I can write a method that will only take a B with some specified T:

scala> def bar[X](b: B { type T = X }) = true
bar: [X](b: B{type T = X})Boolean

scala> bar[Int](new B { type T = Int })
res5: Boolean = true

scala> bar[String](new B { type T = Int })
<console>:10: error: type mismatch;
 found   : java.lang.Object with B
 required: B{type T = String}
              bar[String](new B { type T = Int })
                          ^

Again, this works exactly the way you’d expect it to.

What doesn’t work

When we try to write the equivalent of our foo above, but for type members, things get weird.

scala> def baz(bs: Seq[B { type T = X }] forSome { type X }) = true
baz: (as: Seq[B{type T = X}] forSome { type X })Boolean

scala> baz(Seq(new B { type T = Int }, new B { type T = String }))
res7: Boolean = true

That the last line compiles makes no sense to me. I’ve told it that I want all the type members to be the same. My foo shows that I can do this for type parameters, and bar shows that I can constrain a type based on its type members. But I can’t combine the two.

I’ve tried this on 2.9.2 and 2.10.0-M5.

Motivation

This question is inspired by this one, where my first thought was, oh, just use an existential type (setting aside for a second the issue that it seems to be impossible to get an existential type to scope of the type of a repeated parameter, which would be handy here):

def accept(rs: Seq[RList[Int] { type S = X }] forSome { type X }) = true

But this doesn’t actually work—you get the same weird result as in the simplified example above.

  • 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-06-10T14:12:36+00:00Added an answer on June 10, 2026 at 2:12 pm

    I’ve finally sort it out (at least I hope so). Let’s do it the other way. We build our trait:

    scala> trait B {type T}
    defined trait B
    

    We try and build a sequence of B:

    scala> Seq(new B {type T = Int}, new B {type T = String})
    res0: Seq[B{type T >: String with Int}] = List($anon$1@592b12d, $anon$2@61ae0436)
    

    Damn, it works! Ok, we don’t have an equality for type T but let’s play with it:

    scala> res0 : (Seq[B {type T = X}] forSome {type X >: String with Int})
    res1: Seq[B{type T = X}] forSome { type X >: String with Int } = List($anon$1@592b12d, $anon$2@61ae0436)
    

    It’s closer. No wait, it’s not closer, It’s better than what you’ve proposed as a parameter of baz, we do not only provide a raw type, we also have a upper bound! Thus, we can clearly pass it to baz. This is why it doesn’t work as you expected.

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

Sidebar

Related Questions

I will keep this quick. The attached code for the most part works i
I have a slider I've made, the slider part works correctly, but I'm trying
i have part of Asp.NET 1.1 project. I work with remote site, which works
I have an order-statistic augmented red black tree. it works for the most part.
Suppose I have a very large site like amazon.com. I’d like to use asp.net
For an assignment, part of what I have to do involves the use of
I have a foreach loop that is suppose to check to see if a
So i have a query that is suppose to get me data from two
I have an app that allows you to take a picture, adjust the jpeg
Let's suppose I have a web application made with Backbone, which use Backbone.Router for

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.