Type.ForFunction

D

T

The M Code Behind the Power Query M function Type.ForFunction

In this article, we will explore the M code behind the Power Query M function Type.ForFunction. This function is commonly used to check if a given value is a function or not. Let’s dive in!

What is Type.ForFunction?

Before we dive into the M code behind Type.ForFunction, let’s understand what this function does. Type.ForFunction is a Power Query M function that checks if a given value is a function or not. If the given value is a function, it returns a function type. If the value is not a function, it returns null.

The syntax for Type.ForFunction is as follows:


Type.ForFunction(function as any) as nullable type


Here, the function parameter is the value that needs to be checked for its type. The return type is nullable, which means it can return a value or null.

The M Code Behind Type.ForFunction

The M code behind Type.ForFunction is relatively simple. Let’s break it down step by step.


(Type.FunctionType(_, _) meta [IsType = true])(function)


The first part of this code, `(Type.FunctionType(_, _)`, creates a function type using the Type.FunctionType M function. This function takes two parameters, which are used to describe the function type. The first parameter is the list of function parameter types, and the second parameter is the return type of the function.

In our case, we are using a wildcard (_) for both the function parameter types and the return type. This is because we want to check if the given value is a function, without knowing the exact parameter types or return type.

The next part of the code, `meta [IsType = true])`, adds metadata to the function type. This metadata is used to mark the function type as a type, which can be used by other M functions.

Finally, we apply this function type to the given value using the function parameter in parentheses, `(function)`. This returns the function type if the given value is a function, or null if it is not.

Examples

Let’s look at some examples of how Type.ForFunction can be used.


Type.ForFunction(#"Function Value")


Here, we are checking if the value #”Function Value” is a function. If it is, the function type will be returned. If it is not, null will be returned.


Type.ForFunction(123)


In this example, we are checking the value 123 for its type. Since 123 is not a function, null will be returned.


Type.ForFunction((x) => x 2)


Here, we are checking a lambda function that doubles a given value. Since this is a function, the function type will be returned.

In conclusion, the Type.ForFunction M function is a useful tool for checking if a given value is a function or not. The M code behind this function is relatively simple, but it is an essential part of the Power Query M language.

By using Type.ForFunction, you can easily check if a value is a function, which can be helpful when creating custom functions in Power Query.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)