Wallace
Wallace

Reputation: 17369

F# attributes, typeof, and "This is not a constant expression"

EDIT: Added a more complete example, which clarified the problem.

Some .NET attributes require a parameter of type Type. How does one declare these parameters in F#?

For example, in C# we can do this:

[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Truck))]
class Vehicle { }
class Car : Vehicle { }
class Truck : Vehicle { }

But, in F# the following...

[<XmlInclude(typeof<Car>)>]
[<XmlInclude(typeof<Truck>)>]
type Vehicle() = class end
type Car() = inherit Vehicle()
type Truck() = inherit Car()

...results in a compiler error: This is not a constant expression or valid custom attribute value.

Upvotes: 6

Views: 1112

Answers (2)

Gene Belitski
Gene Belitski

Reputation: 10350

You should address a circular type dependency introduced by forward usage of types in attributes. The snippet below shows how this can be done in F#:

// Compiles OK
[<AttributeUsage(AttributeTargets.All, AllowMultiple=true)>]
type XmlInclude(t:System.Type) =
   inherit System.Attribute()

[<XmlInclude(typeof<Car>)>]
[<XmlInclude(typeof<Truck>)>]
type Vehicle() = class end
and Car() = inherit Vehicle()
and Truck() = inherit Car()

Upvotes: 5

Tomas Petricek
Tomas Petricek

Reputation: 243041

Can you try putting together a more complete example that gives the error? I just quickly tried something similar and it works fine (in F# 3.0 in Visual Studio 2012):

type Car = C

type XmlInclude(typ:System.Type) =
  inherit System.Attribute()

[<XmlInclude(typeof<Car>)>]
let foo = 0

I guess there is some tiny detail somewhere that confuses the F# compiler for some reason - but it should understand typeof (which is, in reality, a function) and allow its use in attributes.

Upvotes: 2

Related Questions