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?