Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

rascal - How to create a set containing different types

How can I create a set containing a loc, int and string? In the example below it seems my map is converted to the container type ' value'. I would like to use the characteristics of a set; Only one element should be contained in the set and the order does not matter . In the example below the same elements are contained in what I was hoping to be a set.

rascal>mySet = ();
map[void, void]: ()
rascal>mySet += {<|project://Test/|, 1, "test"> };
value: {
  (),
  <|project://Test|,1,"test">
}
rascal>mySet += {<|project://Test/|, 1, "test"> };
value: {
  <|project://Test|,1,"test">,
  {
    (),
    <|project://Test|,1,"test">
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Something weird is going on . First mySet is a map, and then you add a set to it and the variable mySet is suddenly a set that contains the previous map and the new element you added. It looks to me that += has a bug.

If you want to add several elements to a set, you should start with a set and add elements to it like so:

rascal>mySet = { |project://Test| ,1, "test" }; // simply create the set with 3 different elements in it
set[value]: { |project://Test| ,1, "test" }

Or you could start with an empty set and add elements:

rascal>mySet = {};
set[void]: {}
rascal>mySet += 1;
set[int]: {1}
rascal>mySet += |project://Test|;
set[value]: {1, |project://Test|}
rascal>mySet += "test";
set[value]: {1, |project://Test|, "test"}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...