syntactic-sugar

How useful is C#'s ?? operator?

喜你入骨 提交于 2019-11-27 00:59:36
问题 So I have been intrigued by the ?? operator, but have still been unable to use it. I usually think about it when I am doing something like: var x = (someObject as someType).someMember; If someObject is valid and someMember is null, I could do var x = (someObject as someType).someMember ?? defaultValue; but almost invariably I get into problems when someObject is null, and ?? doesn't help me make this any cleaner than doing the null check myself. What uses have you guys found for ?? in

Python decorator best practice, using a class vs a function

[亡魂溺海] 提交于 2019-11-26 23:57:52
问题 As I've understood it there are two ways to do a Python decorator, to either use the __call__ of a class or to define and call a function as the decorator. What's the advantages/disadvantages of these methods? Is there one preferred method? Example 1 class dec1(object): def __init__(self, f): self.f = f def __call__(self): print "Decorating", self.f.__name__ self.f() @dec1 def func1(): print "inside func1()" func1() # Decorating func1 # inside func1() Example 2 def dec2(f): def new_f(): print

Syntax sugar: _* for treating Seq as method parameters

江枫思渺然 提交于 2019-11-26 22:10:14
I just noticed this construct somewhere on web: val list = List(someCollection: _*) What does _* mean? Is this a syntax sugar for some method call? What constraints should my custom class satisfy so that it can take advantage of this syntax sugar? Kevin Wright Generally, the : notation is used for type ascription, forcing the compiler to see a value as some particular type. This is not quite the same as casting. val b = 1 : Byte val f = 1 : Float val d = 1 : Double In this case, you're ascribing the special varargs type. This mirrors the asterisk notation used for declaring a varargs parameter

Is foreach purely “syntactic sugar”?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 21:32:08
问题 The compiler compiles a foreach loop into something like a for loop when the foreach is used with an array. And the compiler compiles a foreach loop into something like a while loop when the foreach is used with an IEnumerable or IEnumerable<T> . So does this mean foreach is purely syntactic sugar ? Or is there anything sophisticated about it? Does the CLR know about foreach ? Is there anything specifically designed for foreach in the MSIL code? 回答1: It's purely syntactic sugar in that you

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

ぃ、小莉子 提交于 2019-11-26 19:42:42
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 conversion going on here, similar to the implicit type conversions in the Predef object (but different in

Is there a way to implement custom language features in C#?

谁都会走 提交于 2019-11-26 17:19:48
I've been puzzling about this for a while and I've looked around a bit, unable to find any discussion about the subject. Lets assume I wanted to implement a trivial example, like a new looping construct: do..until Written very similarly to do..while do { //Things happen here } until (i == 15) This could be transformed into valid csharp by doing so: do { //Things happen here } while (!(i == 15)) This is obviously a simple example, but is there any way to add something of this nature? Ideally as a Visual Studio extension to enable syntax highlighting etc. Microsoft proposes Rolsyn API as an

Scala single method interface implementation

安稳与你 提交于 2019-11-26 17:13:29
问题 Does Scala have any syntactic sugar to replace the following code: val thread = new Thread(new Runnable { def run() { println("hello world") } }) with something more like: val thread = new Thread(() => println("hello world")) in cases when the trait/interface needs only one method to be implemented? If not, is there any chance to have this feature in Scala in the future? It is especially useful when one deals with Java classes. I found a similar question asked three years ago: Generically

Getting the desugared part of a Scala for/comprehension expression?

佐手、 提交于 2019-11-26 15:57:20
Does anyone know how to get the (Scala part only) desugared translation of a for/comprehension expression before it actually tries to compile in the REPL (or compiler)? The only thing I've found so far is the compiler "-print" flag but that gives you the full Scala translation… As I already said in the other topic, scalac -print prints out scala code, not java. It translates all scala keywords that are not directly compatible with java to normal scala code. It is not possible to let the compiler translate only parts afaik. But basically a for-comprehension is always translated the same way. A

What is the best or most interesting use of Extension Methods you&#39;ve seen? [closed]

孤街醉人 提交于 2019-11-26 14:58:00
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . I'm starting to really love extension methods... I was wondering if anyone her has stumbled upon one that really blew their mind, or

WITH statement in Java

跟風遠走 提交于 2019-11-26 14:42:48
In VB.NET there is the WITH command that lets you omit an object name and only access the methods and properties needed. For example: With foo .bar() .reset(true) myVar = .getName() End With Is there any such syntax within Java? Thanks! No. The best you can do, when the expression is overly long, is to assign it to a local variable with a short name, and use {...} to create a scope: { TypeOfFoo it = foo; // foo could be any lengthy expression it.bar(); it.reset(true); myvar = it.getName(); } Perhaps the closest way of doing that in Java is the double brace idiom, during construction. Foo foo =