> For the complete documentation index, see [llms.txt](https://harmeetsingh.gitbook.io/scala-type-system/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://harmeetsingh.gitbook.io/scala-type-system/phase-i/chapter-6-parameterized-types.md).

# Chapter 6: Parameterized Types

In Scala, there are two major ways to design your generic or reusable code.  Those are **Parameterized Types** and **Abstract Types**. Both of them have their own pros and cons. In this chapter, we will try to explore parameterized types. Let's start with layman definition, Parameterized types are those types which are declared by types or classes via type constructor( `[ ]` ).&#x20;

From[ chapter 2nd](/scala-type-system/phase-i/chapter-2-types-vs-classes-and-subtyping-vs-inheritence.md), we have an idea about types and classes Now the question is, what is **type constructor**? A type constructor is used to constructing a new type from passed type parameter. Take an example from the below code:&#x20;

```scala
trait Java
trait Scala

class Book[T] { … }

new Book[Java] // creates new type Book of Java
new Book[Scala] // creates a new type Book of Scala
```

In the above example, we are creating two new types which are `Java` and `Scala` , after that create another parameterized type which is called a `Book[T]`. **Why `Book` is called the Parameterized type**? Because the book contains a type constructor as of `[T]`**,** while we are passing `Java` as a type parameter, it creates a new type which is called `Book[Java]` (Book of Java). This concept is similar to construct a new object via class constructor like below:&#x20;

```scala
class Book(`type`: String)
new Book(“Java”) // creates a new object Book which contains Java
new Book(“Scala”)  // creates a new object Book which contains Scala. 
```

Via type constructor, we can create new types as per our requirement. Always remember, creating a new type means `Book[Java]` (Book of Java) type is always different from `Book[Scala]` (Book of Scala) type similar to `new Book(“Java”)` object is different from new `Book(“Scala”)` object. Type constructor contains more than one parameters like:

```scala
trait Flow[In, Out]
trait Http[Request, Response]
```

The name of the type parameter depends on the type you are creating, like in the above code, we are creating two types. `Flow` which contains `In` and `Out` parameters. We can use any character here, but for the conventions perspective, we need to use name, which defines the purpose of the type parameter. The basic convention of type parameters which is followed in **Java** as well, for declaring types, prefer to use a single character in a capital letter as we declared in `Book` class.&#x20;

We can define type constructor with traits, classes, and method. In methods type constructor not creating a new type, but it helps to define generic type parameters to method arguments like below:&#x20;

```scala
def add[T](a: T, b: T): T

add[Int](3, 4)
add[String](“Harmeet”, “Singh”)
```

Because of type constructor in the add method, we can use the same method for various types as per our requirements. The implementation of these methods could be the same or different, that we will explore in [chapter 8th](/scala-type-system/phase-i/chapter-8-type-classes-and-add-hoc-polymorphism.md) using **type classes** and **ad-hoc polymorphism**.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://harmeetsingh.gitbook.io/scala-type-system/phase-i/chapter-6-parameterized-types.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
