thanhhovn / sharpkit

Automatically exported from code.google.com/p/sharpkit
0 stars 0 forks source link

"is" operator and ints #347

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
An "is" check against int is generated into "true".

            object aString = "striny";
            object anInt = 5;

            if (aString is string)
            {
                new jQuery(HtmlContext.document.body).append("aString is a String<br/>");
            }
            else
            {
                new jQuery(HtmlContext.document.body).append("aString isn't a String<br/>");
            }

            if (aString is int)
            {
                new jQuery(HtmlContext.document.body).append("aString is an int<br/>");
            }
            else
            {
                new jQuery(HtmlContext.document.body).append("aString isn't an int<br/>");
            }

            if (anInt is string)
            {
                new jQuery(HtmlContext.document.body).append("anInt is a String<br/>");
            }
            else
            {
                new jQuery(HtmlContext.document.body).append("anInt isn't a String<br/>");
            }

            if (anInt is int)
            {
                new jQuery(HtmlContext.document.body).append("anInt is an int<br/>");
            }
            else
            {
                new jQuery(HtmlContext.document.body).append("anInt isn't an int<br/>");
            }

generates

var aString = "striny";
            var anInt = 5;
            if (Is(aString, System.String.ctor))
            {
                $(document.body).append("aString is a String<br/>");
            }
            else
            {
                $(document.body).append("aString isn\'t a String<br/>");
            }
            if (true)
            {
                $(document.body).append("aString is an int<br/>");
            }
            else
            {
                $(document.body).append("aString isn\'t an int<br/>");
            }
            if (Is(anInt, System.String.ctor))
            {
                $(document.body).append("anInt is a String<br/>");
            }
            else
            {
                $(document.body).append("anInt isn\'t a String<br/>");
            }
            if (true)
            {
                $(document.body).append("anInt is an int<br/>");
            }
            else
            {
                $(document.body).append("anInt isn\'t an int<br/>");
            }

which then results aString being both a String and an int!

aString is a String
aString is an int
anInt isn't a String
anInt is an int

/* Generated by SharpKit 5 v5.3.4 */

Original issue reported on code.google.com by co...@gravill.com on 28 Jan 2014 at 5:51

GoogleCodeExporter commented 8 years ago
Have you tried setting:
[assembly: JsType(TargetType=typeof(int), OmitCasts=false, NativeCasts=false)]

Basically, if OmitCasts is true, any casting will be removed, and if any code 
is asking "x is Type", it will be converted to "true"

Original comment by DanelK...@gmail.com on 5 Feb 2014 at 9:08

GoogleCodeExporter commented 8 years ago
In this instance I need the type check. It's used in a heterogeneous storage of 
either an integer or a map of integers. The int check is actually doing what 
you say - the type check is being converted into "true". I only see this for 
checks against int so far.

I've worked around the issue using a more verbose form:

o.GetType() == typeof(int)

Original comment by co...@gravill.com on 10 Feb 2014 at 2:35

GoogleCodeExporter commented 8 years ago
Yes, casts are omitted by default in SharpKit.JavaScript.dll:
[assembly: JsType(TargetType = typeof(int), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(object), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(byte), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(short), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(double), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(float), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(uint), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(ushort), OmitCasts = true)]
[assembly: JsType(TargetType = typeof(decimal), OmitCasts = true)]

Would you like to try removing those, and extend $Cast() and $Is() to work with 
them as expected? It would give you a lot of flexibility.

Original comment by DanelK...@gmail.com on 11 Feb 2014 at 9:41