Covarsky 1.3.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Covarsky --version 1.3.1                
NuGet\Install-Package Covarsky -Version 1.3.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Covarsky" Version="1.3.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Covarsky --version 1.3.1                
#r "nuget: Covarsky, 1.3.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Covarsky as a Cake Addin
#addin nuget:?package=Covarsky&version=1.3.1

// Install Covarsky as a Cake Tool
#tool nuget:?package=Covarsky&version=1.3.1                

Licensed under the MIT License NuGet Build Status

Covarsky

Covarsky is a tool that brings co(ntra)variant types to F# or any other .ΝΕΤ language that does not natively support them. Powered by Sigourney, it runs an MSBuild task that modifies assemblies after compilation.

How to use

  1. Add the Covarsky NuGet package to your project.

  2. Create a new source file with two (or one, if you only need one kind of variance) attributes like that (F# example shown):

namespace global

open System

[<AttributeUsage(AttributeTargets.GenericParameter)>]
type internal CovariantOutAttribute() =
    inherit Attribute()

[<AttributeUsage(AttributeTargets.GenericParameter)>]
type internal ContravariantInAttribute() =
    inherit Attribute()

Warning: The attributes must be declared in the global namespace, and must be internal.

  1. You are good to go! Let's see an example.
type MyType<[<CovariantOut>] 'T> =
    abstract GetObject: unit -> 'T

// [...]

let cast (x: MyType<string>): MyType<obj> =
    unbox x

As you have seen, we have to perform manual type casts using unbox because F# does not recognize that MyType's 'T is covariant.

Customizing Covarsky

Using custom attribute names

If for any reason you want to customize the attribute names Covarsky will recognize, you can do it by specifying it in your project file:

<PropertyGroup>
    <CustomInAttributeName>MyLibrary.MakeItContravariantPleaseAttribute</CustomInAttributeName>
    <CustomOutAttributeName>MyLibrary.MakeItContravariantPleaseAttribute</CustomOutAttributeName>
</PropertyGroup>

As you see, fully qualified names are accepted. The attribute classes however still have to be internal and belong to the same assembly.

Disabling Covarsky

Since version 1.3.0 Covarsky is enabled by default when you install the package. If for some reason you want to disable it you can do it with the following property:

<PropertyGroup>
    <CovarskyEnable>false</CovarskyEnable>
</PropertyGroup>

Caveats

  • After Covarsky's execution, the two attributes will not be removed.

  • These two attributes will be ignored if used anywhere but in the generic parameters of an interface or a delegate.

  • Using these two attributes in a generic parameter that is already co(ntra)variant will raise a warning but will be ignored as well.

  • Using both attributes on the same time will raise an error and fail the build (unless something above hasn't already happened).

  • Using the attributes in any other wrong way (such as a contravariant interface with a method that returns the generic type) will not be checked by Covarsky and may break your assembly.

Maintainer(s)

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.2 2,181 9/3/2021
1.4.1 429 7/19/2021
1.4.0 398 4/19/2021
1.3.1 624 11/2/2020
1.3.0 429 10/8/2020
1.2.1 487 9/21/2020
1.2.0 614 3/17/2020
1.0.0 593 2/1/2020

Covarsky was updated to use Sigourney 0.3.0, with the enhancements it brings. No user action is needed.