Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Package and Library in Carbon

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

Introduction

Are you bored learning the old programming languages Java, C++, etc.? Want to learn something new?

This article is focused on the new programming language Carbon. Carbon language is an experimental language. It is a general-purpose and open-source project. It was initiated by Google. It was first introduced by Chandler Carruth on 19 July 2022. He is a software engineer at Google. It was in Toronto at the CppNorth conference. Carbon language fixed many shortcomings of C++. It has many similarities to the features set of C++. It is the successor of C++. This language aims to make two-way interoperability and readability.

Now, let's dive into the article to learn about the packages and libraries of Carbon language in detail.

Package and Library in Carbon

Libraries and Packages

Files are the place where code in Carbon language is written. Libraries are a group of files. Packages are a group of libraries. Libraries are responsible for code reusability. They can be imported into the files. The unit of distribution in the carbon language packages

Every package in the Carbon language has its unique namespace. This means that libraries within a package must cooperate to avoid name conflicts. It is optional across packages.

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

Package declaration

Files in Carbon start with an optional package declaration. It consists of the following:

  • The keyword called the package,
  • An identifier for the name of the package (optional),
  • An optional library and a string with the name of the library,
  • You can either use impl or api, and
  • a semicolon (;) for termination.

 

For example:

// Package name is `CodingNinjas`.
// Library name is "Writer".
// This file is an `api` file. It is not an `impl` file.
package CodingNinjas library "Writer" api;

The declaration part can be changed as given below:-

  • If no package name is present. Such as the package library "Writer" api. Then the default package is contributed by the file. The default package is the only one that can import from it.
  • Suppose no library keyword is present. Such as the package CodingNinjas api. The default library is contributed by the file.
  • If no package declaration is present, then default libraries and default packages will belong to the api file. 

There is no need to use default packages. But when you use them, then entry-point functions should be included. Run is the entry-point function by default from the default package.

API and impl suffix

After the package declaration, you can import files into your program using the "include" keyword. In the syntax, you have to include the package name and library keyword with the library's name. But the library keyword is optional. If you remove the library keyword, Carbon will import the default library for that package.

// Import the "CodingNinjas" library from the
// `Articles` package.
import Articles library "CodingNinjas";
// Import the default library from the
// `TechnicalBlogs` package.
import TechnicalBlogs;

In the above syntax, the “import NameOfThePackage …” section has the name “NameOfThePackage” as a private name. It is the name of the given package. You cannot use it to import libraries of the currently used package. If you try to import additional libraries from the package, it will make the additional members of NameOfThePackage visible.

You can import the libraries from the current package by removing the package's name.

// Import the "CodingNinjas" library from the same package.
import library "CodingNinjas";
// Import the default library from the same package.
import library default;

In the above code, the "import library …" section helps you to add all the public top-level names inside the specified library to the top-level scopes of the currently used file. It will add them as private names and for names in the namespaces as well. Every file with "impl" will automatically start importing the api file for its library. You must make the declaration of all the imports. It should appear before the declaration of all the other non-package in a file.

Each library consists of only one api file. This file contains all public name declarations of the library. The declaration must have its definition. These definitions are present in either an impl file or the api file in the library.

// Package name is `CodingNinjas`.
// Library name is "Coder".
// This file is an `api` file. It is not an `impl` file.
package CodingNinjas library "Coder" api;

Frequently Asked Questions

Is Carbon better than C++?

Carbon almost matches the performance of C++. It aims to provide a better developer experience. This is because Carbon reduces existing libraries' tech debt and makes extending those libraries easier.

Is Carbon an interpreted language?

Carbon is a straightforward C++11 scripting language that is embeddable, object-oriented, dynamically typed, and interpreted from the bytecode. It uses smart pointers to allocate memory safely.

What are the uses of Carbon language?

Carbon is supposed to be as analogous to C++ as Kotlin is to Java. The goal of Carbon's designers is to support performance-critical software, language, and software evolution and to have secure and simple code to read and write.

Why did Google make carbon language?

Carbon is being developed to support software and language evolution while producing performance-critical software. Making code simple to read, comprehend, and write is another essential objective. As a result, Modern OS platforms, hardware designs, and environments may be developed quickly and scalable.

Is Carbon replacing C++?

Google's open-source Carbon programming language is not a strict replacement for C++, even after having popular beliefs. Instead, it is designed as a successor. While this may sound the same, the critical difference is that its purpose is to be used alongside C++.

Conclusion

In this article, we have discussed all the concepts of packages and libraries in the Carbon programming language. We have also explained how you can implement packages and libraries in your code in the Carbon programming language.

We hope this blog has helped you enhance your knowledge regarding the carbon programming language. Check out our articles to learn more about the variables in Carbon and data types in Carbon. Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Topics covered
1.
Introduction
2.
Libraries and Packages
3.
Package declaration
4.
API and impl suffix
5.
Frequently Asked Questions
5.1.
Is Carbon better than C++?
5.2.
Is Carbon an interpreted language?
5.3.
What are the uses of Carbon language?
5.4.
Why did Google make carbon language?
5.5.
Is Carbon replacing C++?
6.
Conclusion