Skip to content Skip to sidebar Skip to footer

Can A Regular Expression Be Crafted Which Determines The Return Type Of A Function?

Given the text of the following function: function f3() { return Math.random() > 0.5 ? Promise.resolve(true): 'naaah' } Can a regular expression be compiled which to determi

Solution 1:

Given the code, we cannot determine the return type of this via regular expressions. Proof by Contradiction:

  1. Assume we can find the return type by using a regular expression.

  2. The return type is decided in the final line, via a non-deterministic call to random (it isn't known ahead of time).

  3. Regular expressions are deterministic- they always have the same output when run on the same input.

  4. Thus, the function's return type must be static, since we can run a regular expression and find the return type (and that regular expression will always return the same thing).

  5. Contradiction: The function's return type must be static, but the function's return type is variable due to random

Note: The above assumes the type must be a single one, and does not account for Union types on its own.

Additionally, a proof on why we can't do it always.

  1. Assume that somehow we can figure out every type used in the function in some way.

  2. At some point, we generate a string a="A"+Math.random()

  3. At some later point, we generate a string b="class "+a+"{...}

  4. After this definition, we execute c= eval("new "+a+"()").

  5. When we return c, no regular expression can know the type- the type is newly named in the function, determined randomly and is different for every execution.

This assumes the question was a more general "can javascript regex find the return type of a generic function." Note that if it were possible to find the return type, it would be possible to tell that the program halts (it can't return if it doesn't halt, and the return type would have to be undefined if it never halts), and the Halting problem is still unsolved.

Solution 2:

No, it is not possible to programmatically determine the return type of that function using only RegEx. In order to understand what that function does, you need to parse its code. However, JavaScript is a Chomsky Type 2 grammar (context free grammar) and RegEx is a Chomsky Type 3 grammar (regular grammar). JavaScript is fundamentally too complex to be parsed with RegEx.

If you're just trying to deal with the fact that you have the function as text and you need to evaluate it, you could use the eval() function.

Solution 3:

The answer is no. Since the return type is determined on the result of Math.random() and result of Math.random() is not known until runtime. This particular function must be executed in order to know the return type.

Solution 4:

EDIT based on clarification from OP:

You can certainly get text of a function using f3.toString(). But building reliable regex to parse return value type will never be reliable. The last line in function may or may not be returning a value. In some cases you explicitly have return statements in middle of function. How would you handle that? You will end up twinkling regex for rest of your development cycle

So if I understand correctly, you want to determine what type of value function returned after calling it. Looks like there are two possibilities: a Boolean true and String 'naaah'

You can determine return type using typeof operator:

functionf3() {
  returnMath.random() > 0.5 ? Promise.resolve(true): 'naaah'
}
returnVal=f3()
if ((typeof returnVal)=='string')
  console.log("Got String)
if ((typeof returnVal)=='boolean')
  console.log("Got boolean")

If you really really really want to use regex to determine return type:

functionf3() {
  return Math.random() > 0.5 ? Promise.resolve(true): 'naaah'
}
returnVal=f3().toString()
var regex = /^naaah$/;
if (regex.test(returnVal))
  console.log("Got String)
else 
  console.log("Got boolean")

Solution 5:

You can use .toString() to convert function to string, .replace() with RegExp/Math\.random\(\)\s[>]\s0\.5/ to get Math.random() > 0.5 portion of function, which we call at replacement function of .replace() using Function constructor.

As it makes no difference when that portion of f3 is called, we only need the Boolean result of Math.random() > 0.5 to facilitate the conditional operator result.

Again we use RegExp to check what Math.random() > 0.5 returns for only that part of function using /return\strue/, RegExp.prototype.test() and to get the expected type, either Promise or String.

We then use Function constructor again, passing return f3, confident that we have predicted the return type of the function f3 when called.

functionf3() {
  returnMath.random() > 0.5 ? Promise.resolve(true): 'naaah'
}

f3 = f3.toString().replace(/Math\.random\(\)\s[>]\s0\.5/, function(match) {
  returnnewFunction(`return ${match}`)()
});

// here we can visually determine whether `true` or `false` is returned;// though as vision is based on perspective, we make sure of the type// using `RegExp` at defining `type`console.log(f3);

// here we determine `true` or `false` using `RegExp`let type = /return\strue/.test(f3) ? "Promise" : "String";

console.log(`f3 return type: ${type}`);

f3 = newFunction(`return ${f3}`);

console.log(f3()());

Post a Comment for "Can A Regular Expression Be Crafted Which Determines The Return Type Of A Function?"