DEV Community

Cover image for Structural vs Nominal typing๐ŸŒŸ
Balamurugan D
Balamurugan D

Posted on โ€ข Originally published at balamurugan16.hashnode.dev

Structural vs Nominal typing๐ŸŒŸ

Introduction: ๐Ÿ“

As technology advances, our preferences for programming languages are evolving too. Nowadays, more people are favoring statically typed languages like TypeScript, Go, and Rust over dynamically typed ones like Python, Ruby, and JavaScript. While there's no need to dislike dynamic languages (they have their strengths!), personally, I find statically typed languages my top choice, even if they can be a bit tricky to work with sometimes. ๐Ÿ’ป๐Ÿ’ช

Understanding Structural Typing ๐Ÿ› ๏ธ

Structural typing is well illustrated in TypeScript. Let's see an example:

type Car = {
  name: string;
}

type Bike = {
  name: string;
}

const myCar: Car = {
  name: "Fortuner"
}

const myBike: Bike = myCar;
Enter fullscreen mode Exit fullscreen mode

In this TypeScript example, there are no errors when we assign a variable of type Car to a variable of type Bike. Why? Because both types have the same structure, even though they have different names for their definitions. They both have a property called name with a type of string. This flexibility is a key feature of structural typing, which applies to classes and interfaces in TypeScript too. ๐Ÿ—๏ธ๐Ÿงฑ

Exploring Nominal Typing ๐Ÿญ

Java represents nominal typing. Let's see how it works:

class Car {
    String name;
}

class Bike {
    String name;
}

public class Program
{
    public static void main(String[] args) {
        Car car = new Car();
        car.name = "Fortuner";
        
        Bike bike = car; // This line will raise a type error in Java
    }
}
Enter fullscreen mode Exit fullscreen mode

In this Java example, you'll encounter an "incompatible types" error when trying to assign a variable of type Car to a variable of type Bike. Java strictly enforces nominal typing, so even if the types have the same structure, they must explicitly be of the same named type. ๐Ÿ› ๏ธ๐Ÿšง

Pros and Cons of Each Approach ๐Ÿคโœ…โŒ

Structural typing offers incredible flexibility, following the famous "Duck typing" principle: "If it walks like a duck and it quacks like a duck, then it must be a duck!" It also maintains a good level of security similar to nominal typing.

On the other hand, nominal typing, seen in languages like C++ and Java, has its own set of use cases. However, it often involves writing more boilerplate code to handle the relationships between various types.

Conclusion: ๐ŸŽฏ

Understanding the differences between static and dynamic typing, along with the nuances of structural and nominal typing, can give you an advantage in your programming journey. Making informed decisions when choosing the right language for specific projects becomes easier. Happy coding! ๐Ÿš€๐Ÿ‘ฉโ€๐Ÿ’ป

That's a wrap! Creating content like this for my blogs allows me to share these insightful details with you all. ๐Ÿ“š๐Ÿ” If you found this helpful, stay tuned for more tech-focused content in the future! Feel free to share your thoughts and experiences in the comments below. ๐Ÿ—ฃ๏ธ๐Ÿ˜Š

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay