Skip to content
Playground

モジュール

use による import、my / our / pub の visibility、companion module、ドット選択の解決、標準ライブラリの一覧を扱う。

use

yulang
use std::undet::*
use std::list::map
use std::ops::{(+), (-)}
use my_module::old_name as new_name
use noisy::* without debug

use は module 内の名前を scope へ入れる。* は見えているものをまとめて import する。{...} による group、as による rename、without による除外も使える。operator 名は (+) のように括弧付きで import できる。

Companion module

structtype ... with:enumacterrorrole は同名の companion module を作る。body 内の our / pub はそこへ入る。

yulang
struct point { x: int, y: int } with:
    our p.norm2 = p.x * p.x + p.y * p.y

point::norm2 (point { x: 3, y: 4 })
point { x: 3, y: 4 } .norm2

enumerror の variant も companion に入る。

yulang
opt::just 1
fs_err::not_found "path"

prelude が justnilokerr のような標準 variant を reexport するため、通常は修飾名なしで書ける。

act の operation も同じ。

yulang
console::println_native "hi"

Dot selection

expr.method は、まず receiver の型に結び付いた field や method を探し、その後 role method や effect-row method も解決対象にする。

  • struct field と with: method
  • type ... with: で定義された method
  • .add.index.show のような role method
  • .list.logic.once のような effect-row method

anonymous record の場合、.field は record field を取り出す。act operation 自体は、通常 console::println_native "hi" のように path で呼ぶ。

Standard library modules

Module内容
std::preludeentry file が通常 import する基本定義、role、operator、std reexport
std::ops+, -, *, /, comparison, and, or, not
std::listlist operations と Index impl
std::rangerange constructors と Fold impl
std::optopt 'a と、prelude reexport された nil / just
std::resultresult 'ok 'err と、prelude reexport された ok / err
std::strstr と indexing
std::varref 'e 'a と update helper
std::flowsub, loop control, label loop primitives
std::foldFold role と default method .find / .contains
std::undeteach, guard, .list, .once, .logic
std::junctionall, any
std::consolesay, println, print, host-handled print_native / println_native
std::fsread_text, read_at, open, write_text, exists, is_file, is_dirfs_err
std::errorThrow role
std::indexIndex role

Yulang