Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaffd5e0

Browse files
committed
doc
1 parentfb2eb6a commitaffd5e0

File tree

1 file changed

+76
-4
lines changed

1 file changed

+76
-4
lines changed

‎docs/compute-engine/08-guide-types.md‎

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,75 @@ console.info(ce.box(3.14).isInteger)
746746
```
747747

748748

749+
##Type Inference
750+
751+
When an explicit type is not provided for a symbol, the Compute Engine will
752+
attempt to**infer** the type of the symbol based on the context in which it is used.
753+
This process is known as**type inference**.
754+
755+
When assigning a value to an undeclared symbol, the type of the value is
756+
used to infer the type of the symbol.
757+
758+
If the symbol is a constant, the type is used exactly as the type of the symbol.
759+
If the symbol is a variable, the type of the value may be widened to a more general
760+
type:
761+
762+
<divclassName="symbols-table"style={{"--first-col-width":"18ch"}}>
763+
764+
765+
| Value Type| Inferred Symbol Type|
766+
|:--------------------|:----------------------|
767+
|`complex` <br/>`imaginary` <br/>`non_finite_number` <br/>`finite_number`|`number`|
768+
|`integer` <br/>`finite_integer`|`integer`|
769+
|`real` <br/>`finite_real` <br/>`rational` <br/>`finite_rational`|`real`|
770+
771+
</div>
772+
773+
Examples:
774+
775+
<divclassName="symbols-table"style={{"--first-col-width":"8ch"}}>
776+
777+
| Value| Value Type| Inferred Symbol Type|
778+
|:--------------------|:--------------------------|:--------------------------|
779+
| 34|`finite_integer`|`integer`|
780+
| 3.14|`finite_real`|`real`|
781+
| 4i|`imaginary`|`number`|
782+
| 1/2|`finite_rational`|`real`|
783+
</div>
784+
785+
```js
786+
ce.assign("n",34);
787+
ce.box("n").type;
788+
// ➔ "integer"
789+
```
790+
791+
When a symbol is used in a function expression, the expected type of the
792+
arguments is used to infer the type of the symbol.
793+
794+
```js
795+
ce.declare("n","unknown");
796+
ce.declare("f","(number) -> number");
797+
ce.box(["f","n"]);
798+
ce.box("n").type;
799+
// ➔ "number"
800+
```
801+
802+
A type that has been inferred can be refined later, for example by
803+
assigning a value of a more specific type to the symbol or by using the
804+
symbol in a context that requires a more specific type.
805+
806+
Continuing the example above:
807+
808+
```js
809+
ce.declare("g","(integer) -> number");
810+
ce.box(["g","n"]);
811+
ce.box("n").type;
812+
// ➔ "integer": "n" has been narrowed
813+
// from "number" to "integer"
814+
```
815+
816+
817+
749818
##Defining New Types
750819

751820
**To define new types** use the`ce.declareType()` function.
@@ -756,15 +825,18 @@ For example, to defines a new type `point` that is a tuple of two
756825
integers,`x` and`y`:
757826

758827
```js
759-
ce.declareType("point","tuple<x: integer, y: integer>");
828+
ce.declareType(
829+
"point",
830+
"tuple<x: integer, y: integer>"
831+
);
760832
```
761833

762834
The type is defined in the current lexical scope.
763835

764836

765837
###Nominal vs Structural Types
766838

767-
By default, types are nominal, meaning that to be compatible, they must have
839+
By default, types are nominal, meaning that to be compatible two types must have
768840
the same name and not just the same structure.
769841

770842
```js
@@ -773,7 +845,7 @@ ce.type("tuple<x: integer, y: integer>")
773845
// ➔ false
774846
```
775847

776-
To make a type structural, use the`ce.declareType()` function with the
848+
**To make a type structural**, use the`ce.declareType()` function with the
777849
`alias` option. Two structural types are compatible if they have the same structure,
778850
regardless of their names.
779851

@@ -794,7 +866,7 @@ ce.type("tuple<x: integer, y: integer>")
794866

795867
A recursive type is a type that refers to itself in its definition.
796868

797-
In this case, you canuse a type before declaring it by prefacing ifwith the`type` keyword.
869+
**Touse a type before declaring it**, preface itwith the`type` keyword in the type expression.
798870

799871
For example, a binary tree can be defined as a tuple of a value and two subtrees:
800872

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp