
-----------------------------------
wtd
Tue Feb 05, 2008 11:38 pm

Any language welcome challenge
-----------------------------------
Well, any language in which one can write a program that satisfies the problem description.  ;)

Create a generic class Foo which takes as its type parameter T any class which answers to a method "baz" which returns an integer.  The generic class Foo must have a constructor which takes an object of type T, storing that as "x".  Foo must have a method qux which returns the result of multiplying x's baz method's return value by three.

-----------------------------------
rdrake
Tue Feb 05, 2008 11:40 pm

RE:Any language welcome challenge
-----------------------------------
Are we to post our solutions here?

-----------------------------------
wtd
Wed Feb 06, 2008 12:28 am

Re: Any language welcome challenge
-----------------------------------
Yes.

To kick it off:

class ['t] foo (x : 't) =
    object
        method qux = x#baz * 3
    end

-----------------------------------
rdrake
Wed Feb 06, 2008 1:20 am

Re: Any language welcome challenge
-----------------------------------
Good old C# 2.0:public interface IWooble
{
	int Wooble();
}

public class WoobleObject : IWooble
{
	public int Wooble()
	{
		return 42;
	}
}

public class Baz where T:IWooble
{
	private T _O;

	public Baz(T o)
	{
		this._O = o;
	}

	public int Ninja()
	{
		return this._O.Wooble() * 3;
	}
}

public class Test
{
	public static void Main(string[] args)
	{
		Baz b = new Baz(new WoobleObject());
		System.Console.WriteLine(b.Ninja());
	}
}

-----------------------------------
OneOffDriveByPoster
Wed Feb 06, 2008 11:11 am

Re: Any language welcome challenge
-----------------------------------
template 
class Foo {

    T x;

public:
    Foo (T const &x) : x(x) { } 
    int qux(void) {
        return x.baz() * 3;
    }   
};Hopefully close enough...

-----------------------------------
wtd
Wed Feb 06, 2008 5:43 pm

RE:Any language welcome challenge
-----------------------------------
Excellent.

-----------------------------------
rdrake
Fri Feb 08, 2008 3:46 am

RE:Any language welcome challenge
-----------------------------------
Improved C# 3.0 version:
public class WoobleObject
{
   public virtual int Wooble() { return 42; }
}

public class Baz where T : WoobleObject
{
   public T O { get; set; }

   public int Ninja() { return this.O.Wooble() * 3; }
}

public class Test
{
   public static void Main(string[] args)
   {
      var b = new Baz() { O = new WoobleObject() };
      System.Console.WriteLine(b.Ninja());
   }
}
EDIT:  Dan, why does uppercase Test get changed to just lowercase test?

-----------------------------------
wtd
Fri Feb 08, 2008 1:34 pm

Re: Any language welcome challenge
-----------------------------------
Scala:

trait Baz { 
    def baz: Int 
}

class Foo[T 