Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Generics?
3.
Example
4.
Interface
5.
Checked Parameters and Template Parameters
6.
 
7.
Advantages of Generics
8.
Frequently Asked Questions
8.1.
What is the purpose of the Carbon language?
8.2.
Is Carbon a compiled language or an interpreted language?
8.3.
Is Carbon language ready to use?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Generics In Carbon

Author Sajid Khan
0 upvote
Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

generics in carbon

 

Carbon is a new programming language launched by Google this year. Carbon is a successor to C++ language. Carbon is created to improve the shortcomings of C++ language and provides a similar set of features with the main aim of providing readability and bi-directional interoperability.

In this article, we will dive into the specifics of Generics in Carbon.

What are Generics?

Generics in carbon are used to provide an alternative to carbon (c++) templates. These are parameterized functions and types, that are used to write codes that are generally applicable, meaning that you can avoid writing specialized codes for similar situations involving just different types of parameters. 

  • Generics helps carbon to write functions or classes with compile-time parameters.
  • Carbon uses interfaces to implement generics. These interfaces should have a name and methods and functions that the type should explicitly implement. 
  • Implementation of these interfaces can be part of the type’s definition, or it can be external if it is defined in the library defining the interface.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example

 

Given below are two sorting functions. One is for vectors containing integers and the other is for sorting vectors of strings.

code 1

 

But, with the help of Generics, we can do the same using only one function instead of using one function for each type, as shown below.

code 2

 

In the above code snippet, the use of the syntax ! denotes that the parameter T is generic, and the caller function will have to give the parameter type at the compile time.

Thus, using an i32 vector v in SortVectorType(i32, &v) will be equivalent to SortVectori32(&v). Similarly, for a string vector st, SortVectorType(String, &st) will be equivalent to SortVectorString(&st).

So, using Generics provides us with this ability to generalize.

 

Interface

In the above code snippets, we saw how generics works. In the SortVectorType function, Compare is an Interface. An Interface defines all that is required by the type T. 

In order for the SortVectorType to work, we will have to provide the compiler with a definition of Compare, so the compiler can;

  • Type-check the Generic without having the information from where it is called.
  • Type-check a call to a generic using only the information from the function’s signature.

 

 

Checked Parameters and Template Parameters

In the above code snippet :! Indicates that the parameter T is generic. These generic parameters can be of two types, Checked or Tempelate; by default, it is Checked.

“Checked” here means that the body of SortVectorType is checked when the function is defined, and it is independent of the value of type T. The function would infer that any type T that implements the compare interface would pass this type checking.

We can also declare a template generic parameter by prefixing Template Keyword before :!. Carbon templates follow the same C++ template paradigms.

 

Advantages of Generics

  • It supports both static and dynamic dispatch.
  • Function calls and function bodies both are checked independently against the function signature.
  • Provides fast builds.

Frequently Asked Questions

What is the purpose of the Carbon language?

Carbon is a general-purpose programming language that is still under development. As of now, it is intended to be a successor to C++. It will be used along with C++ in game development and GUI.

Is Carbon a compiled language or an interpreted language?

Carbon is a free and open-source programming language that is statically typed and compiled.

Is Carbon language ready to use?

Carbon is still an experimental language. So, some things may change in the future. The official documentation of Carbon itself states that “Carbon is not ready for use.” However, some design principles are less likely to change. 

Conclusion

In this article, we looked into Generics in Carbon and delved into the specifics of Generics. We also looked at an example to understand generics and interfaces.

 

Recommended reading

Also, check out some of the Guided PathsContests, and Interview Experiences to gain an edge only on Coding Ninjas Studio.

 

                                        

 

Live masterclass