From 72399b385bfa90078858056c4dd5bfc83bf6c1b0 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 27 Sep 2024 03:45:42 +0900 Subject: [PATCH] docs: Add document for type assertion and type application These syntax were added in Steep-1.3. refs: https://github.com/soutaro/steep/wiki/Release-Note-1.3#add-type-assertion-and-type-application-syntax --- manual/annotations.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/manual/annotations.md b/manual/annotations.md index e78bd0cc9..8ee2dc6c7 100644 --- a/manual/annotations.md +++ b/manual/annotations.md @@ -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*