C# 4.0
-
well, I understand it's bothersome to define a class for each possible return type. but in this simple case, how about: public int[] MinMax(int[] numbers) { int min, max; // Code to calculate min/max return new int[] {min, max}; } BTW I don't like Power Collection, they code is convilted and my implementation of RedBlackTree is 7 times faster. OK I'll share it very soon!
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
Elegant, indeed, and I've done this before. But it gets messy if you want to return an integer, 2 strings, and a DateTime, for example. You can create a struct to return this data, and object array (yuch), or use out parameters. Tuples just look cleaner to me. :)
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
-
Elegant, indeed, and I've done this before. But it gets messy if you want to return an integer, 2 strings, and a DateTime, for example. You can create a struct to return this data, and object array (yuch), or use out parameters. Tuples just look cleaner to me. :)
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
Yeah, it's what I mean by "it is cumbersome to create a class for all possible returns type" I guess it's syntaxic sugar but the compiler does a lot of syntaxic sugar already (foreach/yield, var, etc...) But the only clean implementation I can think of is to return a struct kind of struct FunctionMinMaxReturns { public double Min; public double Max; } And I could foresee the type explosion in the documentation if the compiler does that (because these types need to be documented for the developer's sake) An other alternative but it works only in an untyped world, is simply to return object[] from all this tuples function.
A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.
-
Well, if you Don't want to alter it, why would you care to tell your compiler that? This is not C++ or C where it would have made a significant difference in some cases.
Because if you're providing an interface, you provide a contract with the people who use that interface. If I write a library, I can use const to tell a user when they can trust my code not to change their stuff.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
Then so be it, in this case you would simply remove the "const" if it were there, meaning that it shouldn't really have been there to start with
Do you never write code that other people will use ?
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
-
I'd love to see a const keyword on parameters to methods, and optional parameters. Both of which seem simple enough.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
BTW - am I correct in saying you can only apply
const
to value types? That's the behaviour I seem to get with C# 3.0"For fifty bucks I'd put my face in their soup and blow." - George Costanza
-
Yeah, but what method should be called if I want to ignore return value? 1.
int i = functA("a"); // ok int functA(string) is called
2.string s = functA("a"); // ok string functA(string) is called
3.functA("a"); // wtf?
well then how about a void functA("a"); one would assume that this default case would be anticipated by programmer, but failing that; the syntax could be; (cast)functA("a"); and that would work to even though there is no left param; but compiler would flag functA("a"); with a warning. Would that work for you? What would you like to see?
MrPlankton
-
I was wishing for such a thing just yesterday. Ended up using an array, but the calling code is much uglier for having to unpack it. Actually, what would be great would be something like the destructuring assignment syntax recently added to JavaScript. Imagine being able to do this:
double w;
double h;
double d;
...[w,h,d] = CalculateDimensions(...);
:-D
----
You're right. These facts that you've laid out totally contradict the wild ramblings that I pulled off the back of cornflakes packets.
and
return [w,h,d]
? orreturn {w,h,d}
? -
I'd like them to include a compiler switch to treat extension methods as errors. I'd also like them to place a "feature freeze" on the language. A good programming language need not be updated every three years.
Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry
Oh, well then... Treat the
using
directive as an error. While you're at it, require full attribute names. -
So now that C# 4.0 is being talked about, I was wondering what people thought would be good additions to the language. Sorry if this is a repost, but I went through several pages, and didn't see anything, so... What I'd frankly love to see would be tuples. Rather than having to use multiple 'out' parameters, you'd just return multiple values:
public int,int MinMax(int[] numbers)
{
int min, max;
// Code to calculate min/maxreturn min, max;
}What do you think? What would be good for the next version?
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
Oh, another one; the
is
operator needs a complement:!is
isnt
aint
!( x is someclass )
is so clunky! -
and
return [w,h,d]
? orreturn {w,h,d}
? -
I'd really really really like to see absolutely no changes whatsoever. Seriously.
"It's so simple to be wise. Just think of something stupid to say and then don't say it." -Sam Levenson
Thank you. Best regards, Paul.
Jesus Christ is LOVE! Please tell somebody.
-
Christian Graus wrote:
optional parameters
Mixing optional parameters and overloads can lead to pretty bad mess.
I've never had a problem with them in C++, you just have to design it so there's no ambiguity. I think optionals are much cleaner than writing tons of overloads that end up calling the base function with default values. Here's a question that you all might know. Given: void Foo() { Foo(0); } void Foo(x) { Foo(x, 0) } void Foo(x, y) { } Does calling Foo() make 3 function calls, or does the compiler optimize this in any way? If you're simulating optional parameters with 10 overloads, this could get expensive real fast! The alternative is to have every overload call the base function, specifying all the parameters. This would reduce it to 2 function calls. void Foo() { Foo(0, 0); } void Foo(x) { Foo(x, 0) } void Foo(x, y) { } Just rambling now....
- S 50 cups of coffee and you know it's on! A post a day, keeps the white coats away!
-
I'd love to see a const keyword on parameters to methods, and optional parameters. Both of which seem simple enough.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
Christian Graus wrote:
const keyword on parameters to methods
But how would the compiler verify the "constness" of methods that you call on a const object? The methods themselves would have to be declared const, just like in C++. Everything, including the BCL, will need to change for that. There's also the versioning problem. In C++, if a library changes, you are forced to recompile with the modified header files. There's no such need in .NET, so if a method declared const in v1 of the library became non const in v2, the "constness" guarantee will get broken (unless there is a runtime check).
Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro
-
I'd love to see a const keyword on parameters to methods, and optional parameters. Both of which seem simple enough.
Christian Graus No longer a Microsoft MVP, but still happy to answer your questions.
As for the optional parameters, they say that method overloads work better in that respect. I got used to it and don't complain. Maybe one advantage (trying to agree with MS) I can see is that when you debug your C# code the debugger (Call Stack) will show you which overload was called exactly, while it may not be apparent if a default parameter value was used...
-
It's been awhile since I did any c++, but I believe you would get a compile warning with Borlands old c++ compiler and then it would take it's best guess. Casting the function call would make the compiler happy. They could do the same with next version c#.
MrPlankton
MrPlankton wrote:
It's been awhile since I did any c++, but I believe you would get a compile warning with Borlands old c++ compiler and then it would take it's best guess
I'd rather not see it. I treat compiler warnings as errors, but from time to time I have to work with people who do stuff like try..catch with the general exception and don't even log the exception message. :( The compiler cries of course but no one pays attention. *sigh* That proposed feature is very interesting, but I'm afraid of it. ;)
-
well then how about a void functA("a"); one would assume that this default case would be anticipated by programmer, but failing that; the syntax could be; (cast)functA("a"); and that would work to even though there is no left param; but compiler would flag functA("a"); with a warning. Would that work for you? What would you like to see?
MrPlankton
MrPlankton wrote:
well then how about a void functA("a");
What if there's not such method?
-
Me too. I think they already degraded the language in C# 3 by adding extension methods and partial methods just to sell LINQ.
Sunny Ahuwanya "The beauty of the desert is that it hides a well somewhere" -- Antoine de Saint Exupéry
Sunny Ahuwanya wrote:
I think they already degraded the language in C# 3 by adding extension methods
I think of them as of an improvement and use them. :)
-
So now that C# 4.0 is being talked about, I was wondering what people thought would be good additions to the language. Sorry if this is a repost, but I went through several pages, and didn't see anything, so... What I'd frankly love to see would be tuples. Rather than having to use multiple 'out' parameters, you'd just return multiple values:
public int,int MinMax(int[] numbers)
{
int min, max;
// Code to calculate min/maxreturn min, max;
}What do you think? What would be good for the next version?
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
Static verification would be awesome. Contracts[^] would be nice too if they could get some of those features in without making too much of a mess. For instance, Spec# will throw a compile-time error (and squiggly underline in Visual Studio) with this code:
public float Divide(float x, float y)
{
return x / y;
}But this would be valid:
public float Divide(float x, float y)
requires y != 0
{
return x / y;
}As would this:
public float Divide(float x, float y)
{
if(y == 0)
throw new ArgumentException("y");
return x / y;
} -
Why const? What will it even do besides limit the programmer in the usage of said parameters?
-
So now that C# 4.0 is being talked about, I was wondering what people thought would be good additions to the language. Sorry if this is a repost, but I went through several pages, and didn't see anything, so... What I'd frankly love to see would be tuples. Rather than having to use multiple 'out' parameters, you'd just return multiple values:
public int,int MinMax(int[] numbers)
{
int min, max;
// Code to calculate min/maxreturn min, max;
}What do you think? What would be good for the next version?
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan Portland, Oregon, USA
do anybody suggest me any gud article on 4.0 And what is the raod map of 4.0