我试图从语法上解释以字符串形式给出的代码。例如:
val myString = "def f(x:Int):Int=x+1".
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种可以从中返回实函数的方法:例如:
val myIncrementFunction = myDarkMagicFunctionThatWillBuildMyFunction(myString)
println(myIncrementFunction(3))
Run Code Online (Sandbox Code Playgroud)
将打印4
用例:我想稍后在代码中使用该解释代码中的一些简单功能。例如,他们可以提供像def fun(x:Int):Int = x + 1这样的字符串,然后使用解释器来编译/执行该代码,然后我就可以使用此fun(x ),例如在地图中。
问题在于该函数类型对我来说是未知的,这是一个大问题,因为我需要从IMain撤消。我读过有关反射,类型系统等的信息,经过一番谷歌搜索后,我到达了这一点。我也检查了twitter的util-eval,但是从他们的测试中的文档和示例中看不到太多,这是完全一样的。
如果我知道类型,我可以做类似的事情
val settings = new Settings
val imain = new IMain(settings)
val res = imain.interpret("def f(x:Int):Int=x+1; val ret=f _ ")
val myF = imain.valueOfTerm("ret").get.asInstanceOf[Function[Int,Int]]
println(myF(2))
Run Code Online (Sandbox Code Playgroud)
可以正常工作并打印3,但是我被上面提到的问题所困扰,我不知道该函数的类型,而此示例的工作仅是因为我强制转换为在定义用于测试IMain的字符串函数时使用的类型作品。
您知道任何方法可以实现此功能吗?
我是新手,所以如果我写错了,请原谅。
谢谢
reflection functional-programming scala type-inference twitter-util
我正在尝试获取包含我的存储库的特定目录或文件的所有提交。
我尝试了以下代码:
public PlotCommitList getPlotCommits(String path){
System.out.println(path);
PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
PlotWalk revWalk = new PlotWalk(repository);
try {
ObjectId rootId = repository.resolve("HEAD");
if (rootId != null) {
RevCommit root = revWalk.parseCommit(rootId);
revWalk.markStart(root);
revWalk.setTreeFilter(PathFilter.create(path));
plotCommitList.source(revWalk);
plotCommitList.fillTo(Integer.MAX_VALUE);
return plotCommitList;
}
} catch (AmbiguousObjectException ex) {
Logger.getLogger(GitRepository.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(GitRepository.class.getName()).log(Level.SEVERE, null, ex);
}
return plotCommitList;
}
Run Code Online (Sandbox Code Playgroud)
我不只得到影响该文件的提交。我得到了整个列表的一些“子列表”,但不仅仅是那些影响该文件的提交。
也许 TreeFilter 不起作用我怎么想?我应该使用其他方式来获得这些提交吗?我看到 log 命令有一个路径过滤器,但我还没有尝试过,因为它返回一个 RevCommit 列表,对于我的 PlotCommitList,我需要一个 revwalk 来用作源。而且我认为我不能将 RevCommit 转换为 PlotCommit。
一个人在这里遇到了同样的问题(文件A和文件B问题的第一个答案):链接 - 单击此处