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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:11:55+00:00 2026-05-14T01:11:55+00:00

I am trying to understand type members in Scala. I wrote a simple example

  • 0

I am trying to understand type members in Scala. I wrote a simple example that tries to explain my question.

First, I created two classes for types:

class BaseclassForTypes
class OwnType extends BaseclassForTypes

Then, I defined an abstract type member in trait and then defined the type member in a concerete class:

trait ScalaTypesTest {
  type T <: BaseclassForTypes

  def returnType: T
}

class ScalaTypesTestImpl extends ScalaTypesTest {
  type T = OwnType

  override def returnType: T = {
    new T
  }
} 

Then, I want to access the type member (yes, the type is not needed here, but this explains my question). Both examples work.

Solution 1. Declaring the type, but the problem here is that it does not use the type member and the type information is duplicated (caller and callee).

val typeTest = new ScalaTypesTestImpl
val typeObject:OwnType = typeTest.returnType // declare the type second time here
true must beTrue

Solution 2. Initializing the class and using the type through the object. I don’t like this, since the class needs to be initialized

val typeTest = new ScalaTypesTestImpl
val typeObject:typeTest.T = typeTest.returnType // through an instance
true must beTrue

So, is there a better way of doing this or are type members meant to be used only with the internal implementation of a class?

  • 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-14T01:11:55+00:00Added an answer on May 14, 2026 at 1:11 am

    You can use ScalaTypesTestImpl#T instead of typeTest.T, or

    val typeTest:ScalaTypesTest = new ScalaTypesTestImpl
    val typeObject:ScalaTypesTest#T = typeTest.returnType
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to understand type patterns and generic classes in Haskell but can't seem
I'm trying to understand .symtab setcion type for the relocatable object files. And I
I'm trying to understand the constraints on generic type parameters in C#. What is
Am just trying to understand why this exception throws. Cannot implicitly convert type 'System.DateTime?'
I'm trying to understand how to use Type Converters after reading this answer to
I am trying to write a simple CASE statement in MDX that returns Measure
I've been trying to understand Oracle database's use of data type = number. I
Trying to understand why you can't use sql_variant for the value type when using
I am learning c# and trying to understand the type oriented aspects of it.
I am trying to understand tagged types and code extensions and reuse. I first

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.