-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Predefined ValueTuples #1547
Comments
I think they already added this feature in C# 1.0. It's called a |
So your
|
I alredy used this solution:
But as you can see, I have to make my struct generic type, becuase I need this tow value range of two differnt types. I also use type called CharOrEscape, but practically this makes some problems, espitially when passing a valueTuple array to a List<Range> which needs more code fot casting! |
I replied in the #1546 issue because of the problem of adding an array of type1 to a list of type2. C# can't do the cast even if type1 can be csted to type2! |
@MohammadHamdyGhanem The language shouldn't be encouraging developers to tie the code into knots in the manner that you seem to. If you need a named and reusable data structure then you don't need tuples. |
@HaloFour |
@MohammadHamdyGhanem
You seem to like to invent your syntax as you go along and then propose that C# change to meet what you would like. That's not how this works. It also won't remotely happen on a timescale that makes it relevant to this project that you're working on. C# is already getting range syntax. Arguing that C# should add a way to name tuples as proper types so that you can use them as a hack of a range syntax is not likely to gain much in the way of traction. C# already has aliases. You can do |
@HaloFour
This is the best syntax I came up with so far. The pattern will be |
Don't we already have that? It's a regex :) I thought the point of verex was to not be as short as possible, but instead to encourage a more tree-construction-based approach so you could structurally build your regex programmatically instead of parsing it out of a string. |
var x = Pattern.CharClass('a', 'b')
.AddRanges(('0', '9'), ('j', 'r'))
.Repeat(5, RepeatMode.Lazy); There is nothing stopping you from taking a Tuple in as your arguments, but then converting it internally to an actual named type. This is trivial to do, and i would not really be very beneficial for the language to provide a shorthand for something that can already be done so simply by you inside your API. |
But you have a predefined valuetuple. It already exists and works well. You just don't want to use it :)
I don't think the proposal makes things easier. You can already do this: public Pattern AddRanges(params (char, char)[] ranges)
=> AddRanges(ranges.Select(Range.From));
private Pattern AddRanges(IEnumerable<Range<char>> ranges)
{
// Do whatever you want. with the parameter.
return this;
} |
@CyrusNajmabadi |
I thought I read this on another issue, but cannot seem to find it. But today, it isn't possible to define tuple aliases: using System;
// not allowed:
using Person = (string firstName, string lastName);
// works, but has no names to Item1/Item2
using PersonVT = ValueTuple<string, string>; There's been more than one occasion where I wished to have this to indicate more clearly that I actually mean the same tuple type; and not just one that looks the same. ...or I could just go and define a |
Also, if #100 is added, the current calling code could be such: // Current:
var x = new CharClass(new Range('a', 'z'), new Range('A', 'Z'), new Range ('0', '9');
// C# 8.0
var x = new CharClass(new('a', 'z'), new('A', 'Z'), new('0', '9');
// Tuples
var x = new CharClass(('a', 'z'), ('A', 'Z'), ('0', '9'); Yes tuples are still the best, but not much better than with #100 to make any significant argument to add it. |
Another approach:
|
@MohammadHamdyGhanem
|
@HaloFour |
@MohammadHamdyGhanem You can use it for aliases today, it's just somewhat limited. There are various proposals to make it less limited: using StringDictionary = System.Collections.Generic.Dictionary<string, string>; |
I'm using this ValueTuple in one of my classes:
(Char from, Char to)
I found that using such tuple many times in code makes it longer and ugly such as:
protected List<(Char from, Char to)> Ranges = new List<(Char from, Char to)>();
Of cource this became a nightmare if the tuple has more than two elements!
but it solves some problems such as this:
Tuple param array is better that struct param array because it is self descrying and makes calling code shorter:
var x = new CharClass(('a', 'z'), ('A', 'Z'), ('0', '9');
This is better than:
var x = new CharClass(new Range('a', 'z'), new Range('A', 'Z'), new Range ('0', '9');
So, I ask: why can't we declare the ValueTuple as a Type, so we can reuse it anywhere?
Something like this:
So we can write:
The param info tool tip should show the CharRange Tuple as (Char from, Char to) so the user knows what he should write. We pass the values to this tuple param as usual:
var x = new CharClass(('a', 'z'), ('A', 'Z'), ('0', '9');
There is another benefit: the names of the elements will not be lost in reflection, because it's now stored in the type declaration.
Note: I think this may relate to Records.
May be this problem can be solved in general be allowing record values to be passed as Tuple syntax (the reverse of the deconstruction).
I know there are plenty of suggestions out there may be similar, but I can't recall them right now.
The text was updated successfully, but these errors were encountered: