Skip to content

Commit

Permalink
docs: Add document for type assertion and type application
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Sep 26, 2024
1 parent a63fce4 commit 72399b3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions manual/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,40 @@ This annotation tells instance variable of instance.

* `@type` `instance` `ivar` *ivar* `:` *type*
* `@type` `module` `ivar` *ivar* `:` *type*

## Type assertion

Type assertion allows declaring type of an expression inline, without introducing new local variable with variable type annotation.

### Example

```
array = [] #: Array[String]
path = nil #: Pathname?
```

##### Syntax

* `#:` *type*

## Type application

Type application is for generic method calls.

### Example

```
table = accounts.each_with_object({}) do |account, table| #$ Hash[String, Account]
table[account.email] = account
end
```

The `each_with_object` method has `[T] (T) { (Account, T) -> void } -> T`,
and the type application syntax directly specifies the type of `T`.

So the resulting type is `(Hash[String, Account]) { (Account, Hash[String, Account]) -> void } -> Hash[String, Account]`.

#### Syntax

* `#$` *type*

0 comments on commit 72399b3

Please sign in to comment.