tag:blogger.com,1999:blog-7317245822288637185.post5935505579082999531..comments2023-03-28T10:19:42.467-07:00Comments on Graham Hacking Scala: New to Scala? Study the Syntax. CAREFULLY.Graham Leahttp://www.blogger.com/profile/07862939329898536954noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7317245822288637185.post-62060639747028122252010-10-28T07:35:43.551-07:002010-10-28T07:35:43.551-07:00Nothing in Scala returns nothing, but a function c...Nothing in Scala returns nothing, but a function can be declared to return Nothing! No contradiction here: such a function never returns. It either loops forever or throws an exception.<br /><br />[Nothing is the type with no instances. ]mgm7734https://www.blogger.com/profile/11833840237262028815noreply@blogger.comtag:blogger.com,1999:blog-7317245822288637185.post-50100288774471358332010-10-26T09:50:20.852-07:002010-10-26T09:50:20.852-07:00In scala, there aren't any methods that return...In scala, there aren't any methods that return nothing. The value that came out is of type Unit, and the only instance of Unit is (). You just happened to find a syntax shortcut for producing Unit.<br /><br />These are both the same:<br />def test(){}<br />def test() = ()<br /><br /><br />I really had a hard time understanding why this would make sense to anyone when I first started using scala, but when I started looking at the types of functions, and remembering a few pain points from using function references in C#, it all started to make sense.<br /><br />In c#, there are 2 ways to store a reference to a function, depending on whether or not the return type is void. There are 'Action' delegates, and there are 'Func' delegates.<br /><br />All of the 'Action' delegates have type parameters for the number of function parameters, and all of the 'Func' delegates only have type parameters for the number of function parameters, plus an additional type for the return type.<br /><br />So because 'void' isn't an actual type with an actual value in C#, there is no way to refer to the return type of a function that returns void.<br /><br />Scala avoids that completely, all functions return something, and there is only one set of interfaces for dealing with functions instead of 2. And to make things even better, someone can ask for a function that takes an int, and returns anything. For a horribly contrived example:<br /><br />scala> def times2(i:Int) = i*2<br />times2: (i: Int)Int<br /><br />scala> def intFunctionTest(func: Int => Any) = func(10)<br />intFunctionTest: (func: (Int) => Any)Any<br /><br />scala> intFunctionTest(times2)<br />res1: Any = 20<br /><br />scala> intFunctionTest(println)<br />10<br />res2: Any = ()<br /><br /><br />When I passed times2, it multiplied 10 by 2, then returned the value, which was then returned from intFunctionTest.<br /><br />When I passed println, it printed 10, then returned Unit.<br /><br />I could even have written my function like this:<br />def intFunctionTest[T](func: Int => T):T = func(10)<br /><br />and when I passed a function that returned an int, the result would have been an int instead of 'Any'Danhttps://www.blogger.com/profile/02192772456217563238noreply@blogger.comtag:blogger.com,1999:blog-7317245822288637185.post-82203108392375339302010-10-26T07:16:05.064-07:002010-10-26T07:16:05.064-07:00Many of us have suggested that the "def foo {...Many of us have suggested that the "def foo {" syntax (without the =) should be deprecated and removed for exactly this reason. Its intent was supposedly to make the transition from Java less challenging, but of course the added complexity makes for the exact opposite. The only argument in its favor is that once you're used to it, it makes it obvious what methods return Unit without requiring explicit annotation; since the return type is often inferred in idiomatic code, if all methods used the "=" syntax, it might be hard to see which were actually intended to return Unit. Personally, I never use this syntax and always explicitly annotate a return type of Unit and use the = syntax.Kris Nuttycombehttps://www.blogger.com/profile/06347383351250086727noreply@blogger.comtag:blogger.com,1999:blog-7317245822288637185.post-73691140350860661382009-12-16T13:18:08.901-08:002009-12-16T13:18:08.901-08:00Okay, your UPDATE clarifies why main() seems to wo...Okay, your UPDATE clarifies why main() seems to work without using '='.gshttps://www.blogger.com/profile/14786727820938299997noreply@blogger.com