-
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.
- Loading branch information
1 parent
13692ae
commit 087eef5
Showing
3 changed files
with
42 additions
and
15 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,53 @@ | ||
import ballerina/io; | ||
|
||
// The `Person` object type that contains a string field called `name`. | ||
type Person distinct object { | ||
// DistinctPerson is a proper subtype of Person | ||
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; | ||
distinct class DistinctPerson { | ||
public string name; | ||
|
||
function init(string name) { | ||
self.name = name; | ||
} | ||
} | ||
|
||
distinct class Manager { | ||
*Person; | ||
// SomeWhatDistinctPerson is a subtype of DistinctPerson since inherit the same type ID via inclusion | ||
class SomeWhatDistinctPerson { | ||
*DistinctPerson; | ||
public string name; | ||
|
||
function init(string name) { | ||
self.name = name; | ||
} | ||
} | ||
|
||
// EvenMoreDistinctPerson is a proper subtype of DistinctPerson since it has a 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 ("person"); | ||
io:println(person is DistinctPerson); | ||
DistinctPerson distinctPerson = new ("distinctPerson"); | ||
io:println(distinctPerson is Person); | ||
|
||
SomeWhatDistinctPerson someWhatDistinctPerson = new ("someWhatDistinctPerson"); | ||
io:println(someWhatDistinctPerson is DistinctPerson); | ||
io:println(distinctPerson is SomeWhatDistinctPerson); | ||
|
||
EvenMoreDistinctPerson evenMoreDistinctPerson = new ("evenMoreDistinctPerson"); | ||
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 |