Not sure if this is a correct question for here but I was wondering about the Groovy keyword of def (and the equivalent of other dynamic or optionally typed languages).
One useful, or nice usage of something like this is that you could have one type of value assigned to a variable and then change it to another type.
For instance, let's say you get a map of two timestamps that represent a date range from your front end
def filters = [
from: from,
to : to
]
Then when you do some validations you want to pass a date range in date objects to your DAO for SQL queries so you do something like the following
if(filters.from && filters.to) {
def normalizedDateRange = DateUtil.buildDateRange(filters.from, filters.to, maxRangeDays)
filters.from = normalizedDateRange.from
filters.to = normalizedDateRange.to
}
This is acceptable and you get away without needing to create a second map with very similar name or such. My question is if this causes too much overhead in languages like this and is this one of the reasons they are slower than JAVA let's say.
Some people say that you could consider the def as using Object in Java so it allocates enough space to store anything or maybe it store a reference and when you store something different it just frees the space it was taking and reallocates new space and just updates the reference?
Would I gain anything by creating a new object/map here and storing the new values there? Or is the gain so little that it's better to take advantage of the sugar syntax and "cheating" of Groovy?