Skip to content

Commit

Permalink
Merge pull request #1235 from tk0miya/docs/type_assertion_and_applica…
Browse files Browse the repository at this point in the history
…tion

docs: Add document for type assertion and type application
  • Loading branch information
soutaro authored Nov 25, 2024
2 parents 14b562e + 22944aa commit 6037b0c
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 6037b0c

Please sign in to comment.