简体   繁体   English

将工作 lua 代码(用于锤子勺)翻译成茴香; 预期的用户数据类型

[英]translating working lua code (for hammerspoon) to fennel; expected userdata type

Caveat: I don't really know lua or fennel, though I do have a lot of experience in lua-like languages (python etc.) and fennel-like languages (clojure, elisp, etc.).警告:我真的不知道 lua 或茴香,尽管我在类 lua 语言(python 等)和类茴香语言(clojure、elisp 等)方面确实有很多经验。

But: I was using hammerspoon to manage external monitor configuration on my mac.但是:我正在使用hammerspoon来管理我的mac 上的外部显示器配置。 I had the following, correctly-working, code in my init.lua file for hammerspoon to go get my external monitor and switch it to vertical:我的 init.lua 文件中有以下正确工作的代码,用于将 Hammerspoon 转换为 go 获取我的外接显示器并将其切换为垂直:

function rotate90()
   hs.screen.find('Dell'):rotate(90)
end

But then I realized that I wanted to use the pre-built modal menu toolkit spacehammer , which is basically like spacemacs for hammerspoon---but which is written in Fennel.但后来我意识到我想使用预先构建的模态菜单工具包spacehammer ,它基本上就像hammerspoon 的 spacemacs ——但它是用 Fennel 编写的。 Ok, I like lisps, I can handle a config file in a lisp, right?好的,我喜欢 lisp,我可以在 lisp 中处理配置文件,对吧?

So first I get spacehammer working, and verify that the default configuration works fine and doesn't throw any errors.所以首先我让 spacehammer 工作,并验证默认配置是否正常工作并且不会引发任何错误。 Check.查看。

Then I go to port over my simple preexisting functions: I translate my lua function into a Fennel function with a slightly different name: Then I go to port over my simple preexisting functions: I translate my lua function into a Fennel function with a slightly different name:

(fn rotate-vertical []
  (let [monitor (hs.screen.find "Dell")]
    (monitor.rotate 90)))

Right?正确的? It's your basic lisp translation, it should work perfectly fine.这是您的基本 lisp 翻译,应该可以正常工作。 But when I try to run it, all of a sudden I get errors coming from c-land:但是当我尝试运行它时,突然间我收到了来自 c-land 的错误:

2021-02-19 17:24:59: 17:24:59 ERROR: LuaSkin: hs.timer callback error: /Users/myhomedir/.spacehammer/config.fnl:24: ERROR: incorrect type 'number' for argument 1 (expected userdata) stack traceback: [C]: in field 'rotate' /Users/myhomedir/.spacehammer/config.fnl:24: in function </Users/myhomedir/.spacehammer/config.fnl:22> 2021-02-19 17:24:59: 17:24:59 错误:LuaSkin:hs.timer 回调错误:/Users/myhomedir/.spacehammer/config.fnl:24:错误:参数 1 的“数字”类型不正确(预期的用户数据)堆栈回溯:[C]:在字段“旋转”/Users/myhomedir/.spacehammer/config.fnl:24:在 function 中</Users/myhomedir/.spacehammer/config.fnl:22>

the line numbers, interestingly, in the traceback are not correct at all: in config.fnl those lines are comments.有趣的是,回溯中的行号根本不正确:在 config.fnl 中,这些行是注释。 But I assume that it has to be the new function, because that's all I changed, and the error was triggered on attempting to run it.但我认为它必须是新的 function,因为这就是我所做的全部更改,并且在尝试运行它时触发了错误。 But the hammerspoon rotate method that I'm attempting to call definitely takes a number, not whatever "userdata" is...但是我试图调用的hammerspoon rotate方法肯定需要一个数字,而不是任何“用户数据”......

Oooof.哎呀。 Ok, this is actually my misreading of Fennel docs on method calls .好的,这实际上是我对Fennel docs on method calls的误读。 I'll leave the question up just because it seems like an easy gotcha for Google purposes.我会留下这个问题,只是因为对于 Google 而言,这似乎是一个简单的问题。 But apparently method access with periods required you to pass the name of the table in which the method lives a second time, whereas this is not required with method access via colons.但显然,带有句点的方法访问要求您第二次传递该方法所在的表的名称,而通过冒号进行的方法访问则不需要。 So I got it working by changing the last expression to (monitor:rotate 90) --- and I guess (monitor.rotate monitor 90) would have worked as well, judging by the third example in the above-linked section of the docs?因此,我通过将最后一个表达式更改为(monitor:rotate 90)来使其工作 --- 而且我猜(monitor.rotate monitor 90)也可以工作,从文档的上述链接部分中的第三个示例来看?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM