A strongly typed identifier is user-defined data type which serves as an identifier or key that is strongly typed. This is a solution to the "primitive obsession" code smell as mentioned by Martin Fowler. The data type should preferably be immutable if possible. It is common for implementations to handle equality testing, serialization and model binding.
The strongly typed identifier commonly wraps the data type used as the primary key in the database, such as a string, an integer or universally unique identifier (UUID).
Web frameworks can often be configured to model bind properties on view models that are strongly typed identifiers. ObjectâÂÂrelational mappers can often be configured with value converters to map data between the properties on a model using strongly typed identifier data types and database columns.
Passing a strongly typed identifier throughout the layers of an example application.
C# have records which provide immutability and equality testing. The record is sealed to prevent inheritance. It overrides the built-in <code>ToString()</code> method.
This example implementation includes a static method which can be used to initialize a new instance with a randomly generated globally unique identifier (GUID).
C++ has structs but not immutability so here the id field is marked as private with a method named <code>getId()</code> to get the value.
Crystal's standard library provides the record macro for creating records which are immutable structs and lets you create override the built-in <code>to_s</code> method.
D have immutable structs.
Dart have classes with operator overloading.
F# lets you create override the <code>Equals</code>, <code>GetHashCode</code> and <code>ToString</code> methods.
Go have structs which provide equality testing. Go however does not provide immutability.
Groovy have record classes which provide immutability and equality testing.
Haskell can create user-defined custom data types using the <code>newtype</code> keyword. It provides equality testing using the <code>Eq</code> standard class and printing using the <code>Read</code> and <code>Show</code> standard classes.
Java have records which provide equality testing. The record is declared using the <code>final</code> modifier keyword to prevent inheritance. It overrides the built-in <code>toString()</code> method.
This JavaScript example implementation provides the <code>toJSON</code> method used by the <code>JSON.stringify()</code> function to serialize the class into a simple string instead of a composite data type. It calls <code>Object.freeze()</code> to make the instance immutable. It overrides the built-in <code>toString()</code> method and the <code>valueOf()</code> method.
Julia have immutable composite data types.
Kotlin have "inline classes".
Nim have "distinct types".
This PHP example implementation implements the <code>__toString()</code> magic method. Furthermore, it implements the <code>JsonSerializable</code> interface which is used by the built-in <code>json_encode</code> function to serialize the class into a simple string instead of a composite data type. The class is declared using the <code>final</code> modifier keyword to prevent inheritance. PHP has traits as a way to re-use code.
Python has data classes which provides equality testing and can be made immutable using the <code>frozen</code> parameter. It overrides the <code>__str__</code> dunder method.
This example implementation includes a static method which can be used to initialize a new instance with a randomly generated universally unique identifier (UUID).
Python also has <code>NewType</code> which can be used to create new data types.
Raku have classes which provides equality testing and are immutable. It overrides the built-in method.
This example overrides the default gist method. It uses roles which are mixed in into classes to be re-usable.
Ruby have data classes which provides equality testing and are immutable. It overrides the built-in <code>to_s</code> method.
This example implementation includes a static method which can be used to initialize a new instance with a randomly generated universally unique identifier (UUID).
In Rust this can be done using a tuple struct containing a single value. This example implementation implements the <code>Debug</code> and the <code>PartialEq</code> traits. The <code>PartialEq</code> trait provides equality testing.
Scala have case classes which provide immutability and equality testing. The case class is sealed to prevent inheritance.
Swift have the <code>CustomStringConvertible</code> protocol which can be used to provide its own representation to be used when converting an instance to a string, and the <code>Equatable</code> protocol which provides equality testing.
Zig have structs with constants but by design does not have operator overloading and method overriding.