A Blatant Programming Question
-
leppie wrote:
Who knows what Item0 and Item1 and Item2 is?
The Tuple is a generic type so that shouldn't be a problem.
And now you have 2+ problems ;p
-
Anonymous classes handle exactly two of those cases.
Within context, I will handle all of those. But yes, if exposing them non-functionally, only 2.
-
I never thought I'd see the day when I'd vote anything posted by one of my very favorite posters, Roger Wright, a "one," but in this case I feel there is a compelling reason to do so based on what I view as a concern for the quality of CP as-a-whole, and I have fully explained that here on "Bugs and suggs:" [^]. best, Bill p.s. go ahead and down-vote me all you like: if my entire reputation on CP is wiped out, and I go "underwater," well: let's just say, I have "gills," and "don't give a damn" at nearly 69 years of age: in my opinion if a person by the time of their sixties doesn't know what their values are, and is not willing to go out-on-a-limb, and stand up for them, well ... you fill in the rest of the sentence, please ...
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut
Even if you were a mute deaf I would still upvote you ;p
-
If you can't think of a reason, and a custom class or struct will suffice (and be more descriptive), then write a class or struct. I'll occasionally use a Tuple briefly to test something before I write a class. If you can use a class, you should use a class.
People are so scared of writing something once...
-
Well, my biased opinion: Tuples were added to .NET languages because they are an important feature of functional languages, and since Microsoft was adding F# to be IL compatible, they needed support for tuples. Tuples are a simple way of creating typed structures on the fly (type is inferred by the usage of their items), without having to actually create a class or struct. In F#, they are even simpler than records. So, given that, tuples are useful when you need to return more than one value from a function call. For example, a success/fail along with a success/default value. Or the real and imaginary components of a complex number. I don't find tuples necessarily that useful for passing data into a function, the exception being passing the tuple return of one function into another function. Tuples do not replace lists, arrays, or other collections. Does that help? Marc
Marc Clifton wrote:
tuples are useful when you need to return more than one value from a function call
Yet this is still an ugly hack for returning multiple values. A proper language would be consuming the (remaining) values (on the stack) based on the continuation.
-
Tuple types were added to facilitate language interoperability and to reduce duplication in the framework. As you noticed a tuple is a simple generic data structure that holds an ordered set of items. Tuples are supported natively in languages such as F# and IronPython.
Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Whenever methodologies become productized, objectivity is removed from the equation. -- Mike Myatt
Given the Python argument, I would prefer .NET support PHP then too ;p
-
Given the Python argument, I would prefer .NET support PHP then too ;p
Here you go: Welcome to Phalanger[^]
Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Whenever methodologies become productized, objectivity is removed from the equation. -- Mike Myatt
-
Laziness - that's why I occasionally use them. I feel dirty afterwards.
Ah... Just call the garbage collecter afterword, and you would be fine. :laugh:
-
Anonymous classes handle most of these cases and cleaner in my opinion. Who knows what Item0 and Item1 and Item2 is?
Hi Leppie, I have written a request to you to please give an example of use of anonymous classes here on the "Tuple" thread on the C# forum:[^]. I'd really appreciate hearing more about that ! best, Bill
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut
-
I never thought I'd see the day when I'd vote anything posted by one of my very favorite posters, Roger Wright, a "one," but in this case I feel there is a compelling reason to do so based on what I view as a concern for the quality of CP as-a-whole, and I have fully explained that here on "Bugs and suggs:" [^]. best, Bill p.s. go ahead and down-vote me all you like: if my entire reputation on CP is wiped out, and I go "underwater," well: let's just say, I have "gills," and "don't give a damn" at nearly 69 years of age: in my opinion if a person by the time of their sixties doesn't know what their values are, and is not willing to go out-on-a-limb, and stand up for them, well ... you fill in the rest of the sentence, please ...
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut
While I completely disagree with your opinion, I highly respect it and accordingly, voted you a "5" as well. :-D It was a technical question - which is perfectly valid in the Lounge - about a concept, not a programming help request. It has broad application to multiple languages, and is entirely proper in the Lounge. I found the responses illuminating, but in no way instructive, which was exactly what I was seeking - a general perspective.
Will Rogers never met me.
-
Here you go: Welcome to Phalanger[^]
Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Whenever methodologies become productized, objectivity is removed from the equation. -- Mike Myatt
I meant being influenced by the semantics of PHP ;p
-
I meant being influenced by the semantics of PHP ;p
leppie wrote:
influenced by the semantics of PHP
Those being? Mind, I've got nothing against PHP, I just never use it ... ;)
Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Whenever methodologies become productized, objectivity is removed from the equation. -- Mike Myatt
-
leppie wrote:
influenced by the semantics of PHP
Those being? Mind, I've got nothing against PHP, I just never use it ... ;)
Espen Harlinn Principal Architect, Software - Goodtech Projects & Services AS Whenever methodologies become productized, objectivity is removed from the equation. -- Mike Myatt
Espen Harlinn wrote:
Those being?
Dont really know, but none or horrible comes to mind ;p
-
Ah... Just call the garbage collecter afterword, and you would be fine. :laugh:
I like your thinking
-
Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)
Will Rogers never met me.
The concept of using arrays has been bought up - and why use them instead of arrays. It's because arrays have no type safety - leading to logical bugs at runtime. For example: Buggy code:
// I promise to return an array of length 2, with a String and Double.
public static object[] DoStuff()
{
return new object[] { "Hello", 123 }; // Did you mean 123.0?
}public static void Main()
{
var ret = DoStuff();
var a = (string)ret[0]; // What happens if DoStuff() doesn't return the correct number of items?
var b = (double)ret[1]; // As above, as well as what if the type is wrong (which it is)?
}Contrast this with:
// No comment required because this documents itself.
public static Tuple DoStuff()
{
return Tuple.Create("Hello", 123); // Compiler error, not runtime error! Wooo!
}public static void Main()
{
var ret = DoStuff();
var a = ret.Item1; // Always succeeds.
var b = ret.Item2; // Always succeeds.
}Finally consider the [
internal
, clearly] extension method magic you can do. So basically they are needed when you need an array of fixed length where each element in the array is a strong type. Remember that C# is [typically] a strongly-typed language andTuple
is a great citizen in the .Net type system for any language that is strongly-typed (and functional languages like F# too). You could write a class that represents your return type/whatever, but then again, why do you useDictionary<TKey, TValue>
? Shouldn't you write a specific type for that?Tuple
is as much as a strongly-typed primitive as an array (it was just previously missing); it just needs to be used correctly (see above paragraph). Edit: Yes, I know that it originates from functional languages, but as always the .Net Framework team are showing us that at the end of the day all things are the same: and today the functional features in C# feel like natural parts of a imperative language (even though previously they were not). I strongly feel that the same principle applies toTuple
. Yes, I know that a runtime based on multiple return values (like Go) would be more 'correct'; but theTuple
is the biggest bang for 'risk buck' - in addition you can use it in places other than return types. Basically, learn to love that your cheese was moved into the fridge (where it belongs).He who asks a question i
-
Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)
Will Rogers never met me.
When defined at compile-time, a tuple is nothing else than a struct (a class if you prefer) and each member is referenced by a name. The .NET Tuples allow defining them at run-time, referencing the members by an index, to allow some form of run-time polymorphism. You will use a .NET Tuple when the composition of the items is unknown until run-time, for instance if it is input by the user.
-
Things I find them very useful for: - Returning multiple values without the bother of out or ref parameters, and so I don't have to make a whole other class. - Working with Enumerable.Zip - Keys into Dictionarys so I don't have to bother writing a fast and correct Equals and GetHashCode() methods - Ensuring immutability. Unlike an array, the items are readonly. An array is faster and nicer, but it's mutable, which is sometimes a pain.
Agree with all, I sometimes use it to avoid writing a class and use it in my model only, if I want to construct a list of 'paired' types for a display for example
____________________________________________________________ Be brave little warrior, be VERY brave
-
Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)
Will Rogers never met me.
Not sure if you actually got an answer here, but the 20 or so results I scanned seemed hopelessly misguided. Tuples are used enormously in functional programming and are extremely useful in LINQ. With LINQ queries, you may have an object of type person with Name, Age, Gender, etc. Now if you execute "from Persons where age > 20 select Age, Gender", what is the type of the result: answer a tuple type with the first item an integer and the 2nd an enum type. This is where they are useful. They avoid having to litter your code with classes that add no meaningful information and are pure data values. They are commonly useful as return types, e.g. a tuple where the first item indicates if the 2nd is applicable. Many more usages exist.
-
Not really... ;P There's a discussion going on in the C# forum about Tuples, and I'm curious what one would use them for. From browsing VS2010 Help, it appears to me that this is a way to make vectors of mixed types which, if used as a type for an Array, could allow mixed arrays. Is this correct? And what would be an example of using such a beast? Wouldn't a dataset be more efficient? Enquiring minds want to know, as they say at the checkout counter. :)
Will Rogers never met me.
One of the best things tuples do is they allow you to return multiple values from a function instead of just one value as was the case in C# before C# 4.0.
-
I never thought I'd see the day when I'd vote anything posted by one of my very favorite posters, Roger Wright, a "one," but in this case I feel there is a compelling reason to do so based on what I view as a concern for the quality of CP as-a-whole, and I have fully explained that here on "Bugs and suggs:" [^]. best, Bill p.s. go ahead and down-vote me all you like: if my entire reputation on CP is wiped out, and I go "underwater," well: let's just say, I have "gills," and "don't give a damn" at nearly 69 years of age: in my opinion if a person by the time of their sixties doesn't know what their values are, and is not willing to go out-on-a-limb, and stand up for them, well ... you fill in the rest of the sentence, please ...
"One of the few good things about modern times: If you die horribly on television, you will not have died in vain. You will have entertained us." Kurt Vonnegut