Home / Learn / 1 Hello, Una
1.6 Day-to-day: morning notices
Page 7 of 11
1.6.1 What the program must do
Every morning, when a store opens, a screen by the entrance shows the notices of the day, and the staff read them before the doors open. The notices are:
- the store's name, its address and a greeting;
- one part for each subject: the hours, the shipping cutoff, the offer of the day and the café, each a heading with its notice under it;
- where customers can ask a question, at the end.
A line of dashes separates the parts, so that a reader finds each one at a glance. The texts change often: the offer every day, the hours with the seasons. So each text should be written once, in a place with a name, and the code that prints should not need to change when a text does.
The program is one module, morning, in a package whose una.toml names it as the entry and grants the console:
[package]
name = "morning"
version = "0.1.0"
edition = "0.2"
[run]
entry = "morning"
grants = ["console"]1.6.2 The notices, one function each
Each notice is a function that gives back its text:
--- The name of the store, the first line of the notices.
pub store_name() -> Text = "Harbor Street store"
--- Where the store is, under its name.
pub store_address() -> Text = "12 Harbor Street, by the ferry"
--- The greeting under the name.
pub greeting() -> Text = "Good morning"
--- When the store is open today.
pub opening_hours() -> Text = "Open today from 9:00 to 18:00"
--- Until when an order placed today ships today.
pub shipping_cutoff() -> Text = "Orders placed before 17:00 ship today"
--- Today's offer.
pub special() -> Text = "Today's special: desk lamps"
--- When the café inside the store opens.
pub cafe_hours() -> Text = "The café opens at 10:00"
--- Where customers can ask a question.
pub help_desk() -> Text = "Questions? Ask at the help desk by the main door"Each has a doc comment that says what the text is for, a name that says what it holds, and the result type Text. None of them has a uses line: each only gives back a text, and prints nothing, so each is written in the short form, on one line.
This is the part of the program that changes from day to day. When the café changes its hours, one line changes, the body of cafe_hours, and every place that prints the café's hours prints the new ones. Since these functions print nothing, each one can also be tested on its own, as Testing it shows.
1.6.3 Printing a part: rule and section
Two private functions print. rule prints the line of dashes, and section prints a heading, its notice and a rule:
--- Prints a line of dashes, which separates the parts of the notices.
rule() -> Unit
uses @console: Console[print]
=
@console.print("------------------------------")
--- Prints a heading and the notice under it, then a rule.
section(heading: Text, notice: Text) -> Unit
uses @console: Console[print]
=
@console.print(heading)
@console.print(notice)
rule()Both print: section prints two lines itself, and a third through rule. Both are private, so Una would accept them without a uses line and work it out from their bodies, but this book writes the line on every function that prints, so that its header says so. A function that calls rule prints too, so section holds the console as well. Both are written in the long form, as every function with a uses line is, and both give back Unit, since they are called for the lines they print.
Both are private, with no pub: only this module uses them. section takes two parameters, the heading and the notice, so every part of the notices is printed the same way. To change the look of every part, such as a rule of stars in place of dashes, you change one function.
1.6.4 main
main prints the name, the address and the greeting, a rule, then one section for each subject, and the help desk last:
--- Prints the morning notices.
pub main() -> Unit
uses @console: Console[print]
=
@console.print(store_name())
@console.print(store_address())
@console.print(greeting())
rule()
section("Hours", notice: opening_hours())
section("Shipping", notice: shipping_cutoff())
section("Offer", notice: special())
section("Café", notice: cafe_hours())
@console.print(help_desk())Its steps run in order, from the top. The first three print texts that functions give back; rule() prints the first line of dashes; each section prints one part. Each call of section labels its second argument, notice:, and the argument is a call: opening_hours() runs first, and its text becomes the notice.
main reads like the notices themselves: a reader sees the order of the parts without reading how a part is printed, and adds a part with one line. The texts live in the notice functions, the layout in rule and section, and the order in main: each kind of change has one place.
1.6.5 Running it
In the day-to-day directory:
una check .
una run .una check . prints nothing, and una run . prints the notices:
Harbor Street store
12 Harbor Street, by the ferry
Good morning
------------------------------
Hours
Open today from 9:00 to 18:00
------------------------------
Shipping
Orders placed before 17:00 ship today
------------------------------
Offer
Today's special: desk lamps
------------------------------
Café
The café opens at 10:00
------------------------------
Questions? Ask at the help desk by the main door1.6.6 The whole program
The whole file, to read in one piece or to copy:
module morning
-- The notices that the console of the store prints each morning, when the store opens.
--- The name of the store, the first line of the notices.
pub store_name() -> Text = "Harbor Street store"
--- Where the store is, under its name.
pub store_address() -> Text = "12 Harbor Street, by the ferry"
--- The greeting under the name.
pub greeting() -> Text = "Good morning"
--- When the store is open today.
pub opening_hours() -> Text = "Open today from 9:00 to 18:00"
--- Until when an order placed today ships today.
pub shipping_cutoff() -> Text = "Orders placed before 17:00 ship today"
--- Today's offer.
pub special() -> Text = "Today's special: desk lamps"
--- When the café inside the store opens.
pub cafe_hours() -> Text = "The café opens at 10:00"
--- Where customers can ask a question.
pub help_desk() -> Text = "Questions? Ask at the help desk by the main door"
--- Prints a line of dashes, which separates the parts of the notices.
rule() -> Unit
uses @console: Console[print]
=
@console.print("------------------------------")
--- Prints a heading and the notice under it, then a rule.
section(heading: Text, notice: Text) -> Unit
uses @console: Console[print]
=
@console.print(heading)
@console.print(notice)
rule()
--- Prints the morning notices.
pub main() -> Unit
uses @console: Console[print]
=
@console.print(store_name())
@console.print(store_address())
@console.print(greeting())
rule()
section("Hours", notice: opening_hours())
section("Shipping", notice: shipping_cutoff())
section("Offer", notice: special())
section("Café", notice: cafe_hours())
@console.print(help_desk())
example "the notices start with the store's name"
do store_name()
expect result == "Harbor Street store"
example "the address gives the street"
do store_address()
expect result == "12 Harbor Street, by the ferry"
example "the greeting is for the morning"
do greeting()
expect result == "Good morning"
example "the hours give the opening and the closing time"
do opening_hours()
expect result == "Open today from 9:00 to 18:00"
example "the cutoff names the hour"
do shipping_cutoff()
expect result == "Orders placed before 17:00 ship today"
example "the offer names what is on sale"
do special()
expect result == "Today's special: desk lamps"
example "the café keeps its own hours"
do cafe_hours()
expect result == "The café opens at 10:00"
example "the help desk says where to go"
do help_desk()
expect result == "Questions? Ask at the help desk by the main door"Next:1.7 Testing it