It turns out "is" is just a normal overridable operator. Who knew?

This commit is contained in:
Bob Nystrom
2015-11-07 13:00:24 -08:00
parent 931d9ca4d3
commit 7ae9888c4f
5 changed files with 30 additions and 4 deletions

View File

@ -3,7 +3,7 @@
## Static Methods ## Static Methods
## **same**(obj1, obj2) ### **same**(obj1, obj2)
Returns `true` if *obj1* and *obj2* are the same. For [value Returns `true` if *obj1* and *obj2* are the same. For [value
types](../values.html), this returns `true` if the objects have equivalent types](../values.html), this returns `true` if the objects have equivalent
@ -20,7 +20,9 @@ even on user-defined classes.
### **!** operator ### **!** operator
Returns `false`, since most objects are considered [true](control-flow.html#truth). Returns `false`, since most objects are considered [true][].
[true]: control-flow.html#truth
### **==**(other) and **!=**(other) operators ### **==**(other) and **!=**(other) operators
@ -28,10 +30,25 @@ Compares two objects using built-in equality. This compares [value
types](../values.html) by value, and all other objects are compared by types](../values.html) by value, and all other objects are compared by
identity—two objects are equal only if they are the exact same object. identity—two objects are equal only if they are the exact same object.
### **is**(class) operator
Returns `true` if this object's class or one of its superclasses is `class`.
:::wren
System.print(123 is Num) //> true
System.print("s" is Num) //> false
System.print(null is String) //> false
System.print([] is List) //> true
System.print([] is Sequence) //> true
It is a runtime error if `class` is not a [Class][].
### **toString** ### **toString**
A default string representation of the object. A default string representation of the object.
### **type** ### **type**
The [Class](#class-class) of the object. The [Class][] of the object.
[class]: class.html

View File

@ -123,7 +123,7 @@ We also have a slew of infix operators—they have operands on both sides.
They are: They are:
:::wren :::wren
== != < > <= >= .. ... | & + - * / % == != < > <= >= .. ... | & + - * / % is
Like prefix operators, they are all funny ways of writing method calls. The left Like prefix operators, they are all funny ways of writing method calls. The left
operand is the receiver, and the right operand gets passed to it. So `a + b` is operand is the receiver, and the right operand gets passed to it. So `a + b` is
@ -138,6 +138,13 @@ objects, but they are method calls like other operators.
[range]: values.html#ranges [range]: values.html#ranges
The `is` keyword is a "type test" operator. The base [Object][] class implements
it to tell if an object is an instance of a given class. You'll rarely need to,
but you can override `is` in your own classes. That can be useful for things
like mocks or proxies where you want an object to masquerade as a certain class.
[object]: core/object.html
## Subscripts ## Subscripts
Another familiar syntax from math class is *subscripting* using square brackets Another familiar syntax from math class is *subscripting* using square brackets

View File

@ -14,6 +14,7 @@ class Foo {
!=(other) { "infix != " + other } !=(other) { "infix != " + other }
&(other) { "infix & " + other } &(other) { "infix & " + other }
|(other) { "infix | " + other } |(other) { "infix | " + other }
is(other) { "infix is " + other }
! { "prefix !" } ! { "prefix !" }
~ { "prefix ~" } ~ { "prefix ~" }
@ -37,3 +38,4 @@ System.print(foo | "a") // expect: infix | a
System.print(!foo) // expect: prefix ! System.print(!foo) // expect: prefix !
System.print(~foo) // expect: prefix ~ System.print(~foo) // expect: prefix ~
System.print(-foo) // expect: prefix - System.print(-foo) // expect: prefix -
System.print(foo is "a") // expect: infix is a