Tag Info

New answers tagged

0

While C# does not allow a type to have multiple constructors except by giving them all different signatures, types can have any number of factory methods. My inclination would be to have a protected constructor with a "noTricks" parameter, but have consumers of the class use factory methods to construct instances. Tricky and non-tricky methods can then be ...


0

Interestingly, the answer to this question will depend on the language. Specifically, there is an interaction between overloading and generic programming (*), and depending on how generic programming is implemented it might be just syntactic sugar (Rust) or absolutely necessary (C++). That is, when generic programming is implemented with explicit ...


2

If the dispatch is resolved at compile time, depending only on the static type of the argument expression, then you can certainly argue that it's "syntactic sugar" replacing two different methods with different names, provided that the programmer "knows" the static type and could just use the right method name in place of the overloaded name. It is also a ...


2

It really depends by your defintion of "syntactic sugar". I'll try to address some of the definitions that come to my mind: A feature is syntactic sugar when a program that uses it can always be translated in an other that doesn't use the feature. Here we are assuming that there exist a primitive set of features that cannot be translated: in other words ...


0

In Java type information is compiled in and which of the overloads is called is decides at compile time. The following is a snippet from sun.misc.Unsafe (the utility for Atomics) as viewed in Eclipse's class file editor. // Method descriptor #143 (Ljava/lang/Object;I)I (deprecated) // Stack: 4, Locals: 3 @java.lang.Deprecated public int ...


0

I think it is simple syntactic sugar in most languages (at least all I know...) since they all require an unambigous Function-call at compile time. And the compiler simply replaces the function call with an explicit pointer to the right implementation signature. Example in Java: String s; int i; mangle(s); // Translates to CALL ('mangle':LString):(s) ...


7

Depending on the language, it is syntactic sugar or not. In C++ for instance, you can do things using overloading and templates which would not be possible without complications (write manually all instantiations of the template or add a lot of template parameters). Note that dynamic dispatch is a form of overloading, dynamically resolved on some ...


1

It looks like "syntactic sugar" sounds derogatory, like useless or frivolous. That is why the question triggers many negative answers. But you are right, method overloading doesn't add any feature to the language except for the possibility to use the same name for different methods. You can make the parameter type explicit, the program will still work the ...


0

In some languages it is no doubt merely syntactic sugar. However, what it is a sugar of depends on your point of view. I'll leave this discussion for later in this answer. For now I'd just like to note that in some languages it is certainly not syntactic sugar. At least not without requiring you to use a completely different logic/algorithm to implement the ...


8

Given that name-mangling works, doesn't it have to be nothing more than syntactic sugar? It allows the caller to imagine he is calling the same function, when he isn't. But he could know the real names of all his functions. Only if it were possible to achieve delayed polymorphism by passing an untyped variable into a typed function and have its type ...


12

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()) { ...


5

For contemporary languages, it's just syntactic sugar; in a completely language-agnostic sort of way, it's more than that. Previously this answer stated simply that it's more than syntactic sugar, but if you'll see in the comments, Falco raised the point that there was one piece of the puzzle that contemporary languages appear to all be missing; they don't ...


25

To answer this, you first need a definition for "syntactic sugar." I'll go with Wikipedia's: In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...


0

Elaborating on @back2dos answer, there are two things going on in your code. loading words into a List based on some filters parsing words (in this case, from a file, splitting on new lines) So I'd vote for two methods. Note - I'm a Java guy so I'm guessing about the types... void filterWords(IEnumerable<string> rawwords) // note - why return ...


5

This violates at least three principles of object-oriented programming, and potentially a fourth. First, this violates the Principle of Least Astonishment. Let's assume we are given documentation for only one of the methods, and we have to infer what the other does. First: // Reads words from a file public void LoadWords(string) // ??? public void ...


10

I really wonder why the version with actual IO has been added to the class in the first place. What's the point here? How is that the responsibility of the class? Why not add a method to load the words from a zipped file, or from an .xml or a .json or a .doc or an SQL database or over http or ftp or whatever? Because it's not the responsibility of the ...



Top 50 recent answers are included