Skip to content

Type Testing Functions or Methods #9

Open

Description

@DavePearce

Unfortunately, it is quite difficult to perform type testing on a function or method. The primary reason for this is that JavaScript does not embed enough type information into function pointers. For example, consider the following:

type Sized is { int value }
type SizeGetter is function(Sized) -> int
type SizeSetter is function(Sized,int) -> Sized

function f((SizeSetter|SizeGetter) x) -> int:
    if x is SizeGetter:
        return 1
    else:
        return 0

This currently translates into the following JavaScript:

function f_u2n10SizeGettern10SizeSetter(x) {
    if(is$n10SizeGetter(x)) {
        return 1;
    } else {
        return 0;
    }
}

function is$f1n5Sized1Ie(val) {
    if(val != null && typeof val === "function") {
        return true;
    }
    return false;
}

Unfortunately, as we can see, typeof val could at best return function in this case. To go beyond this, we need to embed some kind of information into the function pointer itself. Or, alternatively, consider a different way of implementing type tests (e.g. tagging).

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