Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Default variance for type parameters #110

Description

@periodic1236

The section on Type Parameters in Ch. 14 states the following (emphasis mine).

generic-type-parameter-variance indicates the variance for that parameter: + for covariance, - for contravariance. If generic-type-parameter-variance is omitted, covariance is assumed.

However, the default behavior for type parameters without a variance annotation appears to be invariant. If covariance were assumed, the following file should be error-free.

<?hh // strict

class A<T> {}

function foo(A<num> $x): void {}

function test(): void {
  $x = new A<int>();
  foo($x);
}

Nevertheless, the type checker reports:

foo.php:9:7,8: Invalid argument (Typing[4110])
  foo.php:5:16,18: This is a num (int/float)
  foo.php:8:14,16: It is incompatible with an int
  foo.php:5:16,18: Considering that this type argument is invariant with respect to A

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions