Top‐level statements - IS4Code/Sona GitHub Wiki

A certain category of statements are allowed to be placed only within the main body of the program, and serve as declarations or instructions for the environment. As such, their use within functions, nested blocks, or conditionals is prohibited.

All top-level statements have a single mandatory argument, which may or may not be enclosed in parentheses. As top-level statements are statements, ; may be used to end them.

import

The import statement is used to bring into the scope the elements within a particular namespace or type so that they can be referenced using their simple name.

Syntax

import_statement:
  'import' namespace_or_module |
  'import' type '.' '*' |
  'import' file_name;

The type of the import statement is distinguished by the syntax of its argument:

  • A plain compound name indicates a namespace or a module, importing everything within it.
  • A type name ending with .* imports all static members of the type.
  • A string indicates a file, loading it and importing its main module.

The import statement affects all statements that follow it, even other import statements.

Examples

The following example shows how namespaces or types can be imported:

// Imports the System namespace:
import System;
// Imports all members of the System.Console type:
import Console.*
// References System.Console.WriteLine through the imported type:
WriteLine("Hello, World!")
F# source
open System
open type Console
WriteLine "Hello, World!"

This example imports a file by its name:

import "path/Script.ext"
// Every top-level element in Script is accessible.
F# source
#load "path/Script.ext"
open Script

require

The require statement indicates a reference to an external package that is required to execute the code in the program.

Syntax

require_statement:
  'require' package_name;

The package name is a string in a particular format understood by the interpreter, for example a file name or an identifier for the F# Interactive Dependency Manager.

Examples

The following example references two packages, one from disk and another from NuGet:

require "Extensions.dll"
require "nuget: Newtonsoft.Json"
F# source
#r "Extensions.dll"
#r "nuget: Newtonsoft.Json"

include

The include statement is similar to require but references the contents of an uncompiled script file on disk, which is loaded and interpreted alongside the main program. Unlike import, the main module of the script is not automatically imported.

Syntax

include_statement:
  'include' file_name;

Examples

The following example includes a file and uses a function from it:

include "path/Script.ext"
Script.func()
F# source
#load "path/Script.ext"
Script.func()
⚠️ **GitHub.com Fallback** ⚠️