Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add documentation for option types #432

Merged
merged 3 commits into from
May 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion doc/encore/lang/semantics/semantics.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,33 @@ function:
print(apply(bump, 3))
}|

@section{Option types}
Option types, (sometimes called 'Maybe types') are polymorphic
types that represent optional values. Using option types is simple:
a value is wrapped inside a @code{Just} data constructor
(e.g. @code{Just 32}), while @code{Nothing} presents an absent value.

Copy link
Contributor

@PhucVH888 PhucVH888 May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to connect single sentences like this.
Option types, or Maybe types are polymorphic types that represent optional values. Using option types is simple. Meaningful values are wrapped inside @code{Just} data constructor (e.g. @code{Just 32}), or values that are of no importance are written by @code{Nothing} data constructor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is not unimportant. "Option types, (sometimes called 'Maybe types') are polymorphic types that represent optional values. Using option types is simple: a values is wrapped inside a @code{Just} data constructor (e.g. @code{Just 32}), while @code{Nothing} presents an absent value."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The online editor is quite simple but powerful. Please feel free to use the editor and update the wording in this PR. I have already fixed the aforementioned issues

Option types are useful to substitute the @code{null} reference to objects.
The following example shows the use of option types in a function
that creates a string (wrapped inside a @code{Just}) if the input value
is greater than @code{0} and @code{Nothing} otherwise.

@codeblock|{
def test_if_greater_than_zero(x: int): Maybe String {
if x > 0 then Just("Test passes")
else Nothing
}
}|

There exists limited support for type inference when using the
@code{Nothing} data constructor. This means that, sometimes,
you will have to cast the type of @code{Nothing}. In the previous
example, you would substitute the @code{Nothing} (line 3)
by @code{Nothing : Maybe String}.

The section related to pattern matching (below) explains how to
extract the value (if it exists) from an option type.

@section{Pattern Matching}
Pattern matching allows the programmer to branch on both the value of data,
as well as its structure, while binding local names to sub-structures. Encore's
Expand Down Expand Up @@ -1036,4 +1063,4 @@ not support patterns directly in the function head.

One limitation of the current implementation is that you have to
declare the type of the function in every function head, even
though the type has to be the same for all clauses.
though the type has to be the same for all clauses.
9 changes: 8 additions & 1 deletion doc/encore/lang/syntax/grammar.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ This section introduces the Encore grammar by using the BNF-grammar notation and
@(encore/keyword passive)
@(encore/keyword end)
@(encore/keyword typedef)
@(encore/keyword Just)
@(encore/keyword Nothing)

@; Non-terminals
@(encore/nonterm Program)
Expand All @@ -123,6 +125,7 @@ This section introduces the Encore grammar by using the BNF-grammar notation and
@(encore/nonterm LetDecls)
@(encore/nonterm Int)
@(encore/nonterm Real)
@(encore/nonterm Option)
@(encore/nonterm Op)
@(encore/nonterm Char)
@(encore/nonterm String)
Expand Down Expand Up @@ -161,7 +164,7 @@ This section introduces the Encore grammar by using the BNF-grammar notation and
(list TypeDef
@seq[typedef Name @optional[TypeParams] equal Type])

(list TypeParams
(list TypeParams
@seq[l TypeVar @kleenestar[@BNF-group[comma TypeVar]] b])

(list TypeVar @seq[@elem{[a-z]} @kleenestar[@elem{[a-zA-Z0-9_]}]])
Expand Down Expand Up @@ -265,13 +268,17 @@ This section introduces the Encore grammar by using the BNF-grammar notation and
@elem{"String"}
Int
Real
Option
@seq[Expr Op Expr]
@seq[not Expr]
@seq[lambda open-paren ParamDecls close-paren arrow Expr])

(list Op
@alt[l b equals distinct plus minus prod div mod and or])

(list Option
@seq[Just Expr]
Nothing)
(list Name
@elem{[a-zA-Z][a-zA-Z0-9]*})

Expand Down