scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Feature Request: == and Dates

Open michaelaird opened this issue 13 years ago • 4 comments

For some reason, in Javascript when you compare 2 date objects with == (or ===), it compares object references instead of values. (http://stackoverflow.com/questions/8245494/javascript-datetime-compare?lq=1). Obviously, this is not the expected behaviour when the other comparisons (>,<,etc) work fine.

Would it be possible to have the compiler compensate for this and emit something like

date1.getTime() === date2.getTime()

michaelaird avatar Aug 10 '12 14:08 michaelaird

Interesting... yes, this is something the compiler should take care of.

If you'd like this to look into this, this is likely where the change needs to be made: https://github.com/nikhilk/scriptsharp/blob/master/src/Core/Compiler/Compiler/ExpressionBuilder.cs#L246

nikhilk avatar Sep 25 '12 01:09 nikhilk

You'll need to check that the dates are not null prior to invoking getTime().

strygo avatar Sep 25 '12 14:09 strygo

@nikhilk i had a look at that code you referenced and i think i sort of get it but i'm not entirely sure how to do the "transform" on the sides of the expression to call getTime (and check for null). Is there an example of that anywhere that i can look at?

michaelaird avatar Sep 25 '12 14:09 michaelaird

Good point @strygo - we should create a method such as Date.equals in mscorlib.js that includes null checking.

@michaelaird ... I would have to try to be sure (this is github coding), but I think this is what would need to be done (around line 439):

if ((operatorType == OperatorType.EqualEqualEqual) &&
    (leftExpression.EvaluatedType == dateType) && (rightExpression.EvaluatedType == dateType)) {
    MethodSymbol equalsMethod = (MethodSymbol)dateType.GetMember("Equals");

    MethodExpression equalsExpression = new MethodExpression(new TypeExpression(mathType, SymbolFilter.Public | SymbolFilter.StaticMembers), equalsMethod);
    equalsExpression.AddParameterValue(leftExpression);
    equalsExpression.AddParameterValue(rightExpression);

    return equalsExpression;
}

If you look for similar patterns in that file, you'll find a number of other scenarios where expression trees are transformed, and new ones are created programmatically.

Let me know if that helps... if it doesn't work, let me know, and I'll look into this for the next release.

nikhilk avatar Sep 25 '12 17:09 nikhilk