syntactic-sugar

Syntactic sugar vs. feature

In C# (and Java) a string is little more than a char array with a stored length and a few methods tacked on. Likewise, (reference vs. value stuff aside) objects are little more than glorified structs with inheritance and interfaces added. On one level, these additions feel like clear features and enhancements unto themselves. On another...

WITH statement in Java

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! ...

Abbreviation for PHP's array()

I don't know how about you, but I'm not very fond of the way arrays are constructed in PHP. I have this feeling that I use array keyword way too often and that array($k => $v) or e.g. array($k1=>array($k2=>$v)) are way too long given usefulness of maps. (Moreover, recently I've learned JS way of doing it and now I really am jealous) The...

Code Generation based on method Attributes

I was reading through some articles on Caching and Memoization and how to implement it easily using delegates and generics. The syntax was pretty straightforward, and it is surprisingly easy to implement, but I just feel due to the repetitive nature it should be possible to generate code based on an Attribute, instead of having to write ...

Syntactic sugar for compile-time object creation in Scala

Lets say I have trait fooTrait[T] { def fooFn(x: T, y: T) : T } I want to enable users to quickly declare new instances of fooTrait with their own defined bodies for fooFn. Ideally, I'd want something like val myFoo : fooTrait[T] = newFoo((x:T, y:T) => x+y) to work. However, I can't just do def newFoo[T](f: (x:T, y:T) => T) =...

sql: BETWEEN v1 AND v2

Is there a difference in the order of v1 and v2 in a BETWEEN query on SQL Server? SELECT * FROM table WHERE col BETWEEN v1 AND v2 currently I don’t get any results if v1 is bigger than v2. Is this only syntactic sugar for col >= v1 AND col <= v2 or does it really take all values between the two? on my current observations I gues...

How useful is C#'s ?? operator?

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 i...

JavaScript equivalent of Python's __setitem__

var obj = {} obj.__setitem__ = function(key, value){ this[key] = value * value } obj.x = 2 // 4 obj.y = 3 // 9 JavaScript doesn't have __setitem__ and this example obviously doesn't work. In python __setitem__ works like: class CustomDict(dict): def __setitem__(self, key, value): super(CustomDict, self).__setitem__(key, val...

What does Brendan Eich mean when he speaks about adding sugar and macros to JavaScript?

I'm currently reading Coders at Work, and I'm at the chapter interviewing Brendan Eich. It's a little dense compared to the preceding chapters, to say the least. Around page 144, he talks about adding sugar and macros to JavaScript. What do these terms mean? Why is there backlash against adding them? ...

C# Dictionary: Each Key Has An Identical Value - Can I Remove the Redundancy?

Consider the following code, where each key has an identical value: IDictionary<string, string> quarterbackDictionary = new Dictionary<string, string>(); quarterbackDictionary.Add("Manning", "Manning"); quarterbackDictionary.Add("Brady", "Brady"); quarterbackDictionary.Add("Rivers", "Rivers"); My question: Can I remove the redu...

C# multiple arguments in one to DRY out parameter-passing

I don't know if this is possible, but in some of my unit tests, I end up initializing different objects with the same arguments. I would like to be able to store those arguments in some variable and just initialize the multi-parameter object constructor with that variable so instead of doing: Thing thing1 = new Thing(arg1, arg2, arg3, ...

C++: A way to declare a variable (or more than one) in an if statement that separates out the variable definition and the test?

One can do this: case WM_COMMAND: if (WORD wNotifyCode = HIWORD(wparam)) { ... } And one can do this: case WM_COMMAND: { WORD wNotifyCode = HIWORD(wparam); if (wNotifyCode > 1) { ... } } But one cannot do: case WM_COMMAND: if ((WORD wNotifyCode = HIWORD(wparam)) > 1) { ... } Using a for statement here I think is mi...

Magic First and Last Indicator in a Loop in Ruby/Rails?

Ruby/Rails does lots of cool stuff when it comes to sugar for basic things, and I think there's a very common scenario that I was wondering if anyone has done a helper or something similar for. a = Array.new(5, 1) a.each_with_index do |x, i| if i == 0 print x+1 elsif i == (a.length - 1) print x*10 els...

Operators and methods in Ruby

Most things that look like operators are methods in Ruby; 1 + 2 is syntactic sugar for 1.+(2). Even though + and * are methods that a program can redefine, Ruby has special magic to evaluate 1 + 2 * 3 as 1.+(2.*(3)) instead of 1.+(2).*(3). I wonder where this special magic lives in Ruby--if it is hard-wired into the interpreter. Ari. ...

PHP: Call a method on a returned class

I have a method which returns a class and want to call a method on it. Instead of $theClass = $this->getClass(); $theClass->foo(); I would like to write $this->getClass()->foo(); Is there a syntax for this as of PHP4? This works: $this->{$this->getClassName()}->foo(); But I would like to manipulate the class beforehand (I do th...

Custom ActionScript classes: Syntax sugar

Because of some limitations (example) in the built-in ActionScript 3 Dictionary class I'm looking to build a wrapper which adds such things. Is it possible to keep the syntax below for my custom class, and if so how? var dic:MyDic = new MyDic(); dic[stuffy] = someObject; ...

How would one go about adding (minor) syntactic sugars to Java?

Suppose I want to add minor syntactic sugars to Java. Just little things like adding regex pattern literals, or perhaps base-2 literals, or multiline strings, etc. Nothing major grammatically (at least for now). How would one go about doing this? Do I need to extend the bytecode compiler? (Is that possible?) Can I write Eclipse plugin...

Java for each vs regular for -- are they equivalent?

Are these two constructs equivalent? char[] arr = new char[5]; for (char x : arr) { // code goes here } Compared to: char[] arr = new char[5]; for (int i = 0; i < arr.length; i++) { char x = arr[i]; // code goes here } That is, if I put exactly the same code in the body of both loops ...

Why would you use "AS" when aliasing a SQL table?

I just came across a SQL statement that uses AS to alias tables, like this: SELECT all, my, stuff FROM someTableName AS a INNER JOIN someOtherTableName AS b ON a.id = b.id What I'm used to seeing is: SELECT all, my, stuff FROM someTableName a INNER JOIN someOtherTableName b ON a.id = b.id I'm assuming there's no difference ...

What are all the instances of syntactic sugar in Scala?

I decided to create this question to have a single source for all things syntactic sugar in Scala. I feel these details are some of the things most frustrating to starting users and 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...