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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:16:54+00:00 2026-06-11T00:16:54+00:00

I have this class in scala trait PageComponent { protected var page:Page = _

  • 0

I have this class in scala

trait PageComponent {
  protected var page:Page = _
  protected var pageData:PageData = _
  def initialise(page0:Page, pageData0:PageData) {
    page = page0
    pageData = pageData0    
  }
}

Each subclass of PageComponent has it’s own subclass of Page and PageData as well. When a subclass calls page or pageData, I want it to return it’s own type for this.

I’ve tried stuff like

trait PageComponent {
  type P <: Page
  protected var page:P = _
  protected var pageData:PageData = _
  def initialise(page0:P, pageData0:PageData) {
    page = page0
    pageData = pageData0    
  }
}

and this sort of works such that the subclass can call page and gets the page type defined by implementing the type P.

However, I’m trying to call the initialise method and I’m passing through an object of type Page and it is showing this error

error: type mismatch;
found   : pageInfoToGoTo.page.type (with underlying type com.xxx.gui.Page)
required: pageComponentToGoTo.P
pageComponentToGoTo.initialise(pageInfoToGoTo.page, pageData)

I’m obviously wrong here but I thought because P “is a” Page this should work for me.

Any ideas?

—–edit for more info——

I do have something like

class AhoyPage extends Page    

object MyPageComponent extends PageComponent {
  type P = AhoyPage
}

after reading jwinandy’s answer I changed the code so that the initialise method is called this way

pageComponentToGoTo.initialise(pageInfoToGoTo.page.asInstanceOf[pageComponentToGoTo.P], pageData)

and it now works. So I guess my question is answered. Thanks. That cast isn’t very nice though. I have a list of PageInfo that looks like this

PageInfo(page:Page, ...)

when a PageInfo is selected I use the page to look up the page component in a map. There must be a better way of doing it but if not, I’m happy with this.

  • 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-11T00:16:56+00:00Added an answer on June 11, 2026 at 12:16 am

    You have to define P when you subclass.

    In you case, you have to had :

    type P = com.xxx.gui.Page
    

    Example, this works :

    trait Page
    trait PageData
    
    trait PageComponent {
      type P <: Page
      protected var page:P = _
      protected var pageData:PageData = _
      def initialise(page0:P, pageData0:PageData) {
        page = page0
        pageData = pageData0    
      }
    }
    
    class AhoyPage extends Page    
    
    object MyPageComponent extends PageComponent {
    
        type P = AhoyPage
    }
    
    object MyApp extends App {
        MyPageComponent.initialise(new AhoyPage, new PageData {})
        // OR
        MyPageComponent.initialise(new MyPageComponent.P, new PageData {})
    
    }
    

    Please post more of your code if you want to refactor a bit to achieve a better level of composability.

    — edit —

    Like https://gist.github.com/3608684

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

Sidebar

Related Questions

Scala programmer should have known that this sort of writing : class Person{ var
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
Let's say that I have this: class Dependency1 { def methodD1 { } }
If I have the following Scala code: trait BaseTrait[EnumType <: Enumeration] { protected val
I have the following code: class Parameterizable{ var map: Map[String, String] = new scala.collection.immutable.HashMap()
I have the following classes/traits in Scala trait Write[-T] { def add(elem : T);
I have something like this in scala: abstract class Point[Type](n: String){ val name =
I have this class: Class B { private String D; private String E; }
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class in which I run a for loop 10 times. This

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.