diff --git a/examples/distinct-object-types/distinct_object_types.bal b/examples/distinct-object-types/distinct_object_types.bal index f20469474e..01042b2b70 100644 --- a/examples/distinct-object-types/distinct_object_types.bal +++ b/examples/distinct-object-types/distinct_object_types.bal @@ -1,22 +1,40 @@ import ballerina/io; -// The `Person` object type that contains a string field called `name`. -type Person distinct object { +class Person { public string name; + + function init(string name) { + self.name = name; + } }; -// The `Engineer` and `Manager` classes are structurally the same but introducing the -// `distinct` keyword distinguishes them by considering them as nominal types. -distinct class Engineer { - *Person; +// The `DistinctPerson` type is a proper subtype of the `Person` type. +distinct class DistinctPerson { + public string name; + + function init(string name) { + self.name = name; + } +} + +// The `SomeWhatDistinctPerson` type is a subtype of the `DistinctPerson` type +// since it includes the `DistinctPerson` type's type IDs via inclusion. +class SomeWhatDistinctPerson { + *DistinctPerson; + + public string name; function init(string name) { self.name = name; } } -distinct class Manager { - *Person; +// The `EvenMoreDistinctPerson` type is a proper subtype of the `DistinctPerson` +// type since it has an additional type ID. +distinct class EvenMoreDistinctPerson { + *DistinctPerson; + + public string name; function init(string name) { self.name = name; @@ -24,7 +42,17 @@ distinct class Manager { } public function main() { - Person person = new Engineer("Alice"); - // The `is` operator can be used to distinguish distinct subtypes. - io:println(person is Engineer ? "Engineer" : "Manager"); + Person person = new ("John Smith"); + io:println(person is DistinctPerson); + + DistinctPerson distinctPerson = new ("Alice Johnson"); + io:println(distinctPerson is Person); + + SomeWhatDistinctPerson someWhatDistinctPerson = new ("Michael Brown"); + io:println(someWhatDistinctPerson is DistinctPerson); + io:println(distinctPerson is SomeWhatDistinctPerson); + + EvenMoreDistinctPerson evenMoreDistinctPerson = new ("Sarah Wilson"); + io:println(evenMoreDistinctPerson is DistinctPerson); + io:println(distinctPerson is EvenMoreDistinctPerson); } diff --git a/examples/distinct-object-types/distinct_object_types.md b/examples/distinct-object-types/distinct_object_types.md index 7a6d247505..00594a2f5e 100644 --- a/examples/distinct-object-types/distinct_object_types.md +++ b/examples/distinct-object-types/distinct_object_types.md @@ -1,8 +1,8 @@ # Distinct object types -Using the `distinct` keyword in the type definition creates distinct object types. This concept allows defining a type with nominal typing within a structured type system. This is useful when interacting with the external world through API interfaces like `GraphQL`. You may want to leverage nominal typing via this distinct typing feature of Ballerina. +For more explicit control over object type relations you can use `distinct` object types. Each distinct object type declaration has a unique type ID. When you include a distinct object type within another object type declaration, the new type's type ID set will include the type IDs of the included type. When checking if a given object type `OSub` is a subtype of a distinct object type `OSuper` there is the additional requirement that the `OSub` type must contain all the type IDs of the `OSuper` type. -Conceptually, a distinct type including another distinct type results in multiple interface inheritance. +This way you can achieve the same behavior as a nominal type system within Ballerina's structured type system, which is useful to support features such as GraphQL API interfaces. ::: code distinct_object_types.bal ::: @@ -13,3 +13,4 @@ Conceptually, a distinct type including another distinct type results in multipl - [Error subtyping](/learn/by-example/error-subtyping/) - [Defining classes](/learn/by-example/defining-classes/) - [Flexibly typed](https://ballerina.io/why-ballerina/flexibly-typed/) +- [GraphQL service - Interfaces](https://ballerina.io/learn/by-example/graphql-interfaces/) diff --git a/examples/distinct-object-types/distinct_object_types.out b/examples/distinct-object-types/distinct_object_types.out index a7c9955961..5c2f046965 100644 --- a/examples/distinct-object-types/distinct_object_types.out +++ b/examples/distinct-object-types/distinct_object_types.out @@ -1,2 +1,7 @@ $ bal distinct_object_types.bal -Engineer +false +true +true +true +true +false