Mr. T
Mr. T

Reputation: 85

Scala: How to return a type

I have defined a type in one class like the following:

type attributes = (Double, Date, Date)

Now I would like to use that type in another class. Is it somehow possible to return this type via a method or so?

EDIT: I wish to use this type to define the parameters and output of methods in other classes. I would like to have the definition of this type in just one class so that I am flexible if I want to change the type.

A possibility I just found is to use traits to define a type:

trait Attributes {
val time: Double
val departure: Date
val arrival: Date
}

That for example makes it possible for me to use that type later when defining a method in another class:

def log(attributes: Attributes): Unit = {
...
}

This is how I wanted to use the type later. However, creating a trait for that doesn't seem to be a nice solution as the trait will never be extended; it just defines a type. I would like to have the type defined in a class and then make use of that definition in another class. For example like this:

def log(attributes: SomeClass.Attributes): Unit = {
...
}

Upvotes: 2

Views: 861

Answers (4)

Alexey Romanov
Alexey Romanov

Reputation: 170713

I wish to use this type to define the parameters and output of methods in other classes. I would like to have the definition of this type in just one class so that I am flexible if I want to change the type... However, creating a trait for that doesn't seem to be a nice solution as the trait will never be extended; it just defines a type.

This is (part of) what case classes are for: case class Attributes(time: Double, departure: Date, arrival: Date). You can define it at top level instead of inside a class, unlike a type. If you do want to associate it with a class, I suggest putting it into the companion object instead:

class SomeClass { ... }
object SomeClass {
  case class Attributes(time: Double, departure: Date, arrival: Date)
}

Upvotes: 0

Toxaris
Toxaris

Reputation: 7266

Put the type definition into an object:

object SomeClass {
  type attributes = ...
}

class SomeClass {
  ...
}

def log(attributes: SomeClass.Attributes): Unit = {
  ...
}

Upvotes: -1

Jesper
Jesper

Reputation: 206776

The syntax for that is like this:

class Example {
  type attributes = (Double, Date, Date)
}

class AnotherClass {
  def method(attrs: Example#attributes) = ???
}

You can also import the type, for example:

object Example {  // changed this to an object instead of a class
  type attributes = (Double, Date, Date)
}

class AnotherClass {
  import Example.attributes

  def method(attrs: attributes) = ???
}

Upvotes: 2

Aditya Singh
Aditya Singh

Reputation: 16650

Yes you can do it by explicitly mentioning the type

type attributes = (Double, String)

def myFunc(a: attributes): attributes = a

myFunc((12.67, "Test" ))

Result:

defined type alias attributes
myFunc: (a: (Double, String))(Double, String)
res0: (Double, String) = (12.67,Test)

Upvotes: 0

Related Questions