Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a delegate as a type parameter and using it throws error CS0314

I'm trying to pass a delegate type as a type parameter so that I can then use it as a type parameter later on in the code, like so:

// Definition
private static class Register
{
  public static FunctionObject Create<T>(CSharp.Context c, T func)
  {
    return new IronJS.HostFunction<T>(c.Environment, func, null);
  }
}

// Usage
Register.Create<Func<string, IronJS.CommonObject>>(c, this.Require);

However, the C# compiler complains:

The type 'T' cannot be used as type parameter 'a' in the generic type or method
'IronJS.HostFunction<a>'. There is no boxing conversion or type parameter
conversion from 'T' to 'System.Delegate'."

I attempted to fix this by appending "where T : System.Delegate" to the function, however, you can't use System.Delegate as a restriction on type parameters:

Constraint cannot be special class 'System.Delegate'

Does anyone know how to resolve this conflict?

DOESN'T WORK (Argument and return type information is lost during cast):

Delegate d = (Delegate)(object)(T)func;
return new IronJS.HostFunction<Delegate>(c.Environment, d, null);
like image 464
June Rhodes Avatar asked May 29 '11 11:05

June Rhodes


1 Answers

If you look at https://github.com/fholm/IronJS/blob/master/Src/IronJS/Runtime.fs you'll see:

and [<AllowNullLiteral>] HostFunction<'a when 'a :> Delegate> =
  inherit FO
  val mutable Delegate : 'a

  new (env:Env, delegateFunction, metaData) =
  {
      inherit FO(env, metaData, env.Maps.Function)
      Delegate = delegateFunction
  }

In other words, you cannot use C# or VB to write your function because it requires using System.Delegate as a type constraint. I recommend either writing your function in F# or using reflection, like this:

public static FunctionObject Create<T>(CSharp.Context c, T func)
{
  // return new IronJS.HostFunction<T>(c.Environment, func, null);
  return (FunctionObject) Activator.CreateInstance(
    typeof(IronJS.Api.HostFunction<>).MakeGenericType(T),
    c.Environment, func, null);
}   
like image 192
Gabe Avatar answered Oct 27 '22 00:10

Gabe