Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is C#'s equivalent to Haskell's newtype?

In Haskell, there's two ways of providing an alias for types: type and newtype. type provides a type synonym, which means the synonym is regarded by the type checker as exactly the same as the original type:

type UserId = Int
hasAccess :: UserId -> Bool
hasAccess id = {-- stuff --}

-- Elsewhere in the program
login :: Int -> Bool
login n = hasAccess n -- Typechecker won't complain

A newtype is similar, but is regarded by the type checker as a different type:

newtype UserId = UserId Int
hasAccess :: UserId -> Bool
hasAccess (UserId id) = {-- stuff --}

-- Elsewhere in the program
login :: Int -> Bool
login n = hasAccess n -- Typechecker will complain, n isn't a UserId !

In C#, you can define type synonyms with a top-level using declaration:

using UserId = Int;

However, a strongly-typed, compiler-checked type alias does not seem to be present in C# by default. I've looked into automatic code generation with T4 templates and CodeDOM to generate a class wrapper, but I don't really know how I could cleanly integrate those into my programming flow.

Ideally, I would like to be able to say on a top-level:

// Something like this?
using Int.UserId;

/* Elsewhere */
var id = new UserId(5);

public bool HasAccess( UserId id )
{
    /* Stuff */
}

This kicks the code generation into gear at compile-time. If that's not possible or provides a chicken-and-egg issue for IntelliSense, an automatic compilation option that runs every x minutes (or a button or whatever) would be nice.

like image 361
Beerend Lauwers Avatar asked Apr 27 '15 11:04

Beerend Lauwers


People also ask

What is meant by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C and why it is used?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C and C++ difference?

In a nutshell, the main difference between C and C++ is that C is a procedural with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.

What is C language with example?

C language is a system programming language because it can be used to do low-level programming (for example driver and kernel). It is generally used to create hardware devices, OS, drivers, kernels, etc. For example, Linux kernel is written in C. It can't be used for internet programming like Java, .Net, PHP, etc.


Video Answer


1 Answers

To expand on Georg's answer, C# 10 has record structs which allows you to have:

public readonly record struct UserId(int Id);

Just a reminder that it's still possible to accidentally construct a UserId with the implicit parameterless constructor:

var uId = new UserId(); // Oops, forgot to pass the argument!

That's why you might want to catch the error at runtime by throwing:

public readonly record struct UserId(int Id)
{
    public UserId() : this(0)
    {
        throw new ArgumentException("Parameterless constructor should not be used");
    }
}

Even this won't prevent all mistakes because parameterless constructor is ignored in some cases.

like image 83
Smi Avatar answered Sep 21 '22 02:09

Smi