Skip to content

Types passed to a higher-order function can get mixed up. #123

Closed as not planned

Description

@dimvar

Here, Flow warns that boolean and number are incompatible. Also, it warns that we may be passing a string to minus.

function app(f, x) {
  return f(x);
}
var call1 = app(function(x) { return x - 1; }, 123);
var call2 = app(function(x) { return x ? 'qwer' : 'adsf'; }, false);
call1 - 5;

Activity

  1. avikchaudhuri commented on Nov 26, 2014

    @avikchaudhuri
    Contributor

    Same issue of Flow's inference not being polymorphic enough. Seems like we need to incorporate your CFA2 work into our analysis! :)

  2. dimvar commented on Nov 26, 2014

    @dimvar
    ContributorAuthor

    I didn't realize that Flow analyzes function calls context insensitively, and that one has to annotate explicitly for polymorphism. Thanks for the tip, it explains a lot in my examples.

  3. gcnew commented on Dec 9, 2016

    @gcnew

    Is the following another instance of this issue?

    function flip<A, B, R>(f: (a: A, b: B) => R): (b: B, a: A) => R {
        return (b, a) => f(a, b);
    }
    
    function fconst<A, B>(x: A, _: B): A {
        return x;
    }
    
    const g = flip(fconst);
    const a1: string = g(1, "hello"); // error
    const a2: number = g("hello", 1); // error
  4. SamChou19815 commented on Jul 30, 2022

    @SamChou19815
    Contributor

    Flow decides to require annotation for polymorphic functions a long time ago.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions