Why does F# not allow multiple attributes where C# does?

788 Views Asked by At

The following code compiles in C#:

[ContentType("text")]
[ContentType("projection")]
public class Class1
{
}

The following code in F# does not compile:

[<ContentType("text")>]
[<ContentType("projection")>]
type Class1() = class end

The compile error in F# is: "The attribute type 'ContentTypeAttribute' has 'AllowMultiple=false'. Multiple instances of this attribute cannot be attached to a single language element."

By decompiling ContentType, I can see that ContentType inherits from MultipleBaseMetadataAttribute which has 'AllowMultiple=true' in the AttributeUsage.

In fact, it seems like F# does not inherit the AttributeUsage from the parent class.

[<AttributeUsage(AttributeTargets.Class, AllowMultiple = true)>]
type FooAttribute() = 
    inherit Attribute()

type BarAttribute() =
    inherit FooAttribute()

[<Foo>]
[<Foo>]
type MyClassCompiles() = class end

where

[<Bar>]
[<Bar>]
type MyClassDoesNotCompile() = class end
1

There are 1 best solutions below

1
On BEST ANSWER

Looks like a bug. Email fsbugs[at]microsoft.com. Here's another apparent bug: it doesn't appear to honor AttributeTargets:

[<AttributeUsage(AttributeTargets.Enum)>]
type FooAttribute() = 
  inherit Attribute()

[<Foo>]
type T = struct end //happily compiles