Skip to main content
Mod Moved Comments To Chat
added 160 characters in body
Source Link
Winston Ewert
  • 25.1k
  • 13
  • 76
  • 104

The term syntactic sugar typically refers to cases where the feature is defined by a trivial substitution. So theThe language doesn't define what a feature does, instead it defines what itsthat it is exactly equivalent to something else. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But its not quite as trivial, and itsit is not how java definesequivalent to that. Within the featuremodel of Java, this is something different. Methodfoo(int a) doesn't implement a foo_int function to be created. Java doesn't implement method overloading isn't defined as calling methods with specialby giving ambiguous functions funny names. Yes, that's equivalent in some wayTo count as syntactic sugar, java would have to be pretending that you really wrote foo_int and foo_double functions but that's simply not how the feature is definedit doesn't.

The term syntactic sugar typically refers to cases where the feature is defined by a trivial substitution. So the language doesn't define what a feature does, instead it defines what its equivalent to. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But its not quite as trivial, and its not how java defines the feature. Method overloading isn't defined as calling methods with special names. Yes, that's equivalent in some way, but that's simply not how the feature is defined.

The term syntactic sugar typically refers to cases where the feature is defined by a substitution. The language doesn't define what a feature does, instead it defines that it is exactly equivalent to something else. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But it is not equivalent to that. Within the model of Java, this is something different. foo(int a) doesn't implement a foo_int function to be created. Java doesn't implement method overloading by giving ambiguous functions funny names. To count as syntactic sugar, java would have to be pretending that you really wrote foo_int and foo_double functions but it doesn't.

deleted 147 characters in body
Source Link
Winston Ewert
  • 25.1k
  • 13
  • 76
  • 104

The term syntactic sugar typically refers to cases where the feature can be implementedis defined by a trivial substitution. So farthe language doesn't define what a feature does, instead it defines what its equivalent to. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But, the transformation isn't its not quite as trivial. At the least, you have to determine the types of the parameters. Butand its more complicated then that:

void foo(Foo foo);
void foo(Bar bar);

foo(new Goat());

How do you translate that? You need to know about the possible overloads of foo,not how java defines the class hierarchy, etcfeature. Because thereMethod overloading isn't a trivialdefined as calling methods with special names. Yes, that's equivalent in some way to rewrite the syntax to get rid of, but that's simply not how the feature, method overloading isn't considered syntactic sugar is defined.

The term syntactic sugar typically refers to cases where the feature can be implemented by a trivial substitution. So far example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But, the transformation isn't trivial. At the least, you have to determine the types of the parameters. But its more complicated then that:

void foo(Foo foo);
void foo(Bar bar);

foo(new Goat());

How do you translate that? You need to know about the possible overloads of foo, the class hierarchy, etc. Because there isn't a trivial way to rewrite the syntax to get rid of the feature, method overloading isn't considered syntactic sugar.

The term syntactic sugar typically refers to cases where the feature is defined by a trivial substitution. So the language doesn't define what a feature does, instead it defines what its equivalent to. So for example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But its not quite as trivial, and its not how java defines the feature. Method overloading isn't defined as calling methods with special names. Yes, that's equivalent in some way, but that's simply not how the feature is defined.

Source Link
Winston Ewert
  • 25.1k
  • 13
  • 76
  • 104

The term syntactic sugar typically refers to cases where the feature can be implemented by a trivial substitution. So far example, for-each loops

for(Object alpha: alphas) {
}

Becomes:

for(Iterator<Object> iter = alpha.iterator(); iter.hasNext()) {
   alpha = iter.next();
}

Or take a function with variable arguments:

void foo(int... args);

foo(3, 4, 5);

Which becomes:

void Foo(int[] args);
 
foo(new int[]{3, 4, 5});

So there is a trivial substitution of syntax to implement the feature in terms of other features.

Let's look at method overloading.

void foo(int a);
void foo(double b);

foo(4.5);

This can be rewritten as:

void foo_int(int a);
void foo_double(double b);

foo_double(4.5);

But, the transformation isn't trivial. At the least, you have to determine the types of the parameters. But its more complicated then that:

void foo(Foo foo);
void foo(Bar bar);

foo(new Goat());

How do you translate that? You need to know about the possible overloads of foo, the class hierarchy, etc. Because there isn't a trivial way to rewrite the syntax to get rid of the feature, method overloading isn't considered syntactic sugar.