-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Optimize the DateTime type for functional programming #56021
Comments
Can you explain which concrete changes you're asking for, how they will differ from what is there today, and which use-cases would be improved by that? Renaming the |
Regarding this, the idea is to implement the concept of FuntorTo represent a functor we need the operator Other possible functors are: MonadTo represent a Some operators that could be implemented are: If the |
Which part of |
We don't, you can also tear off unnamed constructors into functions: void main() {
final DateTime Function(int) dateTimeFromYear = DateTime.new;
print(dateTimeFromYear(2024));
} |
I don't think it's going to be very useful to discuss things like monad laws in the context of a language like Dart where mutable state is everywhere. Dart isn't going to mutate (no pun intended, of course ;-) into Haskell with a different syntax. However, I think there's a way to approach these ideas in a way which is a better fit for Dart.
@Ing-Brayan-Martinez, how well would it fit your needs if People do not agree, but in this context I'll consider a 'data class' to be the same thing as a 'value class', that is, a class whose instances are immutable. There would be some additional properties, of course. In particular, data classes would have some automatically generated members (like operator (Today, data classes are most likely going to be supported by a macro, which means that anybody who needs a slightly different set of auto-generated members can create their own macro that fits the bill.) I'm guessing that With that, extension on DateTime {
// This would be implicitly induced into the class `DateTime` if we had
// data classes and `DateTime` were declared to be a data class.
DateTime copyWith({
int? year,
int? month,
int? day,
int? hour,
int? minute,
int? second,
}) =>
DateTime(
year ?? this.year,
month ?? this.month,
day ?? this.day,
hour ?? this.hour,
minute ?? this.minute,
second ?? this.second,
);
// Add various requested methods to `DateTime`.
DateTime addYear(int year) => copyWith(year: year);
DateTime addMonth(int month) => copyWith(month: month);
DateTime addDay(int day) => copyWith(day: day);
DateTime addHour(int hour) => copyWith(hour: hour);
// ... etc.
}
void main() {
DateTime date = DateTime(2024, 06, 12);
date = DateTime(2024).addMonth(6).addDay(12); // Same thing.
print(date);
} Is it fair to say that you want support for automatically generating these members? One consideration about types could be quite interesting: The number of combinations is large if we wish to maintain in the static type of each expression that we have a proto- The point would be that we would be able to specify the instance variable values one by one, in any order, and it would be a compile-time error to forget any of them, and also a compile-time error to specify any of them twice. In order to avoid having 64 types for this, I've just modeled the situation using the same approach as Is there a nice way to get those errors? |
@eernstg You are right, I totally agree, I think it is necessary to say that I do not intend to convert Dart into Haskell, I just want to adapt some useful concepts, as they were done in Java 8 in 2014, the idea is to adapt the language to support functional programming of practical, minimalist and easy way to understand by human beings
My main priority is the My second priority is that the DateTime type inherits from the Monad type that I have proposed to create to group all types that have a behavior similar to a The rest of the operators are your choice if you want to include them, the idea is to add operators like The external dart_date library aims to add all the necessary operators through The only operator that we do need to add is |
As explained above there is no reason to introduce Also |
Greetings, today I want to talk to you about an update of the
DateTime
data type to make it compatible with the functional programming paradigm, to have a reference point I have decided to be inspired by the LocalDateTime type of the JDK, because this class is already designed to work with this paradigmThe main reason why this change is necessary is because we must make the
DateTime
type use the singlenton pattern so that if a date is defined it is stored within the type and can only be modified through the various operators, this is important to use dates in math calculationsWe all know that calculating dates is very complicated, that is why we must simplify the way of operating dates mathematically, functional programming helps with this
We should stop using the
DateTime(int year, [int month = 1, int day = 1, int hour = 0, int minute = 0, int second = 0, int millisecond = 0, int microsecond = 0])
constructor To use theof()
operator to be faithful to the functional programming paradigm, this would look like thisThe text was updated successfully, but these errors were encountered: