Ask Your Question
0

Implicit variable type in function parameter

asked Dec 19 '11

Johanneke gravatar image Johanneke
77 4 10

In the console I can assign the empty set to a variable, without specifying the type of the variable. The type is then set[void]. However, I can still add values of different types to the set, such as a tuple[int,str]. The type of the variable changes to rel[int,str] after this.

rascal>d = {};
set[void]: {}
rascal>d = d + <1,"a">;
rel[int, str]: {<1,"a">}
rascal>d;
rel[int, str]: {<1,"a">}

After this, I would expect the following code to work:

data TestADT = testADT(rel[int,str]);
public void f(testADT(r)) {
  r = r + <2,"b">;
}

However, when I try to run this using "f(testADT({}))", I get the message "Expected set[void], but got rel[int, str]". I would expect the pattern matching on testADT to make r of the type rel[int,str], but apparently this doesn't happen?

What is going on? And what can I do to be able to add values of type tuple[int,str] to r?

delete close flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered Dec 29 '11

JurgenVinju gravatar image JurgenVinju flag of Netherlands
554 6 20
http://jurgen.vinju.org/

Rascal does not allow type inference for function parameters, and not in variables nested in patterns in function definitions either. The static checker will warn you in the future about this. What you are seeing now is some arbitrary undefined behavior. Your solution is the right one!

link delete flag offensive edit

Comments

This design decision was made to make sure small errors dont lead to very complex and nonlocal error messagesmessages caused by type inference inferring all over the place. Its kept inside function bodies and on the repl only.

JurgenVinju (Dec 29 '11)edit
0

answered Dec 19 '11

Johanneke gravatar image Johanneke
77 4 10

Haha, so I found the answer on how to solve my problem:

data TestADT = testADT(rel[int,str]);
public void f(testADT(rel[int,str] r)) {
  r = r + <2,"b">;
}

Although I'm still curious as to why it won't work when just pattern matching a variable, why does it have to be a variable declaration?

link delete flag offensive edit

Your answer

Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
[hide preview]
Also see the Rascal Tutor.

Question tools

Follow

subscribe to rss feed

Stats

Asked: Dec 19 '11

Seen: 14 times

Last updated: Dec 29 '11