本文整理汇总了Scala中scala.reflect.runtime.universe.typeOf类的典型用法代码示例。如果您正苦于以下问题:Scala typeOf类的具体用法?Scala typeOf怎么用?Scala typeOf使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了typeOf类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ShapelessTest
//设置package包名称以及导入依赖的类
package com.nekopiano.scala.scalasandbox.types
object ShapelessTest {
def main(args: Array[String]): Unit = {
var hm = new HMap
hm.set("hoge", 1)
hm.set("fuga", "moge")
hm.get[Int]("hoge")
hm.get[String]("fuga")
hm.get[String]("hoge")
println(hm)
}
class HMap {
import scala.reflect.runtime.universe.{ typeOf, TypeTag, Type }
private var m = Map[String, (Type, _)]()
def set[T: TypeTag](k: String, v: T): Unit = { m += k -> (typeOf[T] -> v) }
def get[T: TypeTag](k: String): Option[T] = m.get(k) flatMap {
case (t, v) if t <:< typeOf[T] => Some(v.asInstanceOf[T])
case _ => None
}
}
}
示例2: InjectRuntimeGeneratorsTest
//设置package包名称以及导入依赖的类
package org.dama.datasynth.runtime.spark.passes
import org.dama.datasynth.executionplan.ExecutionPlan
import org.dama.datasynth.executionplan.ExecutionPlan.PropertyTable
import org.scalatest.{FlatSpec, Matchers}
import scala.reflect.runtime.universe.{TypeTag, typeOf}
class InjectRuntimeGeneratorsTest extends FlatSpec with Matchers {
"The source code for a boolean runtime property generator with a dependency " should " be correctly generated " in {
val size = ExecutionPlan.StaticValue[Long](1000)
val valueFloat = ExecutionPlan.StaticValue[Float](1.0f)
val generatorFloat = ExecutionPlan.PropertyGenerator[Float]("org.dama.datasynth.common.generators.property.dummy.DummyFloatPropertyGenerator",Seq(valueFloat),Seq())
val tableFloat = PropertyTable[Float]("float","property",generatorFloat,size)
val valueBoolean = ExecutionPlan.StaticValue[Boolean](true)
val generatorBoolean = ExecutionPlan.PropertyGenerator[Boolean]("org.dama.datasynth.common.generators.property.dummy.DummyBooleanPropertyGenerator",Seq(valueBoolean),Seq(tableFloat))
val tableBoolean = PropertyTable[Boolean]("boolean","property",generatorBoolean,size)
val generatorMap : Map[String,String] = Map( "boolean.property" -> "BOOLEANPROPERTY", "float.property" -> "FLOATPROPERTY")
val injectRuntimeGenerators = new InjectRuntimeGenerators(generatorMap)
val executionPlan = injectRuntimeGenerators.run(Seq(tableBoolean,tableFloat))
executionPlan(0) match {
case table : PropertyTable[[email protected]] if table.tag.tpe =:= typeOf[Boolean] => table.generator.className should be ("BOOLEANPROPERTY")
case _ => throw new RuntimeException("Table is not of type PropertyTable[Boolean]")
}
executionPlan(1) match {
case table : PropertyTable[[email protected]] if table.tag.tpe =:= typeOf[Float] => table.generator.className should be ("FLOATPROPERTY")
case _ => throw new RuntimeException("Table is not of type PropertyTable[Float]")
}
}
}
示例3: UnitConverterPresenter
//设置package包名称以及导入依赖的类
package com.stulsoft.pscalafx.demo1
import scala.reflect.runtime.universe.typeOf
import scalafx.application.{Platform, JFXApp}
import scalafx.Includes._
import scalafx.scene.Scene
import scalafx.scene.control.{ComboBox, TextField}
import scalafx.event.ActionEvent
import scalafxml.core.{DependenciesByType, FXMLView}
import scalafxml.core.macros.sfxml
import javafx.beans.binding.StringBinding
@sfxml
class UnitConverterPresenter(from: TextField,
to: TextField,
types: ComboBox[UnitConverter],
converters: UnitConverters) {
// Filling the combo box
for (converter <- converters.available) {
types += converter
}
types.getSelectionModel.selectFirst()
// Data binding
to.text <== new StringBinding {
bind(from.text.delegate, types.getSelectionModel.selectedItemProperty)
def computeValue(): String = types.getSelectionModel.getSelectedItem.run(from.text.value)
}
// Close button event handler
def onClose(event: ActionEvent) {
Platform.exit()
}
}
object ScalaFXML extends JFXApp {
val root = FXMLView(getClass.getResource("/fxml/unitconverter.fxml"),
new DependenciesByType(Map(
typeOf[UnitConverters] -> new UnitConverters(InchesToMM, MMtoInches))))
stage = new JFXApp.PrimaryStage() {
title = "Unit conversion"
scene = new Scene(root)
}
}
示例4: MainViewController
//设置package包名称以及导入依赖的类
package de.m7w3.signal.controller
import de.m7w3.signal.ApplicationContext
import scala.reflect.runtime.universe.{Type, typeOf}
import scalafx.Includes._
import scalafx.scene.Parent
import scalafxml.core.{DependenciesByType, FXMLView}
import scalafxml.core.macros.{nested, sfxml}
@sfxml
class MainViewController(@nested[EditMenuController] editMenuController: MenuController,
@nested[FileMenuController] fileMenuController: MenuController,
@nested[HelpMenuController] helpMenuController: MenuController,
applicationContext: ApplicationContext) {
}
object MainView {
def load(context: ApplicationContext): Parent = {
val dependencies = Map[Type, Any](
typeOf[ApplicationContext] -> context
)
val resourceUri = "/de/m7w3/signal/main_view.fxml"
val fxmlUri = getClass.getResource(resourceUri)
require(fxmlUri != null, s"$resourceUri not found")
FXMLView(fxmlUri, new DependenciesByType(dependencies))
}
}