Skip to content

Commit 7ba55ab

Browse files
authored
Merge pull request #1995 from ruby/update-docs
Update `syntax.md`
2 parents 236c845 + 4ca5ee5 commit 7ba55ab

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

docs/syntax.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -750,16 +750,15 @@ $LOAD_PATH: Array[String]
750750
### Generics
751751

752752
```markdown
753-
_module-type-parameter_ ::= _generics-unchecked_ _generics-variance_ _type-variable_ _generics-bound_
753+
_module-type-parameter_ ::= _generics-unchecked_ _generics-variance_ _type-variable_ _generics-bound_ _default-type_
754754

755755
_method-type-param_ ::= _type-variable_ _generics-bound_
756756

757-
_generics-bound_ ::= (No type bound)
758-
| `<` _bound-type_ (The generics parameter is bounded)
757+
_generics-bound_ ::= (No type bound)
758+
| `<` _type_ (The generics parameter is bounded)
759759

760-
_bound-type_ ::= _class-name_ _type-arguments_ (Class instance type)
761-
| _interface-name_ _type-arguments_ (Interface type)
762-
| `singleton(` _class-name_ `)` (Class singleton type)
760+
_default-type_ ::= (No default type)
761+
| `=` _type_ (The generics parameter has default type)
763762

764763
_generics-variance_ ::= (Invariant)
765764
| `out` (Covariant)
@@ -826,14 +825,27 @@ class PrettyPrint[T < _Output]
826825
end
827826
```
828827

829-
If a type parameter has an upper bound, the type parameter must be instantiated with types that is a subclass of the upper bound.
828+
If a type parameter has an upper bound, the type parameter must be instantiated with types that is a subtype of the upper bound.
830829

831830
```rbs
832831
type str_printer = PrettyPrint[String] # OK
833832
type int_printer = PrettyPrint[Integer] # Type error
834833
```
835834

836-
The upper bound must be one of a class instance type, interface type, or class singleton type.
835+
The generics type parameter of modules, classes, interfaces, or type aliases can have a default type.
836+
837+
```rbs
838+
interface _Foo[T = untyped]
839+
end
840+
841+
interface _Bar[T, S = untyped]
842+
end
843+
844+
type foo = _Foo # equivalent to _Foo[untyped]
845+
type bar = _Bar[String] # equivalent to _Bar[String, untyped]
846+
```
847+
848+
Type parameters with default types cannot appear before type parameters without default types. The generic method type parameters cannot have the default types.
837849

838850
### Directives
839851

0 commit comments

Comments
 (0)