From 4ca5ee52e8be007723d7ec0574c51bf5d0b76eb8 Mon Sep 17 00:00:00 2001 From: Soutaro Matsumoto Date: Fri, 6 Sep 2024 14:47:46 +0900 Subject: [PATCH] Update `syntax.md` --- docs/syntax.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/docs/syntax.md b/docs/syntax.md index 3de06848c4..d4b407dcdd 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -750,16 +750,15 @@ $LOAD_PATH: Array[String] ### Generics ```markdown -_module-type-parameter_ ::= _generics-unchecked_ _generics-variance_ _type-variable_ _generics-bound_ +_module-type-parameter_ ::= _generics-unchecked_ _generics-variance_ _type-variable_ _generics-bound_ _default-type_ _method-type-param_ ::= _type-variable_ _generics-bound_ -_generics-bound_ ::= (No type bound) - | `<` _bound-type_ (The generics parameter is bounded) +_generics-bound_ ::= (No type bound) + | `<` _type_ (The generics parameter is bounded) -_bound-type_ ::= _class-name_ _type-arguments_ (Class instance type) - | _interface-name_ _type-arguments_ (Interface type) - | `singleton(` _class-name_ `)` (Class singleton type) +_default-type_ ::= (No default type) + | `=` _type_ (The generics parameter has default type) _generics-variance_ ::= (Invariant) | `out` (Covariant) @@ -826,14 +825,27 @@ class PrettyPrint[T < _Output] end ``` -If a type parameter has an upper bound, the type parameter must be instantiated with types that is a subclass of the upper bound. +If a type parameter has an upper bound, the type parameter must be instantiated with types that is a subtype of the upper bound. ```rbs type str_printer = PrettyPrint[String] # OK type int_printer = PrettyPrint[Integer] # Type error ``` -The upper bound must be one of a class instance type, interface type, or class singleton type. +The generics type parameter of modules, classes, interfaces, or type aliases can have a default type. + +```rbs +interface _Foo[T = untyped] +end + +interface _Bar[T, S = untyped] +end + +type foo = _Foo # equivalent to _Foo[untyped] +type bar = _Bar[String] # equivalent to _Bar[String, untyped] +``` + +Type parameters with default types cannot appear before type parameters without default types. The generic method type parameters cannot have the default types. ### Directives