【Scala 2.13.5 基础】Method & Function 区别 2021_4_18

本文探讨了Scala中方法与函数的区别,包括它们在类中的角色、如何转换为彼此以及在函数式编程中的应用。文章指出,方法是类的一部分,而函数则是实现特定特质的对象,此外还介绍了函数在内存使用上的优势。
摘要由CSDN通过智能技术生成

引用

http://jim-mcbeath.blogspot.com/2009/05/scala-functions-vs-methods.html

Scala(Java) Method, is part of class
    consitute: name signture annotations bytecode

Scala Function, is complete object => class of a series of trait
    according to: numbers of argument [f0,f1,f2,...]

    As an instance of a class that implements one of these traits, a function object has methods.
    One of these methods is the apply method, which contains the code that implements the body of the function.
    Scala has special "apply" syntax: if you write a symbol name followed by an argument list in parentheses 
    (or just a pair of parentheses for an empty argument list),
    Scala will converts that into a call to the apply method for the named object. 

释意

方法 是 类的一部分,指代 方法签名、注释、字节码 集合体
函数 则是 一个完整的 scala object [实例化对象] => 实现 Scala trait 的 伴生object (trait Function0、Function1 根据函数参数数量区分)

函数对象具有一个明显的方法即是 apply 方法,当我们通过()使用 函数对象变量时 即是 调用该函数对象的 apply 方法

方法转换为函数

我们将 Method 视为 Function 时(将方法分配给变量),Scala 实际上创建一个 函数对象,apply 方法 内容设定成 调用该方法所在类的原始方法

坏处:定义函数对象并将其分配给实例变量比仅定义功能上等效的方法要消耗更多的内存,这是因为附加的实例变量和该函数的另一个对象实例的开销(object’s header)。
好处:Function 提供 Method 无法提供的 大量能力,在需要的场景下 这种额外内存的使用是值得的

	# 将 Method 转换/视为 Function (而不是 拿到一个值通过运行这个方法)
    val f2 = m1 _
    or
    val f2: Int => Int = m1

具体观测方法

    1.test.scala
        class test {
            def m1(x: Int): Int = x + 3
            val f1: Int => Int = (x: Int) => x + 3
        }
    2.scalac test.scala
        => test.class
    3.javap test.class
        public class test {
          public int m1(int);
          public scala.Function1<java.lang.Object, java.lang.Object> f1();
          public static final int $anonfun$f1$1(int);
          public test();
        }

区别

Function 特殊之处

	f1 & f2 是 实现 Function1 接口的生成类的实例变量
    每次分别将 Method 引用为 Function 时,Scala都会创建一个单独的对象
    scala> val f1 = (x:Int) => x+3
    val f1: Int => Int = $Lambda$1006/2031667192@17740dae
    scala> val f2 = m1 _
    val f2: Int => Int = $Lambda$1021/1472200981@411a5965
    
    f1.toString
    (f1 andThen f2)(2)
    f1 != f2 == true
    (m1 _).apply(3)

Method 特殊之处

    def m2[T](x:T): String = x.toString.substring(0,4)
    m2("abcdefg")
    m2(1234567)


    泛型是 Function/Lambda 较为复杂处理的地方
    class myfunc[T] extends Function1[T,String] {
        def apply(x:T) = x.toString.substring(0,4)
    }
    
    scala> val f5 = new myfunc[String]
    val f5: myfunc[String] = <function1>

    scala> val f6 = new myfunc[Int]
    val f6: myfunc[Int] = <function1>

一言以蔽之
方法是指代 Java 类的方法
函数是指代 实现 Scala triat FunctionX 的 object 对象
通常情况下 二者可以混叫,但在使用上 apply() 之便利 还是具有很大区别
函数式编程中 lambda/function函数对象 是强无敌的存在

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值