Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Switch statements with string keys #8

Closed
volkanceylan opened this issue Aug 16, 2012 · 9 comments
Closed

Switch statements with string keys #8

volkanceylan opened this issue Aug 16, 2012 · 9 comments

Comments

@volkanceylan
Copy link
Contributor

When i write some switch statement that operates on string like:

    public override void CreateEditor(string op)
    {
        switch (op)
        {
            case FilterOp.BW:
                Container.html("<input type=\"text\"/><span class=\"sep\">-</span><input type=\"text\"/>");
                return;
            case FilterOp.GE:
                ....
        }

        base.CreateEditor(op);
    }

I get an error like below:

Error 24 System.Collections.Generic.Dictionary<string,int>.Dictionary(int)' is obsolete:This is only for use by the c# compiler, and cannot be used for generating script.'

A field or property System.Collections.Generic.Dictionary<TKey,TValue>' cannot be initialized with a collection object initializer because typeSystem.Collections.Generic.Dictionary<string,int>' does not implement `System.Collections.IEnumerable' interface

@volkanceylan
Copy link
Contributor Author

noticed that there is a problem with references, as project still uses a mix of scriptsharp mscorlib.dll and saltarelle dlls. please ignore this till i resolve issue myself.

@erik-kallen
Copy link
Contributor

I do believe this is a real issue

@erik-kallen erik-kallen reopened this Aug 16, 2012
@erik-kallen
Copy link
Contributor

Turned out I was wrong. Switching on strings does work.

@volkanceylan
Copy link
Contributor Author

Ok, now my project does build but case statements still don't work, so i had to comment them.

You are right, switching on strings does work, but if strings are set of constants defined in another class, it doesn't.

I have a class like this:

public static class FilterOp
{
    ...
    public const string EQ = "eq";
    public const string NE = "ne";
    ...
}

If i change case statement to use inline strings it works.

I'm trying to convert a big ScriptSharp project (about 5 MB of *.cs files) to your compiler. Had some issues but i could manage it to build with several changes / search replaces mostly.

I'll let you know about issues i encountered later, so maybe you can write them in your wiki, and it may help others.

Anyway, your project looks promising. I'd like to contribute.

@erik-kallen
Copy link
Contributor

Can't reproduce this.


public static class FilterOp
{
    public const string EQ = "eq";
    public const string NE = "ne";
}
public class Driver {
    public int Main() {
        string s = null;
        switch (s) {
            case FilterOp.EQ: return 1;
            case FilterOp.NE: return 2;
        }
        return 0;
    }
}

generates

////////////////////////////////////////////////////////////////////////////////
// Driver
Driver = function() {
};
Driver.prototype = {
    main: function() {
        var s = null;
        switch (s) {
            case 'eq': {
                return 1;
            }
            case 'ne': {
                return 2;
            }
        }
        return 0;
    }
};
////////////////////////////////////////////////////////////////////////////////
// FilterOp
FilterOp = function() {
};
Driver.registerClass('Driver', Object);
FilterOp.registerClass('FilterOp', Object);
FilterOp.EQ = 'eq';
FilterOp.NE = 'ne';

Perhaps you need to update-package to get the latest version. For some reason, NuGet seems to install an old version when installing the compiler as a dependency.

@erik-kallen
Copy link
Contributor

And, contributions are of course welcome. Drop me an email and we can discuss it more.

@volkanceylan
Copy link
Contributor Author

Cloned your git repository but still getting similar error in switch statements.

Created an empty project for testing but this time got a Internal Compiler Error:

Can you please confirm this works with yours?

(If i decrease number of case statements i got no error)

using System;
using System.Text;
using System.Html;

namespace TestLibrary1
{
    public static class FilterOp
    {
        public const string IsTrue = "true";
        public const string IsFalse = "false";
        public const string Contains = "contains";
        public const string StartsWith = "startswith";
        public const string EQ = "eq";
        public const string NE = "ne";
        public const string GT = "gt";
        public const string GE = "ge";
        public const string LT = "lt";
        public const string LE = "le";
        public const string BW = "bw";
        public const string IN = "in";
        public const string IsNull = "isnull";
        public const string IsNotNull = "isnotnull";
    }

    public class TestClass
    {
        public static void Test(string testing)
        {
            switch (testing)
            {
                case FilterOp.BW:
                    Window.Alert("hello");
                    return;
                case FilterOp.GE:
                case FilterOp.GT:
                case FilterOp.LT:
                case FilterOp.LE:
                case FilterOp.EQ:
                case FilterOp.NE:
                    Window.Alert("hello");
                    return;
            }
        }
    }

}

In test project i got:

Error   7   Internal Compiler Error (0xc0000005 at address 61CBC3C9): likely culprit is 'CODEGEN'.

An internal error has occurred in the compiler. To work around this problem, try simplifying or changing the program near the locations listed below. Locations at the top of the list are closer to the point at which the internal error occurred. Errors such as this can be reported to Microsoft by using the /errorreport option.
    TestLibrary1

In my main project i still have:

Error   7   The best overloaded method match for `System.Collections.Generic.Dictionary<string,int>.Dictionary(System.Collections.Generic.JsDictionary<string,int>)' has some invalid arguments

Argument `#1' cannot convert `int' expression to type `System.Collections.Generic.JsDictionary<string,int>' 

@erik-kallen
Copy link
Contributor

The first internal compiler error is due to the work-around you are using to get Visual Studio IntelliSense working, it's an internal error in csc.exe, not in Saltarelle. Most likely due to a missing member in my runtime lib.

@erik-kallen
Copy link
Contributor

I just managed to reproduce and resolve the issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants