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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:02:47+00:00 2026-06-16T21:02:47+00:00

trait B { type MyInnerType def foo: MyInnerType } object B1 extends B {

  • 0
  trait B {
    type MyInnerType
    def foo: MyInnerType
  }      
  object B1 extends B {
    type MyInnerType = Double
    val foo = 3.0
  }

  trait A {
    type MyInnerType
    val b: B
    def foo(x: b.MyInnerType): MyInnerType
    def bar(y: MyInnerType): Unit
  }
  object A1 extends A {
    type MyInnerType = Int
    val b = B1
    def foo(x: b.MyInnerType) = 1
    def bar(y: MyInnerType) {}
  }
  object A2 extends A {
    type MyInnerType = String
    val b = B1
    def foo(x: b.MyInnerType) = "a"
    def bar(y: MyInnerType) {}
  }

  val as = Seq(A1, A2)
  as foreach { a => a.bar(a.foo(a.b.foo)) }   // wrong, a.foo(a.b.foo) infers to Any

However, if a.foo does not take parameters, everything works perfectly and a.foo infers to a.MyInnerType. It also works if I cast .asInstanceOf[a.MyInnerType]. Any explanations?

  • 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-16T21:02:48+00:00Added an answer on June 16, 2026 at 9:02 pm

    I’m running scala 2.9.1 and on the REPL I get this for as:

    scala> val as = Seq(A1, A2)
    as: Seq[ScalaObject with A{def foo(x: Double): Any; val b: B1.type; type MyInnerType >: java.lang.String with Int}] = List(A1$@6da13047, A2$@7168bd8b)
    

    However, when I change it to val as:Seq[A] = Seq(A1, A2) I get:

    scala> val as:Seq[A] = Seq(A1, A2)
    as: Seq[A] = List(A1$@6da13047, A2$@7168bd8b)
    
    scala> as foreach { a => a.bar(a.foo(a.b.foo)) }
    

    Sometimes (all the time) scala has trouble inferring types, so you have to annotate what you want. I go to the REPL often to find out what’s really going on.

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

Sidebar

Related Questions

The following: trait Foo { type T val foo: T } trait Bar extends
I have a trait and an implementation looking like: trait Foo[A] { def bar[B
class PasswordCaseClass(val password:String) trait PasswordTrait { self:PasswordCaseClass => override def password = blue }
Observe the following code trait Example { type O def apply(o: O) def f(o:
How would I convert: trait Foo[A <: Foo[A]] to a type member? I.e., I
I have this situation object SuperHorribleLongName { trait X { private[SuperHorribleLongName] def internalGaga() :
Assume the following trait: trait A { type B def +(a:A):A } I use
Given this code: trait S { def s: String } trait I { def
I have the following code: trait CellT[VAL <: AnyVal] { var value: VAL type
I want to define a type-class like this: trait CanFold[-T, R] { def sum(acc:

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.