mardi 22 septembre 2020

Is there some way in scala that I can return a type?

I have a lot of classes such as DataFrameFlow, TextFlow, RDDFlow. They all derive from base class Flow.

Now I want to write a function judgeFlow which can read from a path: String and return something representing exact Flow type from which I can create corresponding instance. The whole code seems like the following

def judgeFlow(path:String) = /*1*/ {
  Flow.getStoreType(path) match {
    case StoreType.tdw =>
      DataFrameFlow
    case StoreType.hdfs =>
      TextFlow
  }
}

def createFlow(typeInfo:/*2*/) = /*3*/{
  new typeInfo()
}

However, I don't know how to write in place 1, 2 and 3.

EDIT

Knowing how to construct them is not enough here, because I also want the following:

  1. pattern matching through typeInfo
  2. some ways to do asInstanceOf

EDIT 2

Definition of Flow

abstract class Flow(var outputName: String) extends Serializable{
  def this() = this("")
...
}

Definition of DataFrameFlow

class DataFrameFlow(d: DataFrame, path: String) extends Flow {
  var data: DataFrame = d

  def this(data: DataFrame) = this(data, "")
  def this(path: String) = this(null, path)
  def this() = this(null, "")
...
}




Aucun commentaire:

Enregistrer un commentaire