custom class attribute for discriminated union case
-
21-12-2019 - |
Question
With the code below, Success and Failure are compiled into 2 separate classes. How can I provide custom attributes for Success and Failure?
type Result<'TSuccess,'TFailure> =
| Success of 'TSuccess
| Failure of 'TFailure
EDIT
Since Success
and Failure
result in classes I need to decorate them with class-compatible attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface)
Can I get this to work? If not, why not?
[<ClassAttribute>]
type Result<'TSuccess,'TFailure> =
| [<ClassAttribute>] Success of 'TSuccess
| [<ClassAttribute>] Failure of 'TFailure
Solution
Attributes can be placed between the |
and the name of the union case.
Here is a simple example
open System
type t = |[<Obsolete("hello")>]A
OTHER TIPS
An attribute on a union case gets associated with the static New<Case>
method returning the subclass for that case or with the Case
property for a valueless case. With this definition in F# ...
type Result<'TSuccess,'TFailure> =
| [<Obsolete>] Success of 'TSuccess
| Failure of 'TFailure
| [<Obsolete>] NotSure
Use this C# snippet to invoke the decompilation ...
var y = Result<string, string>.NewSuccess("yay");
var z = Result<string, string>.NotSure;
Going to the definition of NewSuccess will bring up the C# definition of Result from metadata
showing the obsolete attribute.
[Obsolete]
public static Result<TSuccess, TFailure> NewSuccess(TSuccess item);
[Serializable]
[DebuggerDisplay("{__DebugDisplay(),nq}")]
public class Success : Result<TSuccess, TFailure>
{
[CompilationMapping(SourceConstructFlags.Field, 0, 0)]
[CompilerGenerated]
[DebuggerNonUserCode]
public TSuccess Item { get; }
}
For a case identifier like NotSure above that has no values, the case becomes a property rather than a subclass and the obsolete attribute attached to the case is compiled into thin air as far as the metadata decompilation goes ...
[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[DebuggerNonUserCode]
public static Result<TSuccess, TFailure> NotSure { get; }
Looking through ILdasm, the attributes are there alright on both ...
Method #3 (06000003)
-------------------------------------------------------
MethodName: NewSuccess (06000003)
Flags : [Public] [Static] [ReuseSlot] (00000016)
RVA : 0x00002064
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: GenericInst Class Result`2< Var!0, Var!1>
1 Arguments
Argument #1: Var!0
1 Parameters
(1) ParamToken : (08000001) Name : item flags: [none] (00000000)
CustomAttribute #1 (0c000011)
-------------------------------------------------------
CustomAttribute Type: 0a00000b
CustomAttributeName: System.ObsoleteAttribute :: instance void .ctor()
Length: 4
Value : 01 00 00 00 > <
ctor args: ()
CustomAttribute #2 (0c000012)
-------------------------------------------------------
CustomAttribute Type: 0a00000c
CustomAttributeName: Microsoft.FSharp.Core.CompilationMappingAttribute :: instance void .ctor(value class Microsoft.FSharp.Core.SourceConstructFlags,int32)
Length: 12
Value : 01 00 08 00 00 00 00 00 00 00 00 00 > <
ctor args: ( <can not decode> )
Method #7 (06000007)
-------------------------------------------------------
MethodName: get_NotSure (06000007)
Flags : [Public] [Static] [ReuseSlot] (00000016)
RVA : 0x0000208c
ImplFlags : [IL] [Managed] (00000000)
CallCnvntn: [DEFAULT]
ReturnType: GenericInst Class Result`2< Var!0, Var!1>
No arguments.
CustomAttribute #1 (0c000030)
-------------------------------------------------------
CustomAttribute Type: 0a00000b
CustomAttributeName: System.ObsoleteAttribute :: instance void .ctor()
Length: 4
Value : 01 00 00 00 > <
ctor args: ()
CustomAttribute #2 (0c000031)
-------------------------------------------------------
CustomAttribute Type: 0a00000c
CustomAttributeName: Microsoft.FSharp.Core.CompilationMappingAttribute :: instance void .ctor(value class Microsoft.FSharp.Core.SourceConstructFlags,int32)
Length: 12
Value : 01 00 08 00 00 00 02 00 00 00 00 00 > <
ctor args: ( <can not decode> )