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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:36:05+00:00 2026-06-18T00:36:05+00:00

I have asked a few questions around this topic but this time I want

  • 0

I have asked a few questions around this topic but this time I want to make it a more general discussion, since it seems to me that Scala is lacking some very important blocks.

Consider the following code (which is simplified from my real project),

trait World {
  type State <: StateIntf
  def evolve(s: State): State
  def initialState: State
}

class Algorithm(world: World) {
  def process(s: world.State) {
    val s1 = world.evolve(s)
    // ... do something with s and s1
  }
}

Everything seems so beautiful and mathematical, but

object SomeWorld extends World {...}
new Algorithm(SomeWorld).process(SomeWorld.initialState)  // incompatible type

Certainly you can do in the following way

trait World {
  type State <: StateIntf
  var s: State
  def evolve: Unit      // s = next state
  def initialize: Unit  // s = initial state
  def getState: StateIntf = s
}

But we are just back to the mutable world.

I am told that this is because Scala does not have flow analysis. If that is the problem, shouldn’t Scala get that piece? I only need that compilor can be aware that values passed from val to val are the same so that their inner types must agree. This seems so natural to me, as:

  1. val is the most foundamental concept that involves immutability in scala
  2. Path dependent type compatability is needed to model things like World with complete immutability (which is highly desired from a mathematical perspective)
  3. Flow analysis of passing vals solve the problem

Am I asking for too much? Or is there already a nice way of solving it?

  • 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-18T00:36:07+00:00Added an answer on June 18, 2026 at 12:36 am

    The compiler sometimes needs a little bit of help to prove that what you are doing is legal when using path dependent types. I.e., as you said, the compiler is missing flow analysis, so we must tell it explicitly that we aren’t just using any World, we are using exactly SomeWorld so that we can use SomeWorld.initialState.

    In your case, if you change Algorithm like so:

    class Algorithm[W <: World](world: W) {
      def process(s: world.State) {
        val s1 = world.evolve(s)
        // ... do something with s and s1
      }
    }
    

    Then the following compiles:

    object SomeWorld extends World {...}
    new Algorithm[SomeWorld.type](SomeWorld).process(SomeWorld.initialState)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked a few questions which have touched around this issue, but I've been
I have asked a few questions related to this, but from different angles. I
I have asked a few questions on here and read a few articles around
I know this questions has been asked a few times here. But these seem
I know a few questions have been asked like this one before, such as
I have seen this question asked a few times on SO - but the
I know similar questions have been asked a few times, but I'm struggling to
I guess I've asked a few similar questions before, but I was beating around
I've seen a few questions similar to this one, but none of them have
I know similar questions have been asked before but i think this is slightly

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.