2

I'm learning how to use J via online reading and doing some old Java assignments over again using this language, and would like to know how to make a verb that doesn't take any operands, or return any result. The reason being: I would like to allow myself the ability to type in a verb, let's call it go that would run a sequence of code on it's own and save whatever data it would produce in its execution, but would display nothing at all. The overall goal of this would be to eventually be able to reproduce my vending machine class and interface which requires at least the void returnChange() method.

4

2 回答 2

4

调用 J 动词总是至少有一个正确的参数。向完全忽略正确论点的一元动词发送任何内容(例如0or '')。

函数总是返回一些东西,但是使用i.0''最小化返回的数据。

go =: 3 : 0
    NB. do stuff
    i. 0
)

go ''
于 2010-07-20T17:22:20.660 回答
1

MPelletier 是正确的,J 动词总是需要正确的参数来产生结果,并且在执行时它们必然产生结果。副词和连词的情况类似。J 中没有什么类似于“返回 void”的方法。

MPelletier 提供的示例使用关键字“return”。在这种情况下,关键字无效。此处提供了该程序的修改版本:

go =: 3 : 0
  NB. do stuff
  i. 0 0
)

该程序与 MPelletier 发布的程序之间的一个明显区别是,如果在控制台中执行,它不会在下一个提示之前产生空行。(任何在其形状位置 _2 处为零的结果都会产生这种效果。)

于 2010-08-26T17:43:29.023 回答