agarman
agarman

Reputation: 447

F#: Is there a way to add attributes to the Type output by an ObjectExpression

Adding attributes to members works well because the syntax is exactly like it would be when defining the member in a type.

[<TypeAttribute>]
let Foo () =
  {
    new Baz with
      [<MethodAttribute>]
      member x.Booga () = ()
  }

Above fails to compile with AttributeUsage exception of "This attribute is not valid for use on this language element".

This error doesn't go away even if I add an Attribute hint:

[<type: TypeAttribute>]

If the attribute is moved next to the object expression, where it seems to make sense to go, the code doesn't even parse.

let Foo () =
  [<TypeAttribute>]
  {
    new Baz with
      member x.Booga () = ()
  }

The parse error is "Unexpected symbol '[<' in binding.

let Foo () =
  {
    [<TypeAttribute>]
    new Baz with
      member x.Booga () = ()
  }

Moving the attribute here fails to parse with "Unexpected symbol '[<' in expression. Expected '}' or other token", "Unmatched '{'" and "Unexpected keyword 'member' in definition...". The worst result yet.

So at this point my option is to give up using an object expression in favor of a private type in the module.

[<TypeAttribute>]
type private myBaz () =
 interface Bax with
   member x.Booga () = ()

let Foo () = new myBaz ()

That works fine, but I'd prefer an alternative that allows object expressions to be used instead.

Is there an alternative?

Upvotes: 2

Views: 687

Answers (1)

Brian
Brian

Reputation: 118865

Attributes cannot be used on object expressions, though it does seem like a reasonable suggestion for a future extension to the language (I'll log a suggestion).

http://msdn.microsoft.com/en-us/library/dd233179(VS.100).aspx

Upvotes: 6

Related Questions