Skip to content

Commit

Permalink
[docs] Fix types example so it can be compiled
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulBone committed Jul 17, 2021
1 parent a68d518 commit e4e16f7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 51 deletions.
4 changes: 4 additions & 0 deletions examples/BUILD.plz
Expand Up @@ -21,6 +21,10 @@ modules = [ Mr4 ]
type = program
modules = [ Temperature ]

[types]
type = program
modules = [ Types, Set ]

[modules]
type = program
modules = [ ModuleExample, ModuleToImport ]
Expand Down
9 changes: 9 additions & 0 deletions examples/set.p
@@ -0,0 +1,9 @@
// vim: ft=plasma
// This is free and unencumbered software released into the public domain.
// See ../LICENSE.unlicense

module Set

export
type Set('t) = Set(items : List('t))

109 changes: 58 additions & 51 deletions examples/types.p
@@ -1,68 +1,75 @@
# vim: ft=plasma
# This is free and unencumbered software released into the public domain.
# See ../LICENSE.unlicense
// vim: ft=plasma
// This is free and unencumbered software released into the public domain.
// See ../LICENSE.unlicense

module Types

#
# This example doesn't yet compile. The uncommented code requires a type
# alias for Number and the module system and a Set module.
#
/*
* This example doesn't yet compile. The uncommented code requires a type
* alias for Number and the module system and a Set module.
*/

// We can define our own types.

#
# Type expressions
#

#
# Types
#

# We can define our own types.

# A simple enum
// A simple enum
type Suit = Hearts | Diamonds | Spades | Clubs

# A structure: a single constructor with fields.
type Number = Ace
| One
| Two
| Three
| Four
| Five
| Six
| Seven
| Eight
| Nine
| Ten
| Jack
| Queen
| King

// A structure: a single constructor with fields.
type PlayingCard = Card ( suit : Suit, number : Number )

# A combination of the above, a PlayingCard is either an ordinary card or a
# joker. An orderinary card has fields.
// A combination of the above, a PlayingCard is either an ordinary card or a
// joker. An orderinary card has fields.
type PlayingCardOrJoker = OrdinaryCard ( suit : Suit, number : Number )
| Joker

# Types are polymorphic, they may take type parameters.
type Tree(k, v) = EmptyTree
| Node (
key : k,
value : v,
left : Tree(k, v),
right : Tree(k, v)
)
// Types are polymorphic, they may take type parameters.
type Tree('k, 'v) = EmptyTree
| Node (
key : 'k,
value : 'v,
left : Tree('k, 'v),
right : Tree('k, 'v)
)

# Test that module qualifiers work on type expressions.
// Test that module qualifiers work on type expressions.
import Set

type MyType = MyConstr (
feield : Set.Set(Int)
)

#
# Type Aliases
#

## A type alias, ID is now another word for Int.
#type_alias ID = Int
#
## It's often more useful to alias something more complex.
#type_alias Name = String
#type_alias NameMap = Map(ID, Name)
#
## Type aliases can take parameters:
#type_alias IDMap(x) = Map(ID, x)
#
# Empty main function.
field : Set.Set(Int)
)

//
// Type Aliases
//
//
//# A type alias, ID is now another word for Int.
//type_alias ID = Int
//
//# It's often more useful to alias something more complex.
//type_alias Name = String
//type_alias NameMap = Map(ID, Name)
//
//# Type aliases can take parameters:
//type_alias IDMap(x) = Map(ID, x)
//


// Empty main function.
entrypoint
func main() uses IO -> Int {
return 0
}
Expand Down

0 comments on commit e4e16f7

Please sign in to comment.