Is there a way I can use the magic of generics to write a function that returns an optional if the input is optional, otherwise both non-null?
For example, I have two functions that I want to combine into one.
static Timestamp dateToTimestamp(DateTime dateTime) {
return Timestamp.fromDate(dateTime);
}
static Timestamp? dateToTimestampOptional(DateTime? dateTime) {
return (dateTime == null) ? null : Timestamp.fromDate(dateTime);
}
2
Answers
Make the type as generic
Here’s how you can combine the two functions into one using generics:
Usage: