import ballerina/io;
# Country code for Sri Lanka.
# # Deprecated
# This constant is deprecated. Use the constant `LK` instead.
@deprecated
public const string LKA = "LKA";# New country code for Sri Lanka.
public const string LK = "LK";# Country code for USA.
public const string USA = "USA";
public type CountryCode LK|LKA|USA;
@deprecated
public type Address record {|
    string street;
    string city;
    CountryCode countryCode;
|};
@deprecated
public type Person object {
    public string firstName = "John";
    public string lastName = "Doe";
    Address addr = {
        street: "Palm Grove",
        city: "Colombo 3",
        countryCode: LKA
    };
    @deprecated
    public function getFullName() returns string {
        return self.firstName + " " + self.lastName;
    }};
# Creates and returns a `Person` object given the parameters.
#
# + fname - First name of the person
# + lname - Last name of the person
# + street - Street the person is living at
# + city - The city the person is living in
# + countryCode - The country code for the country the person is living in
# + return - A new `Person` object
#
# # Deprecated
# This function is deprecated since the `Person` type is deprecated.
@deprecated
public function createPerson(string fname, string lname, string street,
                             string city, CountryCode countryCode) returns Person {
    Person p = new;    p.firstName = fname;
    p.lastName = lname;
    p.addr = {street, city, countryCode};
    return p;
}public function main() {
    Person p = createPerson("Jane", "Doe", "Castro Street", "Mountain View", USA);
    io:println(p.getFullName());}

Deprecation

In Ballerina, type definitions, functions, object methods, and constants can be deprecated using the @deprecated annotation. The compiler will generate warnings if a user uses a deprecated construct. If the deprecated construct contains documentation, the user has to add some additional documentation called Deprecated documentation. The Deprecated documentation should ideally include details on why the construct was deprecated and suitable alternate options, which should be used instead.

import ballerina/io;
# Country code for Sri Lanka.
# # Deprecated
# This constant is deprecated. Use the constant `LK` instead.
@deprecated
public const string LKA = "LKA";

The constant LKA is marked as deprecated using the @deprecated annotation. Since it has documentation, the # # Deprecated documentation needs to be added as well. This will appear in the generated documentation.

# New country code for Sri Lanka.
public const string LK = "LK";
# Country code for USA.
public const string USA = "USA";
public type CountryCode LK|LKA|USA;

The type definition contains both the deprecated constant and its replacement.

@deprecated
public type Address record {|
    string street;
    string city;
    CountryCode countryCode;
|};

The Address record type is deprecated, but does not have the # # Deprecated documentation.

@deprecated
public type Person object {
    public string firstName = "John";
    public string lastName = "Doe";

The Person object type is deprecated, but does not have the # # Deprecated documentation.

    Address addr = {
        street: "Palm Grove",
        city: "Colombo 3",

Usage of the deprecated record Address.

        countryCode: LKA
    };

Usage of the deprecated constant LKA.

    @deprecated
    public function getFullName() returns string {
        return self.firstName + " " + self.lastName;
    }

Object methods can also be deprecated.

};
# Creates and returns a `Person` object given the parameters.
#
# + fname - First name of the person
# + lname - Last name of the person
# + street - Street the person is living at
# + city - The city the person is living in
# + countryCode - The country code for the country the person is living in
# + return - A new `Person` object
#
# # Deprecated
# This function is deprecated since the `Person` type is deprecated.
@deprecated
public function createPerson(string fname, string lname, string street,
                             string city, CountryCode countryCode) returns Person {

The function createPerson is marked as deprecated using the @deprecated annotation. Since it has documentation, the # # Deprecated documentation needs to be added as well.

    Person p = new;

Usage of the deprecated object Person.

    p.firstName = fname;
    p.lastName = lname;
    p.addr = {street, city, countryCode};
    return p;
}
public function main() {
    Person p = createPerson("Jane", "Doe", "Castro Street", "Mountain View", USA);

Usage of the deprecated object Person and function createPerson.

    io:println(p.getFullName());

Usage of the deprecated object method getFullName

}
# To run this sample, navigate to the directory that contains the
# `.bal` file and execute the `ballerina run` command.
$ ballerina run deprecation.bal
warning: .::deprecation.bal:35:5: usage of construct 'Address' is deprecated
warning: .::deprecation.bal:39:22: usage of construct 'LKA' is deprecated
warning: .::deprecation.bal:68:5: usage of construct 'Person' is deprecated
warning: .::deprecation.bal:78:5: usage of construct 'Person' is deprecated
warning: .::deprecation.bal:78:16: usage of construct 'createPerson' is deprecated
warning: .::deprecation.bal:81:16: usage of construct 'Person.getFullName' is deprecated
Running executables
Jane Doe