Reputation: 2388
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?
Upvotes: 2
Views: 1112
Reputation: 159
The Swift language has a nice toggle()
instance method for the Bool type.
var myBool: Bool = true
myBool.toggle
And now myBool
is false.
Upvotes: -2
Reputation: 600
Smalltalk will let you do this.
Everything in Smalltalk-80 is available for modification from within a running program. This means that, for example, the IDE can be changed in a running system without restarting it. In some implementations, the syntax of the language or the garbage collection implementation can also be changed on the fly. Even the statement true become: false is valid in Smalltalk, although executing it is not recommended. When used judiciously, this level of flexibility allows for one of the shortest required times for new code to enter a production system.
http://en.wikipedia.org/wiki/Smalltalk
Upvotes: 5
Reputation: 6075
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:
if (42==42) {
print("This is true");
}
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. :)
Upvotes: 2
Reputation: 385800
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:
# keep the old if around
rename if ::_if
# make a new if with a negated condition
proc ::if {condition args} {
set command [list _if !($condition) {*}$args]
uplevel $command
}
Running this interactively I get:
% if true {puts true} else {puts false}
false
% if 1==1 {puts true} else {puts false}
false
(requires tcl 8.5+)
Upvotes: 1
Reputation: 4161
Python (before version 3, apparently):
True = False
print True, False
>>False False
But:
True = False
print 1==1, 1!=2, 2!=2
>>True True False
For details, see, for example:
Upvotes: 2
Reputation:
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:
#define TRUE 0
#define FALSE 1
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.
Upvotes: 1
Reputation: 12167
In the unlikely event that I wanted this, I would just re-define if, (or make IF a macro and then use it...)
Upvotes: 0
Reputation: 7412
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.
Upvotes: 1