0

Having a function taking two arguments:

let yolo x y = 
  x + y

Is it possible to get information (or preferably, value) of one of the applied arguments after an application? Below is a pseudo-code that summarizes what I want to achieve.

let yolo_x = yolo 3
.
.//something
.
let applied_x = Reflection.getListOfAppliedArguments yolo_x
zajer
  • 649
  • 6
  • 17

1 Answers1

2

The compiler does not give you any guarantees about how it compiles such code, so, in general, there is no way of doing this.

In some cases, it seems that the compiler actually represents first-class functions as objects with the value of the parameter stored in a field. This is something you can get via reflection easily:

open System.Reflection

let foo () =
  let yolo x y = x + y
  let yolo_x = yolo 3
  yolo_x.GetType().GetField("x").GetValue(yolo_x)

foo ()

In a case like this where both the function and the partially applied value live inside another local scope (inside foo), the above actually works.

But if you move yolo_x or yolo outside of foo and make them top-level declaration, the compiled code is more optimized and this won't work.

Tomas Petricek
  • 240,744
  • 19
  • 378
  • 553
  • 1
    It seems to me that this approach only works in F# interactive (dotnet fsi). – zajer Jul 18 '23 at 00:57
  • When you copy and paste your example to an .fs file and `dotnet run` it it gives NullReferenceException because it seems to be no such field as "x" (in my case there are two: "f" and "t"). In interactive mode everything works fine. – zajer Jul 18 '23 at 01:04
  • @zajer Alas, I think you're right. I only tested this in the F# Interactive, which apparently behaves differently. – Tomas Petricek Jul 18 '23 at 22:33