-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5820 from heshanpadmasiri/feat/distinct-object
Update distinct object examples
- Loading branch information
Showing
3 changed files
with
48 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,58 @@ | ||
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; | ||
} | ||
} | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
$ bal distinct_object_types.bal | ||
Engineer | ||
false | ||
true | ||
true | ||
true | ||
true | ||
false |