risc-v中文社区

 找回密码
 立即注册
查看: 1185|回复: 0

[经验] typeof和ClassTag使用例子

[复制链接]

347

主题

564

帖子

2237

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2237
发表于 2021-8-25 09:09:14 | 显示全部楼层 |阅读模式
import scala.reflect._
import scala.reflect.runtime.universe._

object PartialFunctionTest {
  def main(args: Array[String]): Unit = {
    val tuple = ("name",10,"wuhan")

    def sum(a:Int,b:Int) = a + b

    val s = sum _
    val s1 = s(_:Int,3)
    val s2 = s1(10)
    val s1_1 = s(_:Int,6)
    val s2_2 = s1_1(10)
    def getType[T](x:T)(implicit evide:TypeTag[T]) = typeOf[T]
    println(s.getClass)                                                                //class com.joe.stu.PartialFunctionTest$$anonfun$1
    println(s1.getClass)                                                        //class com.joe.stu.PartialFunctionTest$$anonfun$2
    println(s2.getClass)                                                        //int
    println(s2_2.getClass)                                                        //int
    println(s2)                                                           //13
    println(s2_2)                                                                            //16
    println(ClassTag(s1.getClass()))                                //com.joe.stu.PartialFunctionTest$$anonfun$2
    println(typeTag[s1.type ])                                                //TypeTag[s1.type]
    println(typeOf[s1.type ])                                                //s1.type
    println(getType(s1))                                                        //Int => Int
    println(getType(s))                                                                //(Int, Int) => Int
    println(getType(s1).equals(getType(s1_1)))                //true        
    println(getType(s2_2))                                                        //Int
    println(getType(s2_2).equals(getType(s2)))                //true        
  }
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



Archiver|手机版|小黑屋|risc-v中文社区

GMT+8, 2024-4-29 06:49 , Processed in 0.014753 second(s), 17 queries .

risc-v中文社区论坛 官方网站

Copyright © 2018-2021, risc-v open source

快速回复 返回顶部 返回列表