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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:57:12+00:00 2026-06-13T05:57:12+00:00

Could someone explain how the type keyword and # operator works in scala and

  • 0

Could someone explain how the type keyword and # operator works in scala and how to use it?
Please look at examples.

//Example1
scala>  type t1 = Option.type
defined type alias t1

//Shouldn't this work since previous example simply works?
scala>  type t2 = String.type
<console>:7: error: type mismatch;
 found   : String.type
 required: AnyRef
       type t2 = String.type
         ^

//lets define custom trait T
scala>  trait T
defined trait T

//... and obtain it's type like in Example1.
//Shouldn't this work since previous Example1 simply works?
scala>  type t3 = T.type
<console>:7: error: not found: value T
       type t3 = T.type
         ^

//Lets define some value of type T
scala>  val v4 = new T{}
v4: T = $anon$1@5c3e8c76

//and obtain it's type (works)
scala>  type t4 = v4.type
defined type alias t4

//this doesn't work
scala>  type t4_1 = (new T{}).type
<console>:1: error: identifier expected but 'new' found.
       type t4_1 = (new T{}).type

//as well as this (doesn't work)
scala>  type t5 = "abc".type
<console>:1: error: identifier expected but string literal found.
       type t5 = "abc".type
         ^

//but this compiles well
scala>  val v6 = "abc"
v6: String = abc

scala>  type t6 = v6.type
defined type alias t6


//lets create some values of created types:
scala>  type t1 = Option.type
defined type alias t1

scala>  val v1_1 = Some(10)
v1_1: Some[Int] = Some(10)

scala>  type t7 = v1_1.type
defined type alias t7

scala>  val v7:t7 = null
v7: t7 = null

scala>  val v7_1:t7 = v1_1
v7_1: t7 = Some(10)

scala>  val v7_2:t7 = Some(10)
<console>:9: error: type mismatch;
 found   : Some[Int]
 required: t7
    (which expands to)  v1_1.type
       val v7_2:t7 = Some(10)
             ^


//next let's try # operator

scala>  class X[A,B](a:A,b:B)
defined class X

//doesn't work
scala>  type xa = X[A,B]#A
<console>:8: error: not found: type A
       type xa = X[A,B]#A
           ^
<console>:8: error: not found: type B
       type xa = X[A,B]#A
             ^

//but such approach works:
scala>  trait X2[C]{
  type A
  type B
  val c:C
}
defined trait X2

scala>  type xa2_1 = X2[String]#A
defined type alias xa2_1

scala>  type xa2_2[M] = X2[M]#A
defined type alias xa2_2
  • 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-13T05:57:14+00:00Added an answer on June 13, 2026 at 5:57 am

    First, your questions about type:

    The right hand side of a type declaration has to be the name of a type with a stable path. So taking your examples one by one:

    type t1 = Option.type
    

    t1 is an alias for the type of the Option object, not the Option class.

    type t2 = String.type
    

    This is an error because there is no String object. The error’s a little weird because String’s a Java class and so operates under different rules (since Java classes never have companions).

    type t3 = T.type
    

    ditto. This time the error’s clearer, because T is a Scala class and so the compiler can unambiguously say “T does not name an object with a type”

    type t4 = v4.type
    

    This is the singleton type of the object named by the val v4. It doesn’t refer to any instance of type T, or even any instance of the anonymous class created by your new T{} expression. It refers to a type that is only represented by v4 and null, i.e. they are the only allowed values of that type.

    type t4_1 = (new T{}).type
    

    This is illegal because the thing you’re taking the type of has to be a stable identifier (roughly, an identifier whose referant can never change — if the full path to the idenfier consists of only the names of packages, objects, and vals, it’s stable).

    type t5 = "abc".type
    

    Ditto.

    type t6 = v6.type
    

    v6 is a stable identifier. t6 is the type inhabited solely by that particular instance of String which is referred to by the name v6 (and null).

    type v6 = v1_1.type
    

    Again, a singleton type.

    val v7: t7 = null
    

    null is a valid value of type t7

    val v7_1:t7 = v1_1
    

    So is this particular object.

    val v7_2:t7 = Some(10)
    

    But this is a different object (even though it’s == to v7, it’s not eq to it) and therefore is not a member of this type.

    Now about #:

    class X[A,B](a:A,b:B)
    

    A and B are type parameters. They can’t be referred to outside the class. You can think of them like abstract type aliases with private[this] visibility, though that’s not quite accurate.

    type xa = X[A,B]#A
    

    So yeah, not visible.

    type xa2_1 = X2[String]#A
    

    Since this A refers to a public type alias, it can be referred to by name outside the class. Note that this particular case is pretty useless, because you know absolutely nothing about this type. if your trait X2 had a method that returned values of type A, you could do something like

    val aFromX2: xa2_1 = x2instance.methodThatReturnsAnA
    

    ..but then you couldn’t do anything else with it, even pass it back to an instance of X2[String] because there’s no guarantee that that the two As would refer to the same type! On the other hand, if you have a concrete instance, you could do this:

    def passAroundA(x2instance: X2[String]) {
      type x2a = x2instance.A // note dot, not #
      val a: x2a = x2instance.methodThatReturnsAnA
      x2instance.methodThatTakesAnA(a)
    }
    

    In this case it works because even though we have no idea what A actually is, we know that the two methods use the same type — whatever was fixed at x2instance‘s construction.

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

Sidebar

Related Questions

Could someone explain and provide real world examples of using with keyword in defining
Could someone please explain why this happening: class Apple { String type; setType(){ System.out.println(inside
Could someone explain what is going on when we use the typedef type cast
Could someone please explain clearly and succinctly the concepts of language type systems? I've
Please could someone explain to me why this regex does not match anything, when
please, could someone explain to me a few basic things about working with languages
Could someone explain why I had to type 10 single quotes here just to
Could someone explain why this works. I have 2 classes in Eclipse. A Class
I don't understand the following phenomenon, could someone explain me please what I got
Please, could someone explain me why "make-array" has no effect on plant1? (LET (plant1)

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.