AndrewP
AndrewP

Reputation: 21

How to define a function that returns a function with signature dependent on parameters

I'd like to get F# code that leads to same behavior as following C# code:

public abstract class ModelBase<TIn, TOut>
{
  internal abstract Func<TIn, TOut> GetFuncInternal();
}

public class Model1 : ModelBase<Tuple<int, int>, int>
{
  private int _val;
  public Model1(int val)
  {
    _val = val;
  }

  internal override Func<Tuple<int, int>, int> GetFuncInternal()
  {
    return _ => _val; 
  }
}

public class Model2 : ModelBase<Tuple<int, double>, double>
{
  private double _val;
  public Model2(double val)
  {
    _val = val;
  }

  internal override Func<Tuple<int, double>, double> GetFuncInternal()
  {
    return _ => _val;
  }
}

public class Model3 : ModelBase<int, bool>
{
  internal override Func<int, bool> GetFuncInternal()
  {
    return _ => true;
  }
}

public static class Helpers
{
  public static Func<TIn, TOut> GetFunc<TIn, TOut>(ModelBase<TIn, TOut> model)
  {
    return model.GetFuncInternal();
  }
}

So code above can be used in the following way:

var func1 = Helpers.GetFunc(new Model1(1));
var func2 = Helpers.GetFunc(new Model2(1.0));
var func3 = Helpers.GetFunc(new Model3());

As you can see the type of GetFunc result depends on type of its parameters.

In F# Models defined as discriminated union:

type Model = 
  | Model1 of int
  | Model2 of double
  | Model3

And the question is: how to define GetFunc in F#? I need something like that (of course following sample doesn't compiled because of type mismatch in pattern matching):

let GetFunc (m: Model) = 
  match  m with
  | Model1 i -> fun (x: int, y: int) -> i
  | Model2 d -> fun (x: int, y: double) -> d
  | Model3 ->   fun (x: int) -> true

Upvotes: 0

Views: 212

Answers (1)

Brian
Brian

Reputation: 118865

Don't define Model like that. Actually, it's hard to tell what you're doing, but this smells like a domain where you don't need to define any types. Just use lambdas and you don't need any of these model types. What are you actually trying to do?

Upvotes: 2

Related Questions