Skip to content

Commit

Permalink
Merge pull request #1995 from ruby/update-docs
Browse files Browse the repository at this point in the history
Update `syntax.md`
  • Loading branch information
soutaro authored Sep 6, 2024
2 parents 236c845 + 4ca5ee5 commit 7ba55ab
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions docs/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 7ba55ab

Please sign in to comment.