Type alias and the optional braces?

Hi,

For the following type Aux,Is there the syntax to define Aux without the braces (in Scala 3)?

trait T[A]:
type U

type Aux[A, P] = T[A] {
type U = P
}

Thanks.

Guofeng

It seems there is!

 ➜ scala
Welcome to Scala 3.1.1 (17.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                       
scala> trait T[A]:
     |   type U
     | 
// defined trait T
                                                                                                                                       
scala> type Aux[A, P] = T[A] with
     |   type U = P
     | 
// defined alias type Aux[A,P] = T[A]{U = P}
                                                                                                                                       
scala> 

I met another issues, how to define the following structural type without braces?

type payByAreaType = {
  val x: Int
  val y: Int
  def area: Double
}

Thanks for your help!

Guofeng

Same way:

scala> type payByAreaType = Object with
     |   val x: Int
     |   val y: Int
     |   def area: Double
     | 
// defined alias type payByAreaType = Object{x: Int; y: Int; area: Double}
                                                                                                                      
scala> type payByAreaType2 = {
     |   val x: Int
     |   val y: Int
     |   def area: Double
     | }
// defined alias type payByAreaType2 = Object{x: Int; y: Int; area: Double}

Got it.

But Object is a class in Java, shall I use Any or AnyRef? is there any difference between using Object and using Any/AnyRef?

Thanks again.

Dunno, they both work:

scala> type payByAreaType = AnyRef with
     |   val x: Int
     |   val y: Int
     |   def area: Double
     | 
// defined alias type payByAreaType = AnyRef{x: Int; y: Int; area: Double}
                                                                                                                      
scala> type payByAreaType = AnyVal with
     |   val x: Int
     |   val y: Int
     |   def area: Double
     | 
// defined alias type payByAreaType = AnyVal{x: Int; y: Int; area: Double}

I don’t know why you would want to define a type alias like this, so it’s up to you. I don’t really get the idea myself.

AnyRef is an alias of Object. And a structural type { def foo: Int } is equivalent to AnyRef { def foo: Int }. So if you want to include value classes in the possible instances of your structural type you should explicitly use Any { def foo: Int }.

Understand clearly.Thanks all.

Just for you to know the following.

The above code works well using Scala REPL, but In VS code, there is a red indicator under the ‘with’ keyword. The problem panel show “identifier expected but indent found scalameta”. Scalametas is v1.19.0, tested against Scala 3.2.0/3.1.3

I think it should be a bug of Scalametals, and reported it to the Scalametals projects.

The issue occurred when I first try the solution here, and it still exists now.

If Scalameta doesn’t accept a piece of code that the compiler does accept, you should probably open an issue here GitHub - scalameta/scalameta: Library to read, analyze, transform and generate Scala programs

Reported and acceptted as a bug. see https://github.com/scalameta/scalameta/issues/2853