寻找可以让你改变真假的编程语言
出于好奇,我正在寻找一种动态的面向对象语言,它允许您将 true 更改为 false,反之亦然。
像这样的事情:
true = false, false = true;
这也应该影响任何条件语句,因此 42 == 42
应该返回 False。 基本上,有了这个前提,语言中的任何内容对于程序员来说都是安全的。
有这样的语言吗?
For my curiosity sake I'm looking for a dynamic object oriented language that allows you to change true to false and vice versa.
Something like this:
true = false, false = true;
This should also affect any conditional statements, therefore 42 == 42
should return False.
Basically, with this premise, nothing in the language would be safe from the programmer.
Is there any language like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Smalltalk 可以让你做到这一点。
http://en.wikipedia.org/wiki/Smalltalk
Smalltalk will let you do this.
http://en.wikipedia.org/wiki/Smalltalk
Python(显然在版本 3 之前):
但是:
有关详细信息,请参见,例如:
为什么 Python 不能按照我的预期处理 true/false 值?
TRUE 是否始终具有非零值?
Python (before version 3, apparently):
But:
For details, see, for example:
Why can’t Python handle true/false values as I expect?
Has TRUE always had a non-zero value?
您的问题有两种可能的答案。
一种可能性:你可能仍然在使用 true 和 false,但你生活在异国他乡,用不同的名字称呼它们。在这种情况下,这只是编译器/调试器环境显示的内容的问题 - 这可以通过修改编程工具来更改。 (或者,您可以从 LISP 开始,缺少布尔原语。)
或者,也许你的意思是你希望它产生后果。例如,如果您希望它不打印任何内容:
这样做的后果超出了我的想象,但如果您要重新定义条件原语(if、switch 等),您可以做到。在简单的 LISP 中,通过创建这些条件的新版本可能是最简单的。布莱恩在谈论 Tcl 时似乎讨论过这一点。
旁白:假设您创建了一种语言,其中 false 为 true,true 为 false,if 为 if not,!=s 为 ==s,等等。也许如果你颠倒足够多的东西,你就会回到原来的语言。 :)
There are two possible answers to your question.
One possibility: you might still be using true and false, but you live in a foreign land where you call them by different names. In that case, it's just a matter of what the compiler/debugger environment displays - that's changeable by modifying the programming tools. (Or, you could start with LISP, which lacks a boolean primitive.)
Or, maybe what you're saying is that you want it to have consequences. For example, if you wanted this not to print anything:
The consequences of this are beyond my ability to imagine, but if you were to redefine conditional primitives (if, switch, etc.) you could do it. It would probably be easiest in a bare-bones LISP, by creating new versions of those conditionals. Looks like Bryan discussed that when talking about Tcl.
Aside: Let's say you create a language where false is true, true is false, ifs are if nots, !=s are ==s, and so on. Perhaps if you inverted enough things you'd get back to the original language. :)
C/C++ 允许您定义宏,所以 TRUE==FALSE 和 FALSE==TRUE
您还可以将 == 运算符重载为相当于 != (反之亦然)作为单独的步骤。
C/C++ would allow you to define macros so TRUE==FALSE and FALSE==TRUE
You could also overload the == operator to be equivalent to != (and vice versa) as a separate step.
更改表示 true 和 false 的文字/常量与更改布尔表达式的含义之间存在差异。第一个很简单,可以使用程序源(例如 C:
或编译器源)轻松完成。
但是,改变布尔表达式在所有情况下的求值方式将不那么简单,但也不是太困难。不过,这将再次需要更改编译器或解释器源代码。
There is a difference between changing the literals/constants that represent true and false and changing the meaning of boolean expressions. The first is easy and can be done trivially with either the program source, for example in C:
or the compiler source.
But changing how boolean expressions are evaluated in all circumstances would be less trivial but not too difficult. It would once again require changes to the compiler or interpreter source, though.
Tcl可能会做你想做的事。或许。您无法完全重新定义 true 和 false,因为 tcl 实际上没有 true 或 false 对象或值之类的东西,但您可以重新定义作用于表达式的所有命令以返回与通常情况相反的命令。例如,您可以重新定义“if”以返回与 if 核心定义相反的内容。
这是一个简单的、不完整的示例:
交互式运行我得到:(
需要 tcl 8.5+)
Tcl might do what you want. Maybe. You can't exactly redefine true and false since tcl doesn't really have such a thing as a true or false object or value, but you can redefine all the commands that act on expressions to return the opposite of what it normally does. For example, you can redefine "if" to return the opposite of the core definition of if.
Here's a trivial, incomplete example:
Running this interactively I get:
(requires tcl 8.5+)
万一我想要这个,我只会重新定义 if,(或者使 IF 成为一个宏,然后使用它......)
In the unlikely event that I wanted this, I would just re-define if, (or make IF a macro and then use it...)
Swift 语言为 Bool 类型提供了一个很好的
toggle()
实例方法。现在 myBool 为 false。
The Swift language has a nice
toggle()
instance method for the Bool type.And now
myBool
is false.