Is it possible to alias a type?

I am learning Vala and want to create a program that uses several Gee.HashSet<string>s and Gee.ArrayList<string>s. So I’d like to create aliases (or whatever the Vala equivalent is) to these, so that I can write, say, var words = new WordSet(); rather than var words = new Gee.HashSet<string>();, and similarly use WordSet as a parameter type rather than Gee.HashSet<string>.

Is this possible? If so, what is the syntax?

The way you’d do it is to define a subclass. It may not be exactly what you want, but it’s very close. In particular, you won’t be able to pass any HashSet to a function that expects a WordSet (but you can do the reverse).

class WordSet : Gee.HashSet<string> {}

void main (string[] args) {
	var wordset = new WordSet();
	foreach (var arg in args) {
		wordset.add(arg);
	}

	foreach (var word in wordset) {
		print("%s\n", word);
	}
}
1 Like

Thank you that works great!