Type-oriented programming/Types and properties

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Types can be viewed of bundles of properties and functions operating on them. Such functions are conventionally called methods. Here’s an example of a simple type:

type Person {
  property name String
  property age Int
}

This type declaration states that the Person type has two properties. An instance of this type can be created using a new statement:

var p = new Person {
  name = "Jane",
  age = 18
}

The properties of a type’s instance can be accessed via the dot operator, for example:

return p.name

NB: The pseudocode can be tried out using the Funcy app, which can be downloaded for free from Apple’s App Store (iOS/macOS), Google Play (Android) or Amazon Appstore. The code to be executed must be placed in a main {} block. The example above can be tried out by running the following code:

type Person {
  property name String
  property age Int
}

main {
  var p = new Person {
    name = "Jane",
    age = 18
  }
  return p.name
}