syntactic-sugar

What are all the instances of syntactic sugar in Scala?

本秂侑毒 提交于 2019-11-26 11:47:18
问题 What are all the instances of syntactic sugar in Scala? They are hard to search for since most/all of them are purely symbols and are thus hard to search for without knowing the name of the concept. TODO: Implicit conversions _ syntax for anonymous functions Other things I\'m forgetting 回答1: Basics: a b is equivalent to a.b . a b c is equivalent to a.b(c) , except when b ends in : . In that case, a b c is equivalent to c.b(a) . a(b) is equivalent to a.apply(b) This is why the following

C# property and ref parameter, why no sugar?

你。 提交于 2019-11-26 09:32:25
问题 I just ran across this error message while working in C# A property or indexer may not be passed as an out or ref parameter I known what caused this and did the quick solution of creating a local variable of the correct type, calling the function with it as the out / ref parameter and then assigning it back to the property: RefFn(ref obj.prop); turns into { var t = obj.prop; RefFn(ref t); obj.prop = t; } Clearly this would fail if the property doesn\'t support get and set in the current

How does Scala's apply() method magic work?

北战南征 提交于 2019-11-26 07:25:18
问题 In Scala, if I define a method called apply in a class or a top-level object, that method will be called whenever I append a pair a parentheses to an instance of that class, and put the appropriate arguments for apply() in between them. For example: class Foo(x: Int) { def apply(y: Int) = { x*x + y*y } } val f = new Foo(3) f(4) // returns 25 So basically, object(args) is just syntactic sugar for object.apply(args) . How does Scala do this conversion? Is there a globally defined implicit

How does the Java 'for each' loop work?

白昼怎懂夜的黑 提交于 2019-11-25 22:12:01
问题 Consider: List<String> someList = new ArrayList<String>(); // add \"monkey\", \"donkey\", \"skeleton key\" to someList for (String item : someList) { System.out.println(item); } What would the equivalent for loop look like without using the for each syntax? 回答1: for (Iterator<String> i = someIterable.iterator(); i.hasNext();) { String item = i.next(); System.out.println(item); } Note that if you need to use i.remove(); in your loop, or access the actual iterator in some way, you cannot use