Skip to main content

Questions tagged [operators]

Regarding programming languages, operators are constructs which behave generally like functions, but which differ syntactically or semantically from usual functions. From Wikipedia: http://en.wikipedia.org/wiki/Operator_%28programming%29

Filter by
Sorted by
Tagged with
3 votes
1 answer
1k views

What is the etymology of the "dot" operator for string concatenation?

Some languages have a . operator for string concatenation. The oldest language I could find that supports it is Perl. Was Perl the first to use it? Why was it chosen?
14 votes
2 answers
9k views

When should I use C#'s implicit type conversion operator?

In C#, we can overload the implicit conversion operator like this (example from MSDN): struct Digit { /* ... */ public static implicit operator byte(Digit d) // implicit digit to byte ...
1 vote
1 answer
2k views

What is the "->" symbol called?

I have seen the -> operator/symbol in Java 8 predicates recently and wondered what its name is. I know that it is used in lambda expressions, but I know that the symbol for lambda is λ, so that's ...
88 votes
15 answers
10k views

I don't understand the arguments against operator overloading [closed]

I just read one of Joel's articles in which he says: In general, I have to admit that I’m a little bit scared of language features that hide things. When you see the code i = j * 5; … in C you know, ...
0 votes
2 answers
262 views

Missing `$` like operators in Haskell

Below I've produced what I believe to be a summary of the standard $ like operators for various classes in Haskell. There's some gaps however. Following the applicative pattern, you would think those ...
3 votes
1 answer
820 views

Well defined mathematical operations for bearing (angle) class

I have written a class to represent bearings (angles with a nautical theme, and a specific normalisation range). In the program, it is necessary to perform some mathematical operations on them, so I'...
3 votes
5 answers
4k views

Is Operator Overloading supported in C

When I read about the LCC (Windows) compiler, I found out it has the implementation for operator overloading. However, after a bit of Googling, I've confirmed that operator overloading isn't ...
4 votes
2 answers
792 views

Implicit Conversion Operators for Value Types in C#

I’ve come across this blog article: Implicit Conversion Operators are Bad. The article discourages the use of implicit conversion with reference types. The article describes problems caused by ...
3 votes
1 answer
956 views

How do you set and check a single bit in a programming language that does not support bitwise operations?

I'm using a programming language (more a scripting language) that does not support any bitwise operators like AND, OR, XOR, NOT (and shift as well). Common arithmetic and logical operations like + - *...
0 votes
1 answer
237 views

Is it correct to call the assignment symbol an "operator" when it is actually a statement?

In some languages (C++, Java, Ruby, etc.) an assignment returns a value and can be used in an expression: x = (y = z); // ok, 'x' gets the value of 'z' In other languages (Ada, VHDL), an ...
5 votes
4 answers
2k views

Lexing: One token per operator, or one universal operator token?

When lexing, what would be the best way to tokenize operators? Would one just create a BinaryOperator token, or a separate token for every single binary operator? Examples: PlusOperator, MinusOperator,...
3 votes
3 answers
1k views

Specifics of Switch and If statements

Why do we need the switch statement if there is the if statement? Why can't we use several ifs like if(a==1) do this1; if(a==2) do this2; ... instead of switch (a) { case(1): {do ...
5 votes
2 answers
2k views

Why isn't exponentiation hardware-implemented?

Why is there no exponentiation operation in hardware, even though many languages have builtin operators for it? Is it because even hardware implementations would need to use the same algorithm as ...
22 votes
6 answers
32k views

What are the advantages of using bitwise operations? [closed]

Following reading the latest CodeProject newsletter, I came across this article on bitwise operations. It makes for interesting reading, and I can certainly see the benefit of checking if an integer ...
29 votes
10 answers
6k views

Is there any reason why most programming languages don't have '!>' (not greater than) and '!<' (not less than) operators?

I wonder if there is any reason - or if it is just an accident of history - that there are no !> and !< operators in most programming languages? a >= b (a greater OR equals b) could be ...
1 vote
2 answers
1k views

Should ** bind more tightly than !, ~?

Designing a programming language, I'm including the ** exponentiation operator. In Fortran and Python, the two languages I know of which have this operator, it binds more tightly than unary minus, ...
12 votes
4 answers
17k views

Instantiating Null Objects with Null-Coalescing Operator

Consider the following typical scenario: if(myObject == null) { myObject = new myClass(); } I'm wondering what is thought of the following replacement using the null-coalescing operator: ...
0 votes
3 answers
716 views

Any languages use the '=>' and '=<' operators? [closed]

I'm just curious if there are any programming languages that use the '=>' and '=<' operators for something. I wonder why these are not commonly used when their variant '<=' and '>=' are so ...
1 vote
1 answer
124 views

Operator distributivity in expressions - syntactic sugar

When writing boolean expressions out, you often have to use something of the type: A == any of B1, ..., Bn C < all of D1, ..., Dm But in code you have to use expanded forms: A == B1 or ... or A =...
0 votes
3 answers
747 views

Using Power of 2 numbers to represent types

Let's say that we have some values, represented by power of 2: TYPE_1 = 1 TYPE_2 = 2 TYPE_3 = 4 TYPE_4 = 8 ... I need to store some of these types in one value. Example: To represent TYPE_1 with ...
1 vote
1 answer
225 views

How does the ? make a quantifier lazy in regex [closed]

I've been looking into regex lately and figured that the ? operator makes the *,+, or ? lazy. My question is how does it do that? Is it that *? for example is a special operator, or does the ? have ...
2 votes
2 answers
988 views

Why do more languages not support ===

I mean it as it is used in JavaScript. Just curious why this is not supported in more languages? Like Java for example does not have it? Seems to be a very useful operator to have.
0 votes
2 answers
730 views

How to extract operators from the grammar productions for conflict resolution in LALR parser?

Is there some standardized or widely accepted algorithm for picking up operators in shift/reduce conflicts in LALR parser? The question is naive, my problem is not with implementing my solution, but ...
10 votes
2 answers
2k views

What is the justification for Python's power operator associating to the right?

I am writing code to parse mathematical expression strings, and noticed that the order in which chained power operators are evaluated in Python differs from the order in Excel. From http://docs....
20 votes
15 answers
5k views

What are bit operators good for? [closed]

Programming languages often come with various bit operators (e.g. bitwise left- and right shift, bitwise AND, OR, XOR...). These don't get used though very much, or at least such has my experience ...
0 votes
1 answer
428 views

Overloading Operators - C++

I was experimenting with new overloaded operators, I have created one void operator and another one that returns something when it's called: #include <iostream> struct chichachicha{ int ...
19 votes
3 answers
10k views

Why isn't the arrow operator in C++ just an alias of *.?

In c++, the * operator can be overloaded, such as with an iterator, but the arrow (->) (.*) operator does not work with classes that overload the * operator. I imagine that the preprocessor could ...
0 votes
1 answer
280 views

Is this common amongst many languages?

In C++, the integer variable bar evaluates to 1 in this code int foo = 0; int bar = ++foo; and 0 in this code int foo = 0; int bar = foo++; Is this behavior prevalent in most popular languages?
14 votes
7 answers
990 views

Are operators clearer to read than keywords or functions? [closed]

It's a bit subjective, but I'm hoping to get a clearer understanding of what factors make an operator clear to use vs obtuse and difficult. I've been considering language designs recently, and one ...
12 votes
11 answers
13k views

Examples of operator overloading, which make sense [closed]

While I learning C#, I found that, the C# supports operator overloading. I have problem with good example which: Make sense (ex. adding class named sheep and cow) Is not an example of concatenation ...
9 votes
4 answers
3k views

Using PHP's ternary operator with only two arguments

I was recently reviewing some of my code and noticed that in a fit of absent-mindedness, I'd left a structure like the following: $guid = empty($subscription->guid) ? : $subscription->guid; ...
7 votes
8 answers
3k views

Comparison of operator overloading abuse in different languages

A common argument against operator overloading is that it can be and is abused, e.g. A+B doing something completely different to addition. Examples are often quoted in C++ snippets, where the ...
2 votes
4 answers
251 views

What should I call the less-than and similar operators?

Most programming languages support operators similar to the following 6, which are spelled here as in C. == != < <= > >= I'm aware of several terms used to include all of these ...
13 votes
3 answers
3k views

How useful are infix operators in a programming language?

How useful are infix operators in a programming language? Are they worth the extra complexity they provide? Can you provide any examples where infix operators are better suited to the problem that can'...
4 votes
7 answers
1k views

Why would one overload the && and & operator? [closed]

The same question goes for | and ||. Why would one overload or 'use' the & and && operator? The only use i thought of are Bitwise Ands for int base types (but not float/decimals) using &...
3 votes
2 answers
306 views

Greater than or identical to?

While browsing my code in a weakly-typed language I was seeing that I've trained myself to use identity (===) where logical. Then I came across a greater (or less) than or equal to (>=), and it ...
17 votes
2 answers
11k views

Bitwise-OR vs Adding Flags

I've seen others use Bitwise-OR to combine flags before: #define RUN 0x01 #define JUMP 0x02 #define SHOOT 0x04 const byte madPerson = RUN | JUMP | SHOOT; That's also the way I do it. But I've also ...

1
2