1

Im quite new in Scala. I need to get the parametrization of a class. How can I do this ? the class looks like this:

   class OutPort[T](name: Symbol, owner: Component) extends Port[T](name)

i got many OutPorts in an LinkedList. In another class i want to get the parametrization of an instance of OutPort, but the parametrization is arbitrary and a solution with isInstanceOf is not capable. is there a special method for such purpose, which i didnt covered yet ?

Lunatikz

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
lunatikz
  • 716
  • 1
  • 11
  • 27

2 Answers2

5

Keep a Manifest of the type, and use that. If you told us why you want to get that back, we might be able to answer the question better too.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • I have to make a simulation framework, where i want to simulate behavior of some components. In this Framework, there is a gui, and i have to delegate values from the gui, to components. – lunatikz Jun 19 '11 at 21:53
  • components have Ports, which are parametrized with a special type. When I get the inputs from gui, i have strings or ints and have to box them in another type, which has a field data of type any, to store data of any type. the type of field data must be arbitrary. then i can send the boxed item to an component which have to delegate this to its outport. a component can have many outports for different types. when i got the data and it is a Int for example, i want to determine the Port with the int parametrisation. the framework must deal with generated scala files – lunatikz Jun 19 '11 at 22:02
  • where the datatypes used are arbitrary and in consequence to me unknown. Im sorry for my pssibly bad english – lunatikz Jun 19 '11 at 22:03
  • 3
    @lunatikz - you can edit your question to provide more detail - easier to read than lots of comments! :-) – Ben Lings Jun 19 '11 at 22:15
  • If you have a GUI you probably have dynamic configuration of ports and connections, and won't have any use for the type parameter anyway. – Jesper Nordenberg Jun 20 '11 at 10:43
2

As mentioned before, you can use a ClassManifest or a manifest. Here's a use example :

class Foo[T](t: T)(implicit m: ClassManifest[T]) {
  def foo = m toString
}

scala> (new Foo(5)) foo

` res1: java.lang.String = Int

scala> (new Foo("hi")) foo
res2: java.lang.String = java.lang.String

scala> (new Foo(new scala.swing.Frame)) foo
res3: java.lang.String = scala.swing.Frame

Here's some SO topics related to Scala's manifests :

What is a Manifest in Scala and when do you need it?

How does Scala's (2.8) Manifest work?

How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?

Community
  • 1
  • 1
Aymen
  • 558
  • 6
  • 13