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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:17:55+00:00 2026-05-27T18:17:55+00:00

My title is probably not really describing the problem real well. I do not

  • 0

My title is probably not really describing the problem real well. I do not need the answer to this question for what I am doing, I have things correct now, but while I was working with the Scala combinator parsers I had this issue that confused me. I would like to understand the language better (I’m a Scala newbie for the most part), so I thought I would see if anyone can explain this to me:

Here is the code:

package my.example

import scala.io.Source
import scala.util.parsing.input.StreamReader
import scala.util.parsing.combinator.lexical.StdLexical
import scala.util.parsing.combinator.syntactical.StandardTokenParsers

class DummyParser extends StandardTokenParsers
{
    def scan
    (
        filename : String
    ) : Unit =
    {
        // Read in file
        val input = StreamReader( Source.fromFile( filename ).bufferedReader )
        // I want a reference to lexical in StandardTokenParsers
        val mylexical = lexical
        // Even if I put a type above like these two below it does not help
        // val mylexical : StdLexical = lexical
        // val mylexical : Tokens = lexical
        val tokensGood : lexical.Scanner = new lexical.Scanner( input )
        /*
        Compile error in following line:
        error: type mismatch;
        found   : mylexical.Scanner
        required: DummyParser.this.lexical.Scanner
        */
        val tokensBad : lexical.Scanner = new mylexical.Scanner( input )
    }
}

The “val tokensBad” line gets the compile error shown in the comments. Isn’t mylexical above referencing the exact same object as this.lexical (defined in StandardTokenParsers that the class above is deriving from). Reading “Programming in Scala” I think I sort of understand that the type of lexical.Scanner is path dependent (Section 20.7), but shouldn’t lexical.Scanner and mylexical.Scanner be the same type? Isn’t lexical and mylexical the same object? Heck, the dog food example in the book on page 426 seems to say the SuitableFood type from two different dogs is the same, and in my case above its the exact same object (I think). What’s really going on here?

  • 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-27T18:17:56+00:00Added an answer on May 27, 2026 at 6:17 pm

    You would like the compiler to consider lexical.Scanner and mylexical.Scanner as equal, based on the fact that at runtime, the value of mylexical and lexical is always the same. This is a run-time property, and the type checker doesn’t do any data-flow analysis (that would be way too slow to be practical).

    So you need to help the type checker by telling it that the two values are always the same. You can do that by using a singleton type. A singleton type is a type that has exactly one value, and is written as (in this case) lexical.type.

    If you change the definition of mylexical to:

    val mylexical: lexical.type = lexical
    

    your program type-checks.

    What we just did is tell the type checker that mylexical can only have one value at runtime, given by the singleton type.

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

Sidebar

Related Questions

This is probably a really easy answer, but for the life of me I
I really have no idea what to title this so someone PLEASE feel free
Q: The title is perhaps too big of question and the answer probably is
The title is probably not very clear. I have the following example in mind:
The question title is probably not correct because part of my question is to
Probably not the best title, but I'll explain: I have an array of objects
I am not sure the title of this question covers what I mean. In
Not sure it is clear from this question title what I mean, so here
Title is the entire question. Can someone give me a reason why this happens?
Just a simple question. (probably really basic I quess) Using XmlHttp I am able

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.