Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

20
LINES

< > BotCompany Repo | #1020936 // codeToFunctionOnArbitraryType

JavaX fragment (include)

1  
static <A, B> F1<A, B> codeToFunctionOnArbitraryType(S code, S type, Class<A> javaType, S varName) {
2  
  code = trim(code);
3  
  Class c;
4  
  S methodName;
5  
  if (isIdentifier(code)) { // name of standard function
6  
    c = loadFunctions_cached_withInit(code);
7  
    methodName = code;
8  
  } else if (jcontains(code, "calc(")) { // code defines its own calc function
9  
    c = veryQuickJava(code);
10  
    methodName = 'calc;
11  
  } else { // code is an expression or block working on variable s
12  
    S main = "sO calc(final " + type + " " + varName + ") {\n" + tok_addReturn(code) + "\n}";
13  
    c = veryQuickJava(main);
14  
    methodName = 'calc;
15  
  }
16  
  
17  
  final Method m = findMethod_precise_onTypes(c, methodName, javaType);
18  
  if (m == null) fail("Not a method on " + c + ": " + code + "(" + javaType + ")");
19  
  ret (F1) func(O o) -> O { invokeMethod(m, null, o) };
20  
}

Author comment

Began life as a copy of #1020643

download  show line numbers  debug dex  old transpilations   

Travelled to 8 computer(s): bhatertpkbcr, cfunsshuasjs, gwrvuhgaqvyk, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1020936
Snippet name: codeToFunctionOnArbitraryType
Eternal ID of this version: #1020936/4
Text MD5: 8bc6b43c92b6bdf43c6b9f05120897b6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-01-17 20:26:48
Source code size: 859 bytes / 20 lines
Pitched / IR pitched: No / No
Views / Downloads: 254 / 265
Version history: 3 change(s)
Referenced in: [show references]