Syntactic Support for Tuples?

T

Travis Parks

Hello:

I wonder if there was ever a discussion about whether or not to make
creating tuples easier.

For instance, in many languages, you can simply indicate a tuple using
comma separated expressions with optional parenthesis.

I am sure supporting such a syntax would be a breaking change, but I
was hoping for _some examples_.

It would also be nice if C# supported the ability to treat "out"
parameters as tuple indexes. So, for a method like:

bool TryParse(string value, out int result);

You could grab the result simply with an expression like this:

bool success, int result = Int32.TryParse("123");

The example above would never compile today. So, why would introducing
it in future versions be a bad thing?

C# already supports "var", so why would determining the type of a
tuple be any harder:

(var number, var text) = 123, "Hello"; // automatically determines
types

Honestly, I am having a hard time coming up with any examples where
these enhancements would be a breaking change. Without syntactic
support, I'd rather not use tuples at all.

Thanks for any breaking examples!
 
T

Travis Parks

Travis said:
I wonder if there was ever a discussion about whether or not to make
creating tuples easier.

I have seen others discuss it, yes.
For instance, in many languages, you can simply indicate a tuple using
comma separated expressions with optional parenthesis.
I am sure supporting such a syntax would be a breaking change, but I
was hoping for _some examples_.

I am sure that no new syntax would be introduced to C# that would be a
breaking change, for tuples or otherwise.  If tuples wound up getting
language support, it would absolutely have to be done in a way that
won't break existing code, nor existing language features.

That will make extending the language to support tuples challenging, to
be sure.  But never say never.  The C# language design team is a bunch
of very smart people who take the job seriously, and I'm sure that if
they decide the feature's important enough, they'll do it right.  If
it's not important enough, it won't be done at all.
It would also be nice if C# supported the ability to treat "out"
parameters as tuple indexes. So, for a method like:
bool TryParse(string value, out int result);
You could grab the result simply with an expression like this:
bool success, int result = Int32.TryParse("123");
The example above would never compile today. So, why would introducing
it in future versions be a bad thing? [...]

I doubt you'd see something like that, mainly because of the ambiguity
involved and the chance that _that_ could be a breaking change.

Instead, the language would probably wind up offering shortcut syntax,
such as we already have for Nullable<T>, for declaring and using tuples.

Pete

A man can hope for syntax support. Right now I am playing with
IronPython and F#. Both support tuples. F# even supports treating out
parameters as part of a tuple, which is where I got the idea.

Some times when I am working in C#, and .NET in general, I start to
feel the results of its old age. For instance, this week, when I was
working in the System.DirectoryServices namespace, I ran into a lot of
classes inheriting ICollection. I know MS wants to keep moving forward
with newer and better libraries. I just hope that in some version they
decide to go back and revamp some of their older libraries to use
generics and other new language features. They've already done some of
that for String, etc., and I hope they continue down that path.
Eventually, deprecating and removing the old collection classes would
be the way to go, IMHO. There's no rule saying old code has to
use .NET 6 or whatever.

The language needs to evolve, too. MS has been careful by creating
"context sensitive" syntax. Introducing things like tuples, without
syntax support, seems like stopping halfway. .NET 2.0 was almost like
a brand new language and it put C# miles ahead of Java, at the time.
Another jump like that may be scary, but it will continue to move C#
to the top of the line. I have developed a certain trust for MS's
decisions involving C#, so learning new syntax to help me be more
productive has just been part of the job. I hate looking at languages
like Python or F# and becoming jealous of its power to express.
 
T

Travis Parks

Travis said:
[...] I hate looking at languages
like Python or F# and becoming jealous of its power to express.

Personally, I think you overstate the usefulness of tuples.  Just having
the classes in .NET alone provides most of the convenience one really
needs, and frankly even that isn't a huge deal.

Tuples are simply for convenience. They do have limited usefulness.
Most of the time, I would feel more comfortable using named classes
with named properties. However, in other languages, doing without
tuples seems like it would be painful. I wonder if they were more
integrated into C# whether their usefulness would seem different to
us. As I said, without syntax, I'm not going to use tuples as much as
I might otherwise.
C# is hardly stagnant.  Each version has introduced fairly significant
additions, and in some cases exceeds what other languages like Python
and F# provide (iterator methods and generic variance come to mind).
The various languages are just different, and it's a fallacy to claim
that C# is somehow "less powerful" simply because the designers chose to
invest their finite resources on certain features instead of others.

C# is very powerful; that is why it remains the main language of use
in my office. However, a lot of the .NET framework is built on top of
now-out-dated code. I think some clean up should be done to the old
libraries. MS can afford to revamp the technology that they are moving
everything ahead with. Eventually, the old code should be replaced and
removed. Outside of tuples, I hope the language of C# grows, too. One
of the biggest problems I had when I programmed in C++ was the
unwillingness to make breaking changes. C++ seems to be gradually
falling further behind. Old languages seem to depend almost entirely
on bigger and better libraries, as if they solve every problem. I
don't want that to happen to C#, too.
But all that said, you never know.  Maybe a future version of C# will in
fact include syntactical support for tuples.  It could happen.

I hope so, and much more.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top