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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:04:31+00:00 2026-05-27T17:04:31+00:00

Sometimes in Scala I find that I get type mismatches related to path-dependent types,

  • 0

Sometimes in Scala I find that I get type mismatches related to path-dependent types, but I can easily reason that in fact the types coincide. Here’s a simple example:

trait Foo { trait Bar }

object Main extends App {
  val foo1 = new Foo { }
  val foo2 = foo1

  def turkle(x: foo1.Bar) {}

  turkle(new foo2.Bar {})
}

which gives: “type mismatch; found : java.lang.Object with Main.foo2.Bar required: Main.foo1.Bar”.

Now of course the paths Main.foo1.Bar and Main.foo2.Bar must coincide, since we wrote val foo2 = foo1. We can verify this, by changing the last line to

turkle((new foo2.Bar {}).asInstanceOf[foo1.Bar])

which both compiles and runs without an exception.

Can Scala automatically perform reasoning like this? If so, how can I make this happen?

(And if not, are there any prospects for extending the type system in this direction?)

I’ll note that sometimes Scala does appear to perform this sort of reasoning. Supposing I change trait Foo to object Foo:

object Foo { trait Bar }

object Main extends App {
  val foo1 = Foo
  val foo2 = foo1

  def turkle(x: foo1.Bar) {}

  turkle(new foo2.Bar {})
}

Now everything compiles fine: somehow Scala has worked out that both Main.foo1.Bar and Main.foo2.Bar are really the same as Foo.Bar.

  • 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-27T17:04:32+00:00Added an answer on May 27, 2026 at 5:04 pm

    Iulian Dragos gave the answer you need in a recent question. The short version is that the compiler doesn’t do flow analysis, so in your first example it can’t tell that foo1.Bar and foo2.Bar are the same type since foo1 and foo2 are just of type Foo. But in the second example foo1 is inferred to be the singleton type Foo.type (a subtype of Foo), so things work as expected.

    You can make your first example work by declaring foo2 as being the singleton type of foo1:

    val foo2:foo1.type = foo1
    

    See section 3.2.1 of the Scala Language Specification 2.9 for reference.

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

Sidebar

Related Questions

The Scala compiler can often infer return types for methods, but there are some
I know that you can mark a scala object as @serializable , but I
I've been doing some research on path-dependent types. The best description I could find
sometimes after exiting the scala interpreter I get back to my bash shell and
I'm new to Scala, and I'm struggling to understand why I sometimes don't get
I sometimes find myself wanting to perform nested iterations over infinite streams in Scala
I am new to Scala and I really like it, but sometimes it surprises
EDIT: Sometimes it happens that our problem is kind of not in code but
I found out reading the spec that scala supports binding type variables when doing
I am still sometimes puzzled by scala occasional syntactic magic. I thought, that writing

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.