Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

attributes

f#

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.

like image 839
Wallace Kelly Avatar asked Aug 22 '13 00:08

Wallace Kelly


2 Answers

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()
like image 196
Gene Belitski Avatar answered Nov 01 '22 04:11

Gene Belitski


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.

like image 34
Tomas Petricek Avatar answered Nov 01 '22 04:11

Tomas Petricek