String 可以在Scala反射中将字符串转换为类型吗?
我想在scala中将字符串转换为类型。以以下两种情况为例:String 可以在Scala反射中将字符串转换为类型吗?,string,scala,reflection,types,String,Scala,Reflection,Types,我想在scala中将字符串转换为类型。以以下两种情况为例: case class Product(id: String, title: String, description: String) type aType = Product client.getProduct[aType](id, "/products").map { x => println(s"Retrieved Product with id '$id': $x") } // Working // Retrieved P
case class Product(id: String, title: String, description: String)
type aType = Product
client.getProduct[aType](id, "/products").map { x => println(s"Retrieved Product with id '$id': $x") } // Working
// Retrieved Product with id 'test_id': Some(Product(test_id,MyTitle,The text of my Product))
case class Store(id: String, title: String, Address: String)
type aType = Store
client.getStore[aType](id, "/stores").map { x => println(s"Retrieved Store with id '$id': $x") } // working
// Retrieved Store with id 'test_id': Some(Store(test_id, Store Name,The address of my Store))
我想让这段代码对任何请求都通用,因为已经定义了案例类。比如说
case class Product(id: String, title: String, description: String)
case class Store(id: String, title: String, Address: String)
case class API_Detail(Name: String, CaseClassName: String, Api_Url:String)
var API_List = List[API_DS]()
val request_type = "Product" // or "Store"
val id = "test_id"
val API_List_Item = API_List.filter(_.Name == request_type)
// Want to do like this...
type aType = API_List_Item.CaseClassName.toType /**/
val RequestURL = API_List_Item.Api_Url
/* Interested to know how to convert string to type. To my knowledge
some form of reflection will be implemented. */
client.getRespone[aType](id, RequestURL).map { x => println(s"Retrieved $request_type with id '$id': $x") } // Working
// Retrieved Product with id 'test_id': Some(Product(test_id,MyTitle,The text of my Product))
谢谢你邀请我来这里。也许您可以通过使用模式匹配来保持解决方案足够简单。使用隐式类的另一种解决方案也是有效的
val requestUrl = "/products"
requestUrl match {
case "/products" => Products(...)
case "/store" => Store(...)
case _ => UnknownType
}
有关模式匹配的其他示例,请参阅我的教程,网址为,我将扩展我先前关于模式匹配的回答。如果我们有一个我正在使用的模式,就没有必要重新模式匹配。我还提供了关于类型类的构建块的详细教程:traits、implicits和函数 您需要展开getResponse(…)来构建和填充具体类型的字段
case class Product(id: String, title: String, description: String)
case class Store(id: String, title: String, address: String)
trait Client[T] {
def getResponse(s: String): Option[T]
}
object Client {
implicit val productClient: Client[Product] = new Client[Product] {
override def getResponse(s: String): Option[Product] = s match {
case "/product" => Some(Product("id", "title", "description"))
case _ => None
}
}
implicit val storeClient: Client[Store] = new Client[Store] {
override def getResponse(s: String): Option[Store] = s match {
case "/store" => Some(Store("id", "title", "address"))
case _ => None
}
}
def apply[T : Client](s: String): Option[T] =
implicitly[Client[T]].getResponse(s)
}
val product: Option[Product] = Client[Product]("/product")
val store: Option[Store] = Client[Store]("/store")
这里的产品是案例类。案例类产品(id:String,title:String,description:String)我认为这是不可能的,但向我们展示更大的上下文,我们也许能够解决它。例如,
响应
应该如何进一步使用?该代码将被调用给谁?您希望能够访问response.id
(和其他字段)吗?如果是,编译器如何确保(在编译时)响应
的类型是产品
,而您只想在运行时提供实际类型?谢谢您的回复。我试图详细说明我的要求,但由于255个字符的限制而失败。我在下面的链接中创建了一个新问题。请看一下,帮我解决一下。提前谢谢。瓦卡斯,你,作为问题的作者。可以对其进行编辑以提供更多详细信息。你不必在每一条有价值的评论后都提出新问题:)。谢谢你,塞格。我已经更新了我的需求。这里的问题是match
语句返回类型<问题中定义的code>Product和Store
是不相关的类型,因此语句返回类型基本上没有意义。您必须对其进行强制转换,或对其重新进行模式匹配,才能从中获得有用的产品
或存储
。