hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

Problem with type inference for delegates and generic types.

Last post 05-30-2008, 6:07 by IanG. 0 replies.
Sort Posts: Previous Next
  •  05-30-2008, 6:07 6037

    Problem with type inference for delegates and generic types.

    Hi,

    I'm trying to figure a type inference problem but struggling somewhat. I'm interacting with a C# generic class

        abstract class BaseClass<StoreType, FunctionType>
        {
            public void AddFunction(int i, FunctionType f) { ... }
        }
        
        public delegate double SimpleFunction(double S);
        public delegate double ComplexFunction(double S, double T);
        
        class SimpleType : BaseClass<double, SimpleFunction>
        { ... }
        class ComplexType : BaseClass<long, ComplexFunction>
        { ... }
        
    I'm trying to call AddFunction on the types

    What I have at the minute is

    let rec apply_between start_date end_date (var : #BaseClass<'a, 'b>) fn =
        if start_date < end_date then     
            var.AddFunction(start_date, fn)
            apply_between (start_date + 1) end_date var fn

    type FSExample =
        inherit CSBaseClass as base
        
        let simple = new SimpleType()
        let complex = new ComplexType()
        
        let simple_fn x = x + 1.0;
        let complex_fn x y = x + y + 1.0;
            
        override self.Initialise() =
            // 10 20 are actually DateTimes but I don't think changes anything
            apply_between 10 20 simple (new SimpleFunction(simple_fn))
            apply_between 10 20 complex (new ComplexFunction(complex_fn))
        

    I appear to need the (var : #BaseClass<'a, 'b>) to stop errors about ambiguous
    types (correct?). It seems to me that I should be able to get rid of the explicit need
    to create the Simple/ComplexFunction delegates and get them implied. But I haven't
    managed it. I tried

    let rec apply_between start_date end_date (var : #BaseClass<'a, 'b>) fn =
        if start_date < end_date then     
            var.AddFunction(start_date, new 'b (fn))
            apply_between (start_date + 1) end_date var fn
            
    But get an error "Calls to object constructors on type parameters can not be given arguments".

    Is there someway I can move the construction of the delegate inside apply_between in some
    generic way? (Or since there's only a couple of options for this set up some explicit switch
    on the types?)

    Many Thanks
    Ian
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems