new and operator ++

  • Thread starter Thread starter Kerneltrap
  • Start date Start date
K

Kerneltrap

Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.
 
This is pretty interesting, as I get the same thing (and one would
assume that the C# compiler wouldn't produce invalid code).

I would submit this to Microsoft Connect as a bug.
 
Kerneltrap said:
Hi,

I have a type:

public struct Value
{
public static Value operator ++(Value v)
{
return new Value();
}

public static Value New()
{
return new Value();
}
}

This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

Changing from struct to class doesn't help.

Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.42
Win XP SP2.

Hi

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

Interesting.
 
Tom Spink said:
Hi

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

Interesting.



This is actually what the C# compiler should do as well, this is a compiler
error that is corrected in Orcas version of the compiler.

Willy.
 
Tom Spink said:
Kerneltrap said:
[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete
 
Tom Spink said:
Kerneltrap said:
[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete


You're missing operator binding: The code reads as:

Value v = ((new Value())++);

The postincrement operator is buried within the right-hand side of the
assignment.

Does

Value v; (v = new Value())++;

work?
 
Tom Spink said:
Kerneltrap wrote:
[...]
This code
Value v1 = Value.New()++;
compiles successfully, but i get InvalidProgramException.
The same for
Value v = new Value()++;
JOOI, the Mono C# Compiler yields a compilation error:
"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"
This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete

You're missing operator binding: The code reads as:

Value v = ((new Value())++);

The postincrement operator is buried within the right-hand side of the
assignment.

Does

Value v; (v = new Value())++;

work?

It doesn't. Same effect.
 
Okay, I'll bite.
Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

What you're missing is, that exp++ contains an assignment to exp. So this
should be "a varialbe, a property or an indexer".

What actually do you wan't to do?

Christof
 
Tom Spink said:
Kerneltrap said:
[...]
This code

Value v1 = Value.New()++;

compiles successfully, but i get InvalidProgramException.

The same for

Value v = new Value()++;

JOOI, the Mono C# Compiler yields a compilation error:

"error CS0131: The left-hand side of an assignment must be a variable, a
property or an indexer"

This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Okay, I'll bite.

Why is "Value v1" and "Value v" not considered a "variable, a property or
an indexer"? Specifically, sure looks like a variable to me. What am I
missing?

Pete



Pete, In top of what others said, the Mono compiler's error CS0131: is
somewhat misleading.
Orcas C# compiler error message:
[error CS1059: The operand of an increment or decrement operator must be a
variable, property or indexer]
is IMO more clear and accurate.


Willy.
 
Pete, In top of what others said, the Mono compiler's error CS0131: is
somewhat misleading.
Orcas C# compiler error message:
[error CS1059: The operand of an increment or decrement operator must be
a variable, property or indexer] is IMO more clear and accurate.

Ah, okay. Since ++ has an implicit assignment, the result has to be able
to be assigned back to the original source, which isn't possible in this
case.

Thanks everyone. Makes sense now.

Pete
 
Willy said:
This is actually what the C# compiler should do as well, this is a
compiler error that is corrected in Orcas version of the compiler.

Willy.

Nice. I figured as much, because of postfix increment's implicit
assignment. It's a shame it's worded a bit funny in gmcs.
 
Nicholas said:
This is pretty interesting, as I get the same thing (and one would
assume that the C# compiler wouldn't produce invalid code).

I would submit this to Microsoft Connect as a bug.

Strange.

Decimal d = New Decimal()++;

or even

Decimal d = 0m++;

Produces just a compilation error as it should. What is the difference?
Decimal is just a struct like any other or is it treated special by the
compiler as it is with System.String?
 
Back
Top