当前位置:网站首页>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
? No10
The 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, sinceunit
is not a function? Ifprintln(f5)
giveunit
, Should not beprintln(f5())
Invalid , becauseunit
Not 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边栏推荐
- nifi从入门到实战(保姆级教程)——flow
- Detailed pointer advanced 1
- 大csv拆分和合并
- App mobile terminal test [4] APK operation
- Under VC, Unicode and ANSI are converted to each other, cstringw and std:: string are converted to each other
- App mobile terminal test [5] file writing and reading
- How idea starts run dashboard
- 秒杀系统2-Redis解决分布式Session问题
- “用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
- Creation and destruction of function stack frames
猜你喜欢
"Remake Apple product UI with Android" (2) -- silky Appstore card transition animation
How are integer and floating-point types stored in memory
突破100万,剑指200万!
MongoDB 的安装和基本操作
nifi从入门到实战(保姆级教程)——flow
需要知道的字符串函数
QT use qzxing to generate QR code
Microservices - load balancing ribbon
Distributed task scheduling XXL job
Unityshader - materialcapture material capture effect (Emerald axe)
随机推荐
String functions that you need to know
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
Visual upper system design and development (Halcon WinForm) -6 Nodes and grids
半监督学习
Break through 1million, sword finger 2million!
几种常见IO模型的原理
Redis高可用与持久化
nifi从入门到实战(保姆级教程)——flow
Redis installation under windows and Linux systems
突破100万,剑指200万!
分布式事务(Seata) 四大模式详解
《微服务设计》读书笔记(上)
秒杀系统3-商品列表和商品详情
Go language self-study series | golang switch statement
Download and install common programs using AUR
Reading notes of "micro service design" (Part 2)
Distributed task scheduling XXL job
WinDbg分析dump文件
坚持输出需要不断学习
大csv拆分和合并