-
Notifications
You must be signed in to change notification settings - Fork 3
Example Snippets
Mike Lewis edited this page Jun 27, 2016
·
13 revisions
Curious what the language looks like, or how certain common coding problems are solved in Epoch? Look no further!
Doing math on both integers and floating point types should be comfortable and familiar:
entrypoint :
{
integer test = 40
integer more = 4
integer less = 2
integer math = 6
integer stuff = 111
integer rocks = 1
integer result = (test + more) - less // 42
integer evil = math * stuff / rocks // 666
}
Epoch allows you to halt a program immediately if some critical invariant is violated:
entrypoint :
{
// Make sure that true is still true:
assert(true)
// Validate some arithmetic:
assert(40 + 2 == 42)
// This line would crash the program immediately upon execution:
//assert(false)
}
Here's a simple example of what Epoch programs look like. This is a slightly over-engineered version of Hello World that illustrates some common type system idioms from Epoch:
//
// A simple Epoch program
//
// Declare an algebraic sum type
type OptionalString : string | nothing
// Define a function
Display : string optstr
{
print(optstr)
}
// Overload the function
Display : nothing
{
print("End of line.")
}
// Entry point function
entrypoint :
{
OptionalString hello = "Hello, world!"
OptionalString blank = nothing
Display(hello)
Display(blank)
}
Strings are a first-class type in Epoch. Concatenation is expressed using the ;
operator to disambiguate from arithmetic addition:
entrypoint :
{
string h = "Hello"
string s = " "
string w = "World"
string message = h ; s ; w
print(message)
}
There is plenty more to see! We recommend exploring the .epoch
files in the repository for more examples of how the code looks and feels.