Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Properly implement F# Unit in C#

This question is not about C#/F# compatibility as in this one.

I'd like to know the proper way to implement a type like F# Unit in place of using void.

Obviously I'll discard this result and I'll not expose this type to the outside world.

Wondering if an empty class could impersonate this role.

internal class Unit
{
}

For example in language-ext library, the author used a struct.

Is there any benefit in this choice?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

I'm not sure what is the best way to define Unit for usage from C#. It might differ from how this is done in F# (because in F#, the compiler hides the usage in a way).

However, you can actually find the implementation of F# unit in the core library:

  • The public interface is defined in prim-types.fsi
  • The implementation is provided in prim-types.fs

Here are the key points about the F# unit implementation

  • It implements GetHashCode and Equals in the same way to the Rx version
  • It is IComparable and all values of the unit type are equal
  • Most importantly, it has a private constructor and so you cannot create a new unit value. It also has no default instance (unlike the Rx unit) and so in F#, all unit values are actually represented as null. However, the language/compiler generally hide this fact.

So, it sounds like the only difference in F# is that it uses the null value. If you want to use unit explicitly, this might not be the best choice. However, if you have Unit.Default then you are ultimately defining a type with two possible values, because it can be either Unit.Default or null (and so it is not really a unit!)

over 4 years ago · Santiago Trujillo Relatório

0

System.ValueTuple (without generic argument) is very much unit in C#. The source code is open.

over 4 years ago · Santiago Trujillo Relatório

0

I have used this Unit type as a reference: https://github.com/louthy/language-ext/blob/main/LanguageExt.Core/DataTypes/Unit/Unit.cs

Coming from a functional extension library.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda