Pregunta

Con el siguiente código, el éxito y el fracaso se compilan en 2 clases separadas.¿Cómo puedo proporcionar atributos personalizados para el éxito y el fracaso?

type Result<'TSuccess,'TFailure> = 
    | Success of 'TSuccess
    | Failure of 'TFailure

EDITARDesde Success y Failure dan como resultado clases, necesito decorarlas con un atributo compatible con clases AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Interface)

¿Puedo hacer que esto funcione?¿Si no, porque no?

[<ClassAttribute>]
type Result<'TSuccess,'TFailure> = 
    | [<ClassAttribute>] Success of 'TSuccess
    | [<ClassAttribute>] Failure of 'TFailure
¿Fue útil?

Solución

Los atributos se pueden colocar entre | y el nombre del caso sindical.

Aquí hay un ejemplo sencillo.

open System
type t =  |[<Obsolete("hello")>]A

Otros consejos

Un atributo en un caso de unión se asocia con el estático New<Case> método que devuelve la subclase para ese caso o con el Case propiedad para un caso sin valor.Con esta definición en F#...

type Result<'TSuccess,'TFailure> = 
    | [<Obsolete>] Success of 'TSuccess
    | Failure of 'TFailure
    | [<Obsolete>] NotSure

Utilice este fragmento de C# para invocar la descompilación...

var y = Result<string, string>.NewSuccess("yay");
var z = Result<string, string>.NotSure;

Al ir a la definición de NewSuccess aparecerá la definición de C# de Resultado. from metadata mostrando el atributo obsoleto.

[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; }
}

Para un identificador de caso como NotSure anterior que no tiene valores, el caso se convierte en una propiedad en lugar de una subclase y el atributo obsoleto adjunto al caso se compila en el aire en lo que respecta a la descompilación de metadatos...

[CompilerGenerated]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[DebuggerNonUserCode]
public static Result<TSuccess, TFailure> NotSure { get; }

Mirando a través de ILdasm, los atributos están bien en ambos...

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> )
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top