rascal
rascal copied to clipboard
False positive: addition and subtraction on datetime values
Describe the bug
The type checker flags addition and subtraction on datetime values as errors.
To Reproduce
datetime hoi = now();
datetime doei = now();
datetime added = hoi + doei;
datetime subtracted = doei - hoi;
Screenshots

Oops, you are completely right: two missing cases. Thanks!
Solving this in the type checker is trivial (have already fixed it). For the compiler I have to study this a bit more: to my surprise IDateTime does not provide add or subtract operations so their implementation must be hidden in the interpreter somewhere ...
I should have checked, but it seems that only subtraction is allowed in the interpreter..
rascal>now() + now()
|prompt:///|(8,3,<1,8>,<1,11>): insert into collection not supported on datetime and datetime
Advice: |http://tutor.rascal-mpl.org/Errors/Static/UnsupportedOperation/UnsupportedOperation.html|
ok
rascal>now() - now()
Duration: duration(0,0,0,0,0,0,0)
https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/interpreter/result/DateTimeResult.java#L357
Indeed!
This means that I will have to port that code to the runtime of the compiler.
Unfortunately yes; this part of the DateTime API is waiting patiently on the introduction of "units and dimensions" in vallang and Rascal, such that datetime - datetime will be an int[Duration] or a real[Duration] in seconds, years, ...
When we have that, the code can move into IDateTime, but before that time it seems clunky to move it there. Of course, it is possible, but we'd have to add more "bootstrap" code in terms of manual declarations of data types for the representation of differences.
Fair enough, let's not go into this rat hole for now :-)
As part of this rewriting I came across very strange stuff around datetimes.
For example there want is also a datetime library, that also has a duration adt.
Yes, way back when we decided that a Duration ADT was a bridge too far for the - operator to produce so I think we settled for a tuple if I recall correctly. For the library that made less sense. Hence we got stuck with two representations for durations. I believe, it was always @mahills perspective that this would be rewritten eventually using a dimension/unit type system.
That is what confused me, it's the inverse, the - operator returns a ADT, while the library returns a tuple.
see:
https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/interpreter/result/DateTimeResult.java#L368-L377
and:
https://github.com/usethesource/rascal/blob/d110bafba97d8dfe4e863754544fa47aa61630ae/src/org/rascalmpl/library/Prelude.java#L420-L425
thanks for the references. that's exactly right. let's leave it for now until we have the dimensions. otherwise, we'd have yet another breaking change in the library and language when we introduce that.
True, I would be surprised if many people use this feature, but I agree to leave it as is.