About this guide
The Motoko Programming Language Guide introduces key features of the general-purpose Motoko programming language and provides examples and reference information to help you learn the nuances of the language and the practical implications of how to apply it.
Motoko 编程语言指南介绍了通用 Motoko 编程语言的主要功能,并提供了示例和参考信息,以帮助您了解该语言的细微差别以及如何应用它的实际含义。
The Motoko programming language is optimized for developing programs that run on the Internet Computer blockchain network and to work with the IC SDK. You could, in principle, also write programs using Motoko for more traditional platforms and to run in other contexts, though support for this is currently best-effort and incomplete. This guide attempts to strike a balance between highlighting features that are uniquely suited to running on the Internet Computer and features that are generally-applicable or well-suited for programs running on all targets.
Motoko 编程语言针对开发在互联网计算机区块链网络上运行并与 IC SDK 配合使用的程序进行了优化。 原则上,您还可以使用 Motoko 为更传统的平台编写程序并在其它上下文中运行,尽管目前对此的支持是尽最大努力且不完整。 本指南试图在突出显示特别适合在 Internet 计算机上运行的功能与普遍适用或非常适合在所有目标上运行的程序的功能之间取得平衡。
Intended audience
This guide provides reference information and examples for programmers who want to explore or plan to use the Motoko programming language. Most of the information in this guide is applicable independent of whether you are developing programs to run on the Internet Computer or working with the IC SDK.
本指南为想要探索或计划使用 Motoko 编程语言的程序员提供参考信息和示例。 无论您是开发在互联网计算机上运行的程序还是使用 IC SDK,本指南中的大部分信息都适用。
The guide assumes you are familiar with basic programming principles and terminology and have at least some experience writing programs in a high-level programming language such as C++ or Rust, or have practical experience working with a scripting language such as JavaScript or TypeScript. In addition, Motoko incorporates some aspects of functional programming, so you might find some knowledge of functional programming design principles helpful in learning to use Motoko.
本指南假设您熟悉基本编程原理和术语,并且至少具有使用 C++ 或 Rust 等高级编程语言编写程序的一些经验,或者具有使用 JavaScript 或 TypeScript 等脚本语言的实际经验。 此外,Motoko 还融入了函数式编程的某些方面,因此您可能会发现一些函数式编程设计原则知识有助于学习使用 Motoko。
Although this guide is intended to help readers from different backgrounds understand the basic design principles and semantics of the Motoko, you should keep in mind that the language implementation and the documentation are also continuing to evolve.
尽管本指南旨在帮助来自不同背景的读者了解 Motoko 的基本设计原则和语义,但您应该记住,语言实现和文档也在不断发展。
Using this guide
To provide a framework for learning Motoko, you might want to start by reviewing Engineering values and goals. The Engineering values and goals describe the core design considerations for the development and evolution of the Motoko programming language.
With those considerations in mind, you can start to explore fundamental concepts, including the role of types and type annotations, in simple code examples and small programs.
Once you are familiar with the basic concepts and terminology, later sections introduce programs that compute in more interesting ways, including function abstractions, user-defined type definitions, user-defined actors, and asynchronous communication.
As you begin using Motoko to write your own programs, you can return to this guide for reference information and examples.
Most of the code examples in this guide are interactive: you can live edit the example, interpret the code in the browser and see the result. The interpreter is provided for education purposes. While most of the language features are supported in the interpreter, they are not exactly the same as the real compiler. For example, you may get a stack overflow for a medium-size input, while the real compiler handles the input just fine. Some of the system features are not fully supported, such as cycles, canister imports and state-mutating query calls.
Documentation conventions
The following conventions are used in this guide:
Fixed-width font is used for sample code, program names, program output, file names, and commands that you type at the command line.
Bold text is used to emphasize commands, buttons, or user interface text, and to introduce new terms.
Italics are used for book titles and to emphasize specific words or terms.
The CAUTION style is used to indicate content that is missing or incomplete.
The WARNING style is used to indicate where content is outdated or potentially inaccurate.
The NOTE style is used to indicate content that describes a feature that is not yet supported but is planned for a future release.
Engineering values and goals
The engineering effort behind the design and implementation of Motoko is driven by a core set of values and goals. The DFINITY engineering organization uses these values and goals to define and prioritize the language features and enhancements to add and improve as part of ongoing language development.
For transparency into the principles that guide the engineering effort, the engineering organization has identified the following sets of core values and secondary values for driving the direction of the Motoko programming language.
Core values
The following guiding principles represent the core values of the engineering organization in prioritized order:
Seamless integration with the Internet Computer blockchain network to ensure that Motoko provides full language support for the actor-based model, asynchronous messaging, data persistence, interface description language interoperability, and other features. 与互联网计算机区块链网络无缝集成,确保Motoko为基于角色的模型、异步消息传递、数据持久化、接口描述语言互操作性等功能提供完整的语言支持。
Ergonomics to ensure that Motoko embraces familiarity, simplicity, clarity, explicitness, and other human factors. 人体工程学确保 Motoko 具有熟悉性、简单性、清晰性、明确性和其他人为因素。
Formal correctness to ensure that Motoko maintains state isolation, a sound type system and type safety, precision, pattern matching, appropriate defaults, and coding best-practices. 形式正确性确保 Motoko 保持状态隔离、健全的类型系统和类型安全、精度、模式匹配、适当的默认值和编码最佳实践。
Secondary values
The following principles represent the secondary values of the engineering organization that are deemed important but not primary driving factors:
以下原则代表了工程组织的次要价值观,这些价值观被认为是重要的,但不是主要驱动因素:
Expressiveness, so that Motoko provides first-class functions, polymorphism, pattern matching, and more as the language evolves.
Performance, so that Motoko provides reasonably fast operation initially and continues to improves as the language evolves.
Readiness, so the Motoko comes with "batteries included" in the form of libraries and examples and out-of-the-box integration with the IC SDK.
Non-goals
As a counterpoint to the core values and goals, the engineering organization also identified the following as "non-goals" that are outside of the scope of the engineering effort: 作为与核心价值观和目标相对应的,工程组织还将以下内容确定为工程工作范围之外的“非目标”:
Having a more advanced type system, with cutting-edge features.
Simplicity over functionality in design or implementation (the "Worse is Better" approach).
Interoperability or support for running Motoko programs on blockchains other than the Internet Computer. 互操作性或支持在互联网计算机以外的区块链上运行 Motoko 程序。
Finding more information
For information about using Motoko with the IC SDK for creating and managing canisters, see the Developer Resources.
For background information on various topics relevant to the design, use, or deployment of Motoko services, or the language’s design itself, consider the following resources as a starting point:
WebAssembly
Modern type systems
Practical Foundations for Programming Languages by Robert Harper. Cambridge University Press, 2016.
Types and Programming Languages by Benjamin C. Pierce. The MIT Press.
Getting additional support
If you are looking for more information or technical support, the DFINITY website provides quick access to frequently-asked questions, technical articles, developer updates, and other resources. From the website, you can search knowledge base articles, open and view support cases, sign up for the newsletter, read the latest blog posts, view how-to videos, download software updates, or exchange ideas with members of the community.
如果您正在寻找更多信息或技术支持,DFINITY 网站可以快速访问常见问题、技术文章、开发人员更新和其他资源。 在该网站上,您可以搜索知识库文章、打开和查看支持案例、注册新闻通讯、阅读最新的博客文章、查看操作视频、下载软件更新或与社区成员交流想法。
In addition to the resources available on the website, you can connect with DFINITY or other developers using social media or by visiting the DFINITY Community Forum on Discourse and joining the conversation.
除了网站上提供的资源之外,您还可以使用社交媒体或访问 DFINITY 社区论坛并加入对话来与 DFINITY 或其他开发人员联系。
Motoko 编程语言指南