On 29/08/2013 4:52 AM, William ML Leslie wrote:
Sandro: see, the problem is with different values of T. I can solve
this problem a few ways, and in a language without style perhaps
neither feels particularly uncomfortable.  To use the vet example, I
could have

when (farDog, resDog => { // farDog : Ref Dog
   when (farCar, resCar => { // farCar : Ref Car
     resCar.driveTo(vet, resDog);
   });
});

Or I could correctly type a two-argument form

public static void when<T0, T1>( Ref T0, Ref T1, (T0, T1) => Void );

Yes, this is what I meant by non-curried functions by default complicating the interface, but there are standard techniques to handle this.For instance, the CLR has LINQ's monadic interface which can encapsulate the types of previous 'when' callsin a cleaner way than your first example:

Future<Dog> farDog;
Future<Car> farCar;
var result = from resDog in farDog
             from resCar in farCar
             select resCar.DriveTo(vet, resDog);

Which desugars into:

var result = farDog.SelectMany(resDog => resCar, (resDog, resCar) => resCar.DriveTo(vet, resDog));

As long as you have lambdas, the 'when' operator looks straightforward enough, unless you have some objection to the above? Perhaps monadic composition is too strong?

While a little more awkward in C#, you can even use an HList:

interface IRow { }
struct Term : IRow { }
struct Entry<TValue, TNext> : IRow
  where TNext : IRow
{
  public TValue Value {get; private set;}
  public TNext Next {get; private set;}
}

public static void When<T>(ref T resolved, T => void)
  where T : IRow

I've only really used this last technique in a PIC that can dispatch on an arbitrary number of parameters, ie. call site builds an IRow encapsulating its parameters and the Dispatch<T> where T : IRow can exploit runtime types to efficiently access the dispatching delegate cached in a static field. Of course, this poor imitation of extensible records is only necessary because currying isn't pervasive and/or because function arguments aren't tuples and record extension isn't available. I don't think you need anything fancier to type 'when' though.

Sandro

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature

_______________________________________________
bitc-dev mailing list
[email protected]
http://www.coyotos.org/mailman/listinfo/bitc-dev

Reply via email to