|
The use of a trigger is coding horror by itself.
|
|
|
|
|
You forgot this
Bob
Ashfield Consultants Ltd
|
|
|
|
|
My schoolmate told me about a situation he faced while coding using C#.
if we have the following code snippet :
public class Item
{
public int this[int y]
{
get
{
return 0;
}
}
}
While compiling an error will appear saying:
'Item': member names cannot be the same as their enclosing type
First I got confused, since I knew that I was defining indexer property for the class 'Item'.
After I referred to MSDN I read this :
Compiler Error CS0542
If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict.
At the end I think that this MSIL specification mustn't be exist at all.
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
This specification exists because other .NET languages such as VB.NET have to explicitly write it down. Think of Item as just another reserved word in C#.
Yes, MS could have chosen an ugly name like __MSIL$%Indexer%%__ or so, but what would it look like in VB or your documentation?
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas Weller wrote: This specification exists because other .NET languages such as VB.NET have to explicitly write it down.
That's right,
I think if a wrapper exist for each language between it's syntax and MSIL emitted code, this will help a lot.
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
Redwan Al-Bougha wrote: I think if a wrapper exist for each language between it's syntax and MSIL emitted code
Well, such a 'wrapper' exists. They usually call it 'compiler'.
Regards
Thomas
www.thomas-weller.de
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Programmer - an organism that turns coffee into software.
|
|
|
|
|
Thomas Weller wrote: They usually call it 'compiler'.
Thanks Thomas ,
I meant instead of :
1- Read language-specific syntax.
2- Convert code to MSIL code.
Do this :
1- Read language-specific syntax, where each language is not related in any way to MSIL code.
2- Convert code to MSIL code.
That's it avoid the happened ambiguity, as easy as this.
Or easily don't use common -reserved- words in any .NET language.
Best wishes,
Redwan Al-Bougha
|
|
|
|
|
You're missing the point. It's not that the MSIL code requires that the name of the property returned by the indexer be 'item', this is just a name that is auto generated by the compiler as a convenience. In your case, you have chosen the one and only class name that collides with the automatically generated name (and any name chosen for automatic generation will collide when used in a class of the same name). .Net also provides a convenient way to fix this: use an attribute to specify the name for the indexer so you can keep your class name as Item:
public class Item
{
[System.Runtime.CompilerServices.IndexerName("TheItem")]
public int this[int y]
{
get
{
return 0;
}
}
}
There is nothing to complain about here.
|
|
|
|
|
Rob Graham wrote: this is just a name that is auto generated by the compiler as a convenience
Thanks Rob,
That's what I wrote in the 1st post :
Redwan Al-Bougha wrote: A default indexer is given the name 'Item' in the emitted code, creating the conflict.
By the way thanks for your notice and for IndexerName attribute
Best Wishes,
Redwan Al-Bougha
|
|
|
|
|
This may more properly be in the Subtle Bugs forum, although it's not entirely subtle.
Anyway, my complaint wouldn't be that the indexer is considered to be named "Item"; I dont like the "member names cannot be the same as their enclosing type" part.
Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci?
|
|
|
|
|
PIEBALDconsult wrote: Why not? If I'm writing a Fibonacci method why shouldn't I put it in a class named Fibonacci?
Exactly. Why not? If programming the class in C++, when you reference it you use something like Fibonacci::Fibonacci, which has always been clear to me what's going on.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
In C# and C++ a method that has the same name as the class itself is called a "constructor"...
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: method that has the same name as the class itself is called a "constructor"...
Thanks, but I already know that.
|
|
|
|
|
Yes, I actually hoped it was so
What I meant is that the limitation is somehow "logical". I don't find logical to have a class named "Fibonacci" that has a method named "Fibonacci". I would rather expect methods like "generate" or "next".
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Yes, I actually hoped it was so
Yeah. With a Master's in CSCI, teaching CSCI at the college level, and over 15 years IT experience, one should know what a constructor is
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
No, at least in C#, a constructor is named .ctor :
System.DivideByZeroException: Attempted to divide by zero.
at Template.Template..ctor(Int32 x)
Like the indexer, the developer doesn't give it a name, only the return type and parameters.
|
|
|
|
|
Ok, it's time for a little nitpicking then (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something.
The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class.
This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity
On the other hand, now we know where the coding horror is
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name
Which is the only name that matters.
WilliamSauron wrote: I know that if a method is declared that has the same name as the class, it is a constructor.
That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.
|
|
|
|
|
PIEBALDconsult wrote: Which is the only name that matters
Unless you target another architecture than the CLR, of course, in which case you are free to name generated symbols as you see fit. Who said that it is forbidden to directly generate IBM 360 assembly from C# ?
PIEBALDconsult wrote: That's funny, I know that if a method is declared that has no name and returns an instance of the class, it is a constructor.
Funnier, my copy of "The C# Programming Language", ISBN 0-321-15491-6 by Anders Hejlsberg, Scott Wiltamuth & Peter Golde, on chapter 10.10 "Instance Constructors", page 344 has strong hints that they understand the constructor name to be similar to a method name, not a type name:
The identifier of a constructor-declarator must name the class in which the instance constructor is declared. If any other name is specified, a compile-time error occurs.
Similarly, my Visual Studio highlights the constructor as a method name, not as a type. But hey, Visual Studio is a Microsoft product, these are known not to follow recognized standards. And who knows, maybe this Hejlsberg guy is just a newbie
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Unless you target another architecture than the CLR, of course
True, but it would still likely generate the same name, for convenience.
My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.
|
|
|
|
|
PIEBALDconsult wrote: True, but it would still likely generate the same name, for convenience
Yes, and since in some assembler languages (the one that comes to mind is Compass but there are others,) labels beginning with a dot must be local, that would make sure your constructors cannot be called from outside your module.
PIEBALDconsult wrote: My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.
Yes, if you prefer calling that an identifier instead of a name (let's not reach for the scissors and talk about the subtle semantic differences between a name and an identifier.) But still a method identifier, not a type identifier. Last time I looked, the identifier denoting a constructor in my Visual Studio editor had the same black color as another method identifier, not the greenish color of my class identifier. Are they all color blind at Microsoft?
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: Visual Studio editor had the same black color
Tools|Options|Environment|Fonts and Colors|Text Editor|Identifier
VS doesn't discriminate between these identifiers, but I could write an editor that does. (I'll stick with Edit.)
P.S. I finally got around to installing Leppie's xacc.ide -- it displays constructor identifiers in the greenish color.
modified on Friday, November 28, 2008 1:07 PM
|
|
|
|
|
WilliamSauron wrote: This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers?
No, they provide an easy way around it. See IndexerNameAttribute .
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|
There is no need to have a way around it through an attribute. If a class has an indexer, then it is a collection of some other things; "Item" is therefore not a good name for that class. It is a Coding Horror. Give the class an appropriate name, and you don't need the attribute...
-- Quidquid latine dictum sit, altum sonatur.
http://streambolics.flimbase.com
S. L.
|
|
|
|
|
WilliamSauron wrote: "Item" is therefore not a good name for that class.
No, it's simply your opinion.
xacc.ide - now with TabsToSpaces support IronScheme - 1.0 beta 1 - out now! ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))
|
|
|
|
|