-
Notifications
You must be signed in to change notification settings - Fork 205
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
Add Tuples to the Dart language #3994
Comments
A Dart record is a tuple. I'm failing to understand why you think it's not. |
Dart already has tuples (we just call them "records" because they also allow named fields). |
Programming languages without tuples have classes such as Tuple2 and Tuple3 or Pair and Triple to simulate tuples or unnamed compound data structures. Dart properly supports tuples, such that these (annoying to write and sometimes costly at runtime) wrapper classes are unnecessary. |
Thanks for the clarification, this is an attempt to bring something new, have a great day |
I appreciate the attempt but I also want to point out that every time you file an issue, multiple members of the Dart team spend time reading, understanding, and responding to that issue. That's time we're not spending designing language features, talking to users, responding to other issues, etc. If you want Dart to bring new features to users, one of the most important things you personally can do is make sure you are using your and our time well. I am sure it took you a lot of time to file this 1,000 word issue and it definitely took me time to read it. That time would have been better spent doing research first and discovering that we already have tuples and it was one of the highest-profile features added in Dart 3.0. |
I just wanted to apologize for my ignorance of some topics, I am a person who is constantly training and studying, something I have learned in life is to learn to fail, I learned this with the book The Lean Startup the phrase that changed my life is: If you're going to fail, do it fast and cheap My mistake was ignoring that a Tuple is the same concept as a Record, I thought they were different concepts. I want to know if there is interest in this proposal to open a new issue where I will rename it as the class Person {
String name;
int age;
Person(this.name,this.age);
}
void main() {
// Declare list representing people
ListMultiple<int, String> list1 = ['ana',16,'yoel',30,'juan',26,'tifany',21,'Marie',43];
// Separate data based on its data type
List<String> names = list1.ofType<String>();
List<int> ages = list1.ofType<int>();
// Create people
List<Person> people = Stream.zip([names, ages])
.map(record => Person(record.$1, record.$2))
.toList();
// Recreate the tuple
ListMultiple<int, String> list2 = Stream.zip([names, ages])
.toListMultiple2<int, String>();
}
The multi-list is the same as a conventional list, the difference is that it can hold more than one type of data If the Dart language team is not interested in this, please close this issue permanently to avoid inappropriate comments from other people. |
@Ing-Brayan-Martinez Your usecase would be better solved by the general untagged union feature. See #83. For instance, you would be able to do (strawman syntax): final class Person {
final String name;
final int age;
const Person(this.name,this.age);
}
void main() {
// Declare list representing people
final list1 = <int | String>['ana', 16, 'yoel', 30, 'juan', 26, 'tifany', 21, 'Marie', 43];
// Separate data based on its data type
final names = [...list1.whereType<String>()];
final ages = [...list1.whereType<int>()];
// For the logic below to work, we assume that we have the same number of names and ages
assert(names.length == ages.length);
// Create people
final people = [
for (int i = 0; i < names.length; i++)
Person(names[i], ages[i]),
];
// Recreate the list
final list2 = [
for (final person in people)
...[person.name, person.age],
];
} Currently, you can already do it with final class Person {
final String name;
final int age;
const Person(this.name,this.age);
}
void main() {
// Declare list representing people
final list1 = ['ana', 16, 'yoel', 30, 'juan', 26, 'tifany', 21, 'Marie', 43]; // Inferred as List<Object>
assert(list1.every((item) => item is String || item is int));
// Separate data based on its data type
final names = [...list1.whereType<String>()];
final ages = [...list1.whereType<int>()];
// For the logic below to work, we assume that we have the same number of names and ages
assert(names.length == ages.length);
// Create people
final people = [
for (int i = 0; i < names.length; i++)
Person(names[i], ages[i]),
];
// Recreate the list, inferred as List<Object>
final list2 = [
for (final person in people)
...[person.name, person.age],
];
} |
Introduction
Greetings, today I have come with a proposal that would be worth sharing, I have had to do a lot of research to talk about a topic that is relevant to you, I am going to propose it as an achievable goal in the medium term, perhaps in the short term, that depends on you without further ado. preamble let's start
What is a Tuple?
A tuple is a data structure that functions as a list of objects of different types, which can be accessed through an index. These objects are not connected to each other but have meaning when we consider them collectively.
What is not a tuple?
It is necessary to explain that it is not a Tuple to avoid confusion with existing characteristics in the language, let's see the following:
List<dynamic>
directly, because a tuple must specify what data types will be admitted, for example aTuple2<String, int>
in this tuple it only admits 2 data types. It is important to specify that a tuple could easily be converted into aList<dynamic>
because this allows all types of objectsTuple2<String, int>
or aList<dynamic>
to be able to customize it better I have to make a separate issue because it is outside the main topicHow to represent a Tuple
Now that the concepts have been defined, let's now move on to the syntax. My proposal defines the tuples as follows:
As you can see, tuples have many variants depending on the number of types they are going to handle, this is ideal for a very precise object-oriented representation with a syntax that is easy to understand and use, this syntax is an idea borrowed from the Java ecosystem that is easily adaptable to the syntax of the Dart language, we must remember that this language is the combination of Java and JavaScript that is why this syntax exists
List<int> list = [1,2,3,4];
that combines the syntax of Java and JavaScript in a single expressionRegarding the scalability of this design based on variants you will not have any problem, because if you need a Tuple with more than 10 data types, it is a clear sign that you already have a bad design for the algorithm you are trying to build and you should use another way of representing your data with a responsible object-oriented design
How to use a Tuple?
The use of tuples will depend on the design of your algorithm, however I am going to make a basic example to give a perspective.
Example 1, create a tuple and use it with Stream
Example 2, convert a map into a tuple and display on the screen
Example 3: Convert a tuple to a list
Example 4: Using tuples with pattern matching
As you can see the concept of Tuple can be compatible with other features of the language that already works very well, I am just trying to give a broad perspective so that you can evaluate the scope of this proposal and the possible incompatibilities.
What problem am I trying to solve?
Answering this question is complicated due to the context of the proposal. The answer could be ambiguous because Tuples are intended to be one more tool than the Dart language already has to create algorithms that can solve problems.
Tuples are used to group data of different types in a context that is somehow disordered and then we intend to group and order in more complex types of data, the use of tuples will be conditioned to the context of where they are used, the same thing happens for example to lists or any data structure
If I'm honest, I don't know how to answer this question clearly. I only know that tuples are a useful tool that is used to create algorithms.
Conclusions
This proposal allows a Tuple design that can be differentiated between List, Map, Set and Record to be a data structure that adds value, the objective is to have tools that enhance the development experience with more and better tools to build algorithms
I have been doing a lot of research, to make a good proposal I politely ask you to consider it, at this moment I don't know if you have other priorities, consider it in the future
If you have any questions or suggestions, please comment. I will be attentive to give you an answer.
I ask you to use a list of questions format in case of any doubt to carry out an effective translation.
The text was updated successfully, but these errors were encountered: