Introducing
Type Guard
to Steep
Takesh KOMIYA (@tk0miya)
タイトルテキスト
self.introduction
タイトルテキスト
self.introduction
タイトルテキスト
My works for RBS ecosystem
タイトルテキスト
My works for RBS ecosystem
タイトルテキスト
My works for RBS ecosystem
タイトルテキスト
タイトルテキスト
My works for RBS ecosystem
タイトルテキスト
My works for RBS ecosystem
タイトルテキスト
We’re hiring
We’re system integrator in Japan. Visit https://timedia.co.jp/
タイトルテキスト
Big thanks to my sponsors
Kazamori LLC.
Gold sponsors
Tea sponsors
タイトルテキスト
Introducing Type Guard to Steep
タイトルテキスト
Introducing Type Guard
to Steep
タイトルテキスト
Introducing Type Guard to Steep
タイトルテキスト
Could you find a bug?
タイトルテキスト
Could you find a bug?
To find a bug, let’s analyze this by Steep
タイトルテキスト
Could you find a bug?
Steep detects a bug in the code
タイトルテキスト
Could you find a bug?
Steep detects a bug in the code
Type `(::User | nil)` does not have method `name`
タイトルテキスト
Could you find a bug?
Steep detects a bug in the code
Type `(::User | nil)` does not have method `name`
Steep recognizes user’s type is (::User | nil)
タイトルテキスト
How do you fix?
Next, let’s fix the bug. How do you fix it?
タイトルテキスト
How do you fix?
Next, let’s fix the bug. How do you fix it?� My answer is nil-check
タイトルテキスト
How do you fix?
タイトルテキスト
How do you fix?
We confirm
user is not nil
by nil-check
タイトルテキスト
How do you fix?
We confirm
user is not nil
by nil-check
user is User | nil
user is User
user is nil
タイトルテキスト
How do you fix?
This behavior is called as “Type narrowing”
user is User | nil
user is User
user is nil
We confirm
user is not nil
by nil-check
タイトルテキスト
How do you fix?
This behavior is called as “Type narrowing”
user is User | nil
user is User
user is nil
Expression for type narrowing is called as “Type guard”
タイトルテキスト
Steep and Type narrowing
Steep supports Type narrowing
タイトルテキスト
Steep and Type narrowing
Steep supports some type guards
タイトルテキスト
Steep and Type narrowing
Steep supports type narrowing by built-in methods�(e.g. is_a?)
タイトルテキスト
Steep and Type narrowing
Steep supports type narrowing by built-in methods�(e.g. is_a?)
Type guard found.
Let’s narrowing
タイトルテキスト
Steep and Type narrowing
Steep supports type narrowing by built-in methods�(e.g. is_a?)
user is narrowed
into User
Type guard found.
Let’s narrowing
タイトルテキスト
Interim Summary
タイトルテキスト
Introducing Type Guard to Steep
タイトルテキスト
Narrowing by #present?
タイトルテキスト
Narrowing by #present?
We confirm
the user is present
タイトルテキスト
Narrowing by #present?
We expect
user is narrowed to User
We confirm
the user is present
タイトルテキスト
Narrowing by #present?
We confirm
the user is present
We expect
user is narrowed to User
タイトルテキスト
Narrowing by #present?
Steep considers #present? is not
a type guard
We confirm
the user is present
We expect
user is narrowed to User
タイトルテキスト
Narrowing by #present?
タイトルテキスト
Type Guard for Union Types
(※)
(※) Pure method is a method with no side effects. See the document of Steep.
タイトルテキスト
Type Guard for Union Types
As a preparation, set the types of #present? up
タイトルテキスト
Type Guard for Union Types
Let’s check it again
タイトルテキスト
Type Guard for Union Types
Let’s check it again
Steep considers
this expression is a Type Guard
Because
…
タイトルテキスト
Type Guard for Union Types
Let’s check it again
Steep considers
this expression is a Type Guard
Because
タイトルテキスト
Type Guard for Union Types
Let’s check it again
Steep considers
this expression is a Type Guard
Because
タイトルテキスト
Type Guard for Union Types
Let’s check it again
Steep considers
this expression is a Type Guard
Because
User | nil
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
This type guard contains
two method calls
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
This type guard contains
two method calls
User#present?: () -> true
NilClass#present?: () -> false
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
This type guard contains
two method calls
User#present?: () -> true
NilClass#present?: () -> false
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
This type guard contains
two method calls
User#present?: () -> true
NilClass#present?: () -> false
Calls having truthy return type
is used to if-block narrowing
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
This type guard contains
two method calls
User#present?: () -> true
NilClass#present?: () -> false
Calls having truthy return type
is used to if-block narrowing
Calls having falsey return type
is used to else-block narrowing
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
Narrowed to User
because User#present? returns true
Narrowed to nil
because NilClass#present? returns false
タイトルテキスト
Type Guard for Union Types
Next, do narrowing
🎉🎉🎉🎉🎉
Narrowed to User
because User#present? returns true
Narrowed to nil
because NilClass#present? returns false
タイトルテキスト
Case of String#present?
String#present? returns both true and false
タイトルテキスト
Case of String#present?
String#present? returns both true and false
タイトルテキスト
Case of String#present?
IO#gets returns String | nil
タイトルテキスト
Case of String#present?
IO#gets returns String | nil
String#present?: () -> bool
NilClass#present?: () -> false
タイトルテキスト
Case of String#present?
String#present?: () -> bool
NilClass#present?: () -> false
Narrowed to String
because String#present? returns bool
Narrowed to String | nil
because String#present? returns bool
and NilClass#present? returns false
IO#gets returns String | nil
タイトルテキスト
Current status of
Type Guard for Union Types
タイトルテキスト
Current status of
Type Guard for Union Types
🎉
タイトルテキスト
Current status of
Type Guard for Union Types
タイトルテキスト
Current status of
Type Guard for Union Types
🎉
タイトルテキスト
Interim Summary
I introduced Type Guard for Union Types to Steep
タイトルテキスト
Introducing Type Guard to Steep
タイトルテキスト
Narrowing by own method
In real world apps, we often have own methods that determine the class or a state of the object
タイトルテキスト
Narrowing by own method
In real world apps, we often have own methods that determine the class or a state of the object
It would be great if Steep supports them. Let’s go!
タイトルテキスト
Narrowing by own method
タイトルテキスト
Narrowing by own method
This code calls #admin? to classify user to admin or not
タイトルテキスト
Narrowing by own method
This code calls #admin? to classify user to admin or not
We confirm
the user is an admin
タイトルテキスト
Narrowing by own method
This code calls #admin? to classify user to admin or not
We expect
user is narrowed to AdminUser
We confirm
the user is an admin
タイトルテキスト
Narrowing by own method
This code calls #admin? to classify user to admin or not
We expect
user is narrowed to AdminUser
We confirm
the user is an admin
タイトルテキスト
Narrowing by own method
This code calls #admin? to classify user to admin or not
We expect
user is narrowed to AdminUser
Steep considers #admin? is not
a type guard
We confirm
the user is an admin
タイトルテキスト
Narrowing by own method
タイトルテキスト
User-defined Type Guard
User-defined Type Guard is declared as annotated method
タイトルテキスト
User-defined Type Guard
User-defined Type Guard is declared as annotated method
タイトルテキスト
User-defined Type Guard
User-defined Type Guard is declared as annotated method
タイトルテキスト
User-defined Type Guard
User-defined Type Guard is declared as annotated method
The type of receiver �will be narrowed to AdminUser
タイトルテキスト
User-defined Type Guard
The predicate consists of subject, verb and type
Subject verb Type
“self”
argument name
“is”
“is_a”
Type name
“self”
argument name
タイトルテキスト
User-defined Type Guard
The predicate consists of subject, verb and type
Subject verb Type
タイトルテキスト
Narrowing by
User-defined Type Guard
Let’s analyze the code with User-defined Type Guard
タイトルテキスト
Narrowing by
User-defined Type Guard
Let’s analyze the code with User-defined Type Guard
This call is considered as type guard
by declaration
タイトルテキスト
Narrowing by
User-defined Type Guard
Let’s analyze the code with User-defined Type Guard
🎉🎉🎉
This call is considered as type guard
by declaration
Narrowed to AdminUser
based on the predicate
“self is AdminUser”
タイトルテキスト
Interim Summary
タイトルテキスト
User-defined Type Guard
Limitations (1)
User-defined Type Guard cannot “change” the type
Invalid.
Object is not
a subtype of User
タイトルテキスト
User-defined Type Guard
Limitations (2)
The type in else-block could not be narrowed well
タイトルテキスト
Advanced usage
Conditional Types
User-defined Type Guard can represent conditional types
タイトルテキスト
Advanced usage
Conditional Types
タイトルテキスト
Advanced usage
Conditional Types
タイトルテキスト
Advanced usage
Conditional Types
タイトルテキスト
Advanced usage
Conditional Types
article is narrowed to
Article & Published
#title is also narrowed
String from String?
タイトルテキスト
Advanced usage
Conditional Types
タイトルテキスト
Introducing Type Guard to Steep
タイトルテキスト
Conclusion
タイトルテキスト
Acknowledges
タイトルテキスト
タイトルテキスト
タイトルテキスト
Current status of
Type Guard for Union Types
タイトルテキスト