当前位置:网站首页>Nine ways to define methods in scala- Nine ways to define a method in Scala?
Nine ways to define methods in scala- Nine ways to define a method in Scala?
2022-07-03 15:57:00 【fyin1314】
problem :
So I've been trying to puzzle through the various ways you can define stuff in Scala, complicated by my lack of understanding of the way {} blocks are treated: therefore , I have been trying to solve your problem in Scala Method of defining content in , And because I am {} The processing of blocks is complicated due to lack of understanding :
object NewMain extends Thing{ def f1 = 10 def f2 {10} def f3 = {10} def f4() = 10 def f5() {10} def f6() = {10} def f7 = () => 10 def f8 = () => {10} def f9 = {() => {10}} def main(args: Array[String]){ println(f1) // 10 println(f2) // () println(f3) // 10 println(f4) // 10 println(f4()) // 10 println(f5) // () println(f5()) // () println(f6) // 10 println(f6()) // 10 println(f7) // <function0> println(f7()) // 10 println(f8) // <function0> println(f8()) // 10 println(f9) // <function0> println(f9()) // 10 }}Presumably some of these are equivalent, some of these are syntactic sugar for others, and some are things I should not use, but I can't for the life of me figure it out. Presumably , Some of them are equivalent , Some of them are other people's grammatical sugar , Some things I shouldn't use , But I can't figure it out for my life .My specific questions are: My specific question is :
How is it that
println(f2)andprintln(f5())givesunit?println(f2)andprintln(f5())giveunit?Isn't the last item in the block10? No10The last item of the block ?How is it different fromprintln(f3()), which gives10? It is associated withprintln(f3())What's the difference , It gives10?If
println(f5)givesunit, shouldn'tprintln(f5())be invalid, sinceunitis not a function? Ifprintln(f5)giveunit, Should not beprintln(f5())Invalid , becauseunitNot a function ?The same applies toprintln(f6)andprintln(f6())This also applies toprintln(f6)andprintln(f6())Of all the ones which print 10:
f1,f3,f4,f4(),f6,f6(),f7(),f8(),f9(), is there any functional difference between them (in terms of what it does) or usage differences (in terms of when I should use which)? Print in all 10:f1,f3,f4,f4(),f6,f6(),f7(),f8(),f9(), Are there any functional differences between them ( In terms of its function ) ) Or use difference ( In terms of when to use )?Or are they all equivalent? Or they are all the same ?
Solution :
Reference resources : https://stackoom.com/en/question/YqCX边栏推荐
- Wechat payment -jsapi: code implementation (payment asynchronous callback, Chinese parameter solution)
- QT use qzxing to generate QR code
- Detailed pointer advanced 2
- How idea starts run dashboard
- Custom annotation
- The difference between RAR and zip files
- 详解指针进阶2
- App移动端测试【5】文件的写入、读取
- 软件逆向破解入门系列(1)—xdbg32/64的常见配置及功能窗口
- Tensorflow realizes verification code recognition (II)
猜你喜欢

Principles of several common IO models

Creation and destruction of function stack frames

Seckill system 2 redis solves the problem of distributed session

Unity功能——Unity离线文档下载及使用

Mongodb installation and basic operation

Microservice API gateway

Reading notes of "micro service design" (Part 2)

“用Android复刻Apple产品UI”(3)—优雅的数据统计图表

从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍

Halcon and WinForm study section 2
随机推荐
Redis在Windows以及Linux系统下的安装
Mongodb installation and basic operation
“用Android复刻Apple产品UI”(3)—优雅的数据统计图表
找映射关系
Go language self-study series | if else if statement in golang
MongoDB 的安装和基本操作
Large CSV split and merge
Semi supervised learning
Calibre LVL
阿飞的期望
WinDbg analysis dump file
互斥对象与临界区的区别
Persisting in output requires continuous learning
Win32 create window and button (lightweight)
Visual upper system design and development (Halcon WinForm) -2 Global variable design
Seckill system 2 redis solves the problem of distributed session
Second kill system 3 - list of items and item details
嵌入式开发:避免开源软件的7个理由
《微服务设计》读书笔记(上)
Embedded development: seven reasons to avoid open source software