Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

default case matches multiple times for same term in a visit #731

Open
mahills opened this issue Dec 4, 2014 · 6 comments
Open

default case matches multiple times for same term in a visit #731

mahills opened this issue Dec 4, 2014 · 6 comments

Comments

@mahills
Copy link
Member

mahills commented Dec 4, 2014

If I do the following in an otherwise empty module:

import Traversal;
import IO;

void doit() {
    l = [1,"one",2,"two",3,"three"];
    bottom-up visit(l) {
        case int n : println("<n> is an int");
        default: {
            println("Found the default");
        }
    }
}

and run it I see the following:

rascal>doit();
1 is an int
Found the default
Found the default
Found the default
2 is an int
Found the default
Found the default
Found the default
3 is an int
Found the default
Found the default
Found the default
Found the default
Found the default
Found the default
ok

Why is default being triggered so many times? I would think it would be triggered at most 4 times, one for each of the 3 strings in the list, and one for the list as a whole.

@josvanroosmalen
Copy link

Because it will also visit for string chars.

Do tot see what is going PN:

default value x: { println("no visitor for <x>")

@josvanroosmalen
Copy link

Sorry should be:

case value x:  { println("No visitor for <x>"); }

This will give:

rascal>doit();
1 is an int
Found the default one
Found the default ne
Found the default e
2 is an int
Found the default two
Found the default wo
Found the default o
3 is an int
Found the default three
Found the default hree
Found the default ree  
Found the default ee
Found the default e
Found the default [1,"one",2,"two",3,"three"]
ok

@mahills
Copy link
Member Author

mahills commented Dec 5, 2014

Question: is this really the behavior we want? This seems amazingly expensive, and not terribly consistent (matching each character inside the string would make more sense if we were going to do this, I would think).

@PaulKlint
Copy link
Member

As usual, I have executed this example with the compiler, and I think that gives the behavior we want:

import Traversal;
import IO;

void doit() {
    l = [1,"one",2,"two",3,"three"];
    bottom-up visit(l) {
        case int n : println("<n> is an int");
        case value x:  { println("No case for <x>"); }
    }
}

and get:

1 is an int
No case for one
2 is an int
No case for two
3 is an int
No case for three
No case for [1,"one",2,"two",3,"three"]

In the compiler, only top-level string visits match every character.

@mahills
Copy link
Member Author

mahills commented Dec 5, 2014

I think that makes much more sense, this is what I would expect.

@jurgenvinju
Copy link
Member

We should make a separate statement kind for top-level string visits, like edit(s) { case ... }. The current overloading of visit is hard to spot and inconsistent with the case oF visiting a string that is nested.

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

No branches or pull requests

4 participants