Python的if __name __ ==“__ main__”用于GNU Smalltalk的习惯用法?

时间:2011-08-05 18:32:12

标签: smalltalk main gnu-smalltalk

这样的事情存在吗?

红宝石:

if __FILE__ == $0
    main
end

的Perl:

unless(caller) {
    main;
}

的Lua:

if type(package.loaded[(...)]) ~= "userdata" then
    main(arg)
else
    module(..., package.seeall)
end

1 个答案:

答案 0 :(得分:1)

充满异国情调的多线shebangs和argv技巧可以完成这项任务。

请参阅Rosetta Code

scriptedmain.st:

"exec" "gst" "-f" "$0" "$0" "$@"
"exit"

Object subclass: ScriptedMain [
    ScriptedMain class >> meaningOfLife [ ^42 ]
]

| main |

main := [
    Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].

(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
    main value.
].