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。珍惜脑细胞
|
|
|
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 的
|
|
|
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
|