zunior
zunior

Reputation: 861

Practical Difference Between Type and Class in Scala

What is practical difference between

class person(val name: String, val weight: Int)

and

type person = (String,Int)

?

Upvotes: 2

Views: 270

Answers (2)

Xiaohe Dong
Xiaohe Dong

Reputation: 5023

In this case, it means to compare a tuple with class person. From my understanding, there maybe no comparison. Probably this example should looks like this ?

class Person(val name: String, val weight: Int)

type P = Person

so P is just alias of Person in this case, nothing fancy.

I assume that you maybe little confused that what we gained if we use "type". From my knowledge, there are several scenarios which make the keyword "type" very useful.

  1. Cake pattern or stackable trait

trait Like { type T }

trait PersonLike extends Like { override type T = Person }

  1. If the type is too complicated, you can shortcut it

type T = (Int => Int) => (String => String)

Anyway, I am not sure this clarify your confusion.

Upvotes: 0

om-nom-nom
om-nom-nom

Reputation: 62835

Type aliases are just aliases. They're substituted (resolved) at compile time and do not exists further. As a result, there no way to reference them from, say, java code.

Upvotes: 1

Related Questions