由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - scala的def或val是冗余的
相关主题
请问static variable init的问题?总是搞不清c++里面函数前头和后头的const...
go is ugly一个scala future的实例
PNWScala 2014Scala的思路
Re: 劈柴进狗狗是靠刷题么? (转载)scala 的future 和 promise有机制防止dead lock吗?
Python 可不可以一次读数据给一个 web service 后,然后一直用这个数据scala 问题请教 -- scala.runtime.Tuple2Zipped
golang 一个thread safe singleton问题The C++ questions I've been asked
int (*b)[2]; 是什么意思?[合集] can C++ constructor be private? the answer is YES!
有几个人能 背着把gof 23种pattern 用java 随便写5,6个写出来C++ Singleton Help
相关话题的讨论汇总
话题: def话题: int话题: val话题: scala
进入Programming版参与讨论
1 (共1页)
w***g
发帖数: 5958
1
如果都按def写
常数 def three = 3
函数 def inc (x: Int) = (x+1)
常数作为一种0元函数,语法很一致
如果都按val写
常数 val three = 3
函数 val inc = (x:Int) => (x+1)
函数的情况下比def多敲一个字母,好歹也是一致的。
不知道为什么要出来两个关键字。上面这些都可以正常运行,结果也一样。
还有下面的变态语法
{ i: Int =>
println("hello world")
i * 2
}
明明因该是
(i:Int)=>{println("Hello, world!");2}
然后_和curried也有冗余
_: def add (a:Int, b:Int) = (a+b)
def add_currie = (add(5, _:Int))
currie: (这种写法更加一般性,可以currie多次,但是傻逼语法非要在括号后加个_)
def add (a:Int)(b:Int) = (a+b)
def add_currie = add(5)_
Peking2我觉得我们应该统一一个本版标准scala语法,反对不能带来任何方便的语法糖。
l**********n
发帖数: 8443
2
Inside a class, val is evaluated on initialization while def is evaluated
only when, and every time, the function is called.
N*****m
发帖数: 42603
3
这都无所谓,主要是operator overloading太多了
一个不熟的库就看着头疼

【在 w***g 的大作中提到】
: 如果都按def写
: 常数 def three = 3
: 函数 def inc (x: Int) = (x+1)
: 常数作为一种0元函数,语法很一致
: 如果都按val写
: 常数 val three = 3
: 函数 val inc = (x:Int) => (x+1)
: 函数的情况下比def多敲一个字母,好歹也是一致的。
: 不知道为什么要出来两个关键字。上面这些都可以正常运行,结果也一样。
: 还有下面的变态语法

w***g
发帖数: 5958
4
大牛你给解释下为什么下面的代码不是语法错误
def inc (x:Int) {x+1}

【在 l**********n 的大作中提到】
: Inside a class, val is evaluated on initialization while def is evaluated
: only when, and every time, the function is called.

l**********n
发帖数: 8443
5
The compiler can infer the return type from what is being returned but it
can’t infer the types of the parameters so we’ll have to leave those
explicit specifications in.
We can however remove the return keyword to further reduce the amount of
ceremony in the method.
If we don’t use the return statement, the compiler will just assume that we
want to return the value of the last statement.
There's another consequence: in a subclass, you can override a def with a
val or a def, but you can only override a val with a val.
The consequences of this decision are: if you include the parentheses in the
definition, then the property can be accessed with or without parentheses.
If you do not include parentheses in the definition, then the property must
be accessed without parentheses.
This is very bad design consideration for Scala. very confusing
p*****2
发帖数: 21240
6

大牛这些哪里看来的呀?我看着很乱。

【在 w***g 的大作中提到】
: 如果都按def写
: 常数 def three = 3
: 函数 def inc (x: Int) = (x+1)
: 常数作为一种0元函数,语法很一致
: 如果都按val写
: 常数 val three = 3
: 函数 val inc = (x:Int) => (x+1)
: 函数的情况下比def多敲一个字母,好歹也是一致的。
: 不知道为什么要出来两个关键字。上面这些都可以正常运行,结果也一样。
: 还有下面的变态语法

p*****2
发帖数: 21240
7

大牛这是哪里看来的呀?

【在 w***g 的大作中提到】
: 大牛你给解释下为什么下面的代码不是语法错误
: def inc (x:Int) {x+1}

w***g
发帖数: 5958
8
我是说没有=。不加等号认为是语法错误会出啥问题。

we

【在 l**********n 的大作中提到】
: The compiler can infer the return type from what is being returned but it
: can’t infer the types of the parameters so we’ll have to leave those
: explicit specifications in.
: We can however remove the return keyword to further reduce the amount of
: ceremony in the method.
: If we don’t use the return statement, the compiler will just assume that we
: want to return the value of the last statement.
: There's another consequence: in a subclass, you can override a def with a
: val or a def, but you can only override a val with a val.
: The consequences of this decision are: if you include the parentheses in the

l**********n
发帖数: 8443
9
In Scala if a method declaration does not have an equal sign before its body
, the compiler infers that the result type will be Unit
http://stackoverflow.com/questions/944111/when-to-use-the-equal
远离scala。珍惜脑细胞

【在 w***g 的大作中提到】
: 我是说没有=。不加等号认为是语法错误会出啥问题。
:
: we

p*****2
发帖数: 21240
10

body
我刚学scala的时候,认真的问了两个问题,然后就累了。以后差不多就算了。

【在 l**********n 的大作中提到】
: In Scala if a method declaration does not have an equal sign before its body
: , the compiler infers that the result type will be Unit
: http://stackoverflow.com/questions/944111/when-to-use-the-equal
: 远离scala。珍惜脑细胞

相关主题
golang 一个thread safe singleton问题总是搞不清c++里面函数前头和后头的const...
int (*b)[2]; 是什么意思?一个scala future的实例
有几个人能 背着把gof 23种pattern 用java 随便写5,6个写出来Scala的思路
进入Programming版参与讨论
w***g
发帖数: 5958
11
返回unit像下面这么写有任何问题吗?
def ReturnUnit () = ()
def GreetAndReturnUnit () = {println("Hello,world!");()}
为了省个等号连原则都不要了。这种明明是错的东西,唉,我终究还是不能免俗。

body

【在 l**********n 的大作中提到】
: In Scala if a method declaration does not have an equal sign before its body
: , the compiler infers that the result type will be Unit
: http://stackoverflow.com/questions/944111/when-to-use-the-equal
: 远离scala。珍惜脑细胞

p*****2
发帖数: 21240
12

大牛觉得haskell怎么样呀

【在 w***g 的大作中提到】
: 返回unit像下面这么写有任何问题吗?
: def ReturnUnit () = ()
: def GreetAndReturnUnit () = {println("Hello,world!");()}
: 为了省个等号连原则都不要了。这种明明是错的东西,唉,我终究还是不能免俗。
:
: body

c******o
发帖数: 1277
13
def 的每次调用都会重计算。
val 的只会在定义的时候计算。
lazy val 的只会在第一次用的时候计算。
它们是严格不同的。也很有用。
w***g
发帖数: 5958
14
这么说就确实是有道理的。纯函数语言像haskell这种是没有变量的,我先入为主了。

【在 c******o 的大作中提到】
: def 的每次调用都会重计算。
: val 的只会在定义的时候计算。
: lazy val 的只会在第一次用的时候计算。
: 它们是严格不同的。也很有用。

z****e
发帖数: 54598
15
scala有不少概念是j2ee上比较熟悉的概念呵呵
比如object是singleton
case class就是dto
z****e
发帖数: 54598
16
对j2ee core patterns熟悉的话
scala倒是没那么困难了
lazy init在singleton那个模式里面也被讨论烂了
spring和guice的差异也类似
scala把这个做到了jvm level上去
蛮好,蛮好
z****e
发帖数: 54598
17
无论是scala还是spark
对于java程序员来说也都是很熟悉的东西
包括maven的使用
如果连gradle这种都搞不定的话
那要补的课就多了
啥时候rod johnson用scala重构一下spring
做一个summer出来就好了
z****e
发帖数: 54598
18
val用java的话说就是final
区别于var
c******o
发帖数: 1277
19
lazy val by reference和singleton不一样,内部可是用reflection的,可以做很奇妙
很复杂的东西,比如正反馈自循环的变量定义 :)
Haskell的人会比较熟悉,比如monadfix,arrowloop
val和final也不完全一样, scala 有final key word 的
z****e
发帖数: 54598
20
no
我说的不是lazy init==singleton
是说singleton里面有一种方式可以实现lazy init
具体点说是内部类
lazy init可以有很多种方式实现
现在能直接写出来,也省得去折腾了

【在 c******o 的大作中提到】
: lazy val by reference和singleton不一样,内部可是用reflection的,可以做很奇妙
: 很复杂的东西,比如正反馈自循环的变量定义 :)
: Haskell的人会比较熟悉,比如monadfix,arrowloop
: val和final也不完全一样, scala 有final key word 的

相关主题
scala 的future 和 promise有机制防止dead lock吗?[合集] can C++ constructor be private? the answer is YES!
scala 问题请教 -- scala.runtime.Tuple2ZippedC++ Singleton Help
The C++ questions I've been asked[合集] which design pattern is used if a static variable insid
进入Programming版参与讨论
c******o
发帖数: 1277
21
这么说吧,用monadfix/arrowloop structure,我可在一行 没loop,没recursive的定义
里计算平方根。
靠的就是 lazy value,自循环的牛顿迭代法。我只用写定义就可以,他会自己算。
本质上是和ee control里的负反馈循环稳定控制电路一样的。

【在 z****e 的大作中提到】
: no
: 我说的不是lazy init==singleton
: 是说singleton里面有一种方式可以实现lazy init
: 具体点说是内部类
: lazy init可以有很多种方式实现
: 现在能直接写出来,也省得去折腾了

z****e
发帖数: 54598
22
你觉得我在否认你说的这些东西么?
我没说singleton就是lazy init或者反过来
我只是说在弄singleton的时候遇到过
这只是一个definition,明白了记住了就好了
至于具体的应用,那要看写的人怎么用了

【在 c******o 的大作中提到】
: 这么说吧,用monadfix/arrowloop structure,我可在一行 没loop,没recursive的定义
: 里计算平方根。
: 靠的就是 lazy value,自循环的牛顿迭代法。我只用写定义就可以,他会自己算。
: 本质上是和ee control里的负反馈循环稳定控制电路一样的。

z****e
发帖数: 54598
23
其实就是void
没有=表示void
Unit就是java的void
这样就不会死脑细胞了
The type of this function is written () => Unit and is the type of all
functions which take no arguments and return nothing (the type Unit is
similar to void in C/C++).

body

【在 l**********n 的大作中提到】
: In Scala if a method declaration does not have an equal sign before its body
: , the compiler infers that the result type will be Unit
: http://stackoverflow.com/questions/944111/when-to-use-the-equal
: 远离scala。珍惜脑细胞

l**********n
发帖数: 8443
24
有道理

【在 z****e 的大作中提到】
: 其实就是void
: 没有=表示void
: Unit就是java的void
: 这样就不会死脑细胞了
: The type of this function is written () => Unit and is the type of all
: functions which take no arguments and return nothing (the type Unit is
: similar to void in C/C++).
:
: body

1 (共1页)
进入Programming版参与讨论
相关主题
C++ Singleton HelpPython 可不可以一次读数据给一个 web service 后,然后一直用这个数据
[合集] which design pattern is used if a static variable insidgolang 一个thread safe singleton问题
请问关于c++实现singleton的问题?int (*b)[2]; 是什么意思?
C++里最常用的design pattern都有哪些?有几个人能 背着把gof 23种pattern 用java 随便写5,6个写出来
请问static variable init的问题?总是搞不清c++里面函数前头和后头的const...
go is ugly一个scala future的实例
PNWScala 2014Scala的思路
Re: 劈柴进狗狗是靠刷题么? (转载)scala 的future 和 promise有机制防止dead lock吗?
相关话题的讨论汇总
话题: def话题: int话题: val话题: scala