0 Replies - 1199 Views - Last Post: 11 April 2010 - 04:21 PM

#1 athlon32  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 363
  • Joined: 20-August 08

Vala - A Pair Class

Posted 11 April 2010 - 04:21 PM

Description: Just compile :)A pair is a class that can hold two values as a pair. As far as I know, Vala doesn't have one in any standard library, so you could use this. Enjoy :)
using GLib;

public class Pair<T1, T2> : GLib.Object {
	public Pair.init(T1 f, T2 s) { Make_Pair(f, s); }
	public Pair() {} // just a default ctor
	public void Make_Pair(T1 f, T2 s) { first = f; second = s; }
	
	public bool Is_Equal(Pair<T1, T2> other) {
		if(this.first == other.first && this.second == other.second) 
			return true;
		else return false;
	}
 
	public T1 first;
	public T2 second;
} 

// THIS IS JUST AN EXAMPLE OF USING IT, GET RID OF IT IF YA WANT :)
void main() {
	var person1 = new Pair<string, int>.init("Mario", 18);
	var person2 = new Pair<string, int>();
	var person3 = new Pair<string, int>();

	person2.first = "Luigi";
	person2.second = 17;
	person3.Make_Pair("Peach", 16); // x)
		

	stdout.printf("The first person's name is %s, and he's %d years old.n",
				  person1.first, person1.second);	
	stdout.printf("The second person's name is %s, and he's %d years old.n",
				  person2.first, person2.second);	
	stdout.printf("The third person's name is %s, and she's %d years old.n",
				  person3.first, person3.second);	
	
	if(!person1.Is_Equal(person2)) {
		stdout.printf("%s is not the same as %sn", person1.first,
					 person2.first);
	}
}




Is This A Good Question/Topic? 0
  • +

Page 1 of 1