2

I'm trying to come up with a way to hash scala functions. That is, I want to be able to generate a hash for a function that is consistent between runtimes if the function hasn't changed, but is different if the logic has changed in any way.

Anonymous scala functions will each generate their own .class file. It'll be similar to the classname which will be something like "package.Main$$anonfun$2" And you can get a handle to that and hash the bytes just fine. However, this only goes one level deep.

if this

val nested = (x:Int) => x + 1
val f = (x:Int) => nested(x) + 1

is changed to this

val nested = (x:Int) => x + 2
val f = (x:Int) => nested(x) + 1

Then the class file for nested will change, but the class file for f will not. I'm trying to figure out how to find out what the class file for nested is if all you have is the reference to f.

I've been trying out using the ASM library and so far I haven't gotten what I want. In this example nested will have a class name something like "package.Main$$anonfun$1" and f will have a class name like "package.Main$$anonfun$2". Starting with f I know its classname, but traversing it with ASM class/field/method visitors all I'm getting is "nested$1" instead of "package.Main$$anonfun$1" like I would want. Anyone got any ideas?

nimbusgo
  • 41
  • 1
  • 2
  • I'd think of another approach here. Take a look at reification: http://stackoverflow.com/a/11056581/1349366 You will be able get the AST of your function (however, only one level, and you have to wrap the body function body in your helper macro). – Aivean Apr 04 '16 at 23:56
  • I've explored using macros but so far I've encountered the same problem. As far as I can tell, macros will give you the AST for whatever is in the macro expression, but not for whatever it references. I want to actually follow the references. Is there a way to use macros to follow the references? – nimbusgo Apr 05 '16 at 19:19

0 Answers0