8

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
Johan
  • 660
  • 1
  • 6
  • 13
  • Incidentally, this sounds like an exact duplicate of the following question, but it's actually not. This is about F# honoring the AllowMultiple setting on an attribute, and the other question is about the syntax for combining multiple attributes in a single statement. http://stackoverflow.com/questions/9620712/is-it-possible-to-combine-multiple-attributes-in-f – Joel Mueller Sep 27 '12 at 13:44

1 Answers1

8

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
Daniel
  • 47,404
  • 11
  • 101
  • 179