From 7ae9888c4f71d073e7e4f7428bd45463ac7020b1 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sat, 7 Nov 2015 13:00:24 -0800 Subject: [PATCH] It turns out "is" is just a normal overridable operator. Who knew? --- doc/site/core/object.markdown | 23 ++++++++++++++++--- doc/site/method-calls.markdown | 9 +++++++- test/{language/is => core/object}/is.wren | 0 .../is => core/object}/nonclass_on_right.wren | 0 test/language/method/operators.wren | 2 ++ 5 files changed, 30 insertions(+), 4 deletions(-) rename test/{language/is => core/object}/is.wren (100%) rename test/{language/is => core/object}/nonclass_on_right.wren (100%) diff --git a/doc/site/core/object.markdown b/doc/site/core/object.markdown index 267124ab..c71e3a8a 100644 --- a/doc/site/core/object.markdown +++ b/doc/site/core/object.markdown @@ -3,7 +3,7 @@ ## Static Methods -## **same**(obj1, obj2) +### **same**(obj1, obj2) Returns `true` if *obj1* and *obj2* are the same. For [value types](../values.html), this returns `true` if the objects have equivalent @@ -20,7 +20,9 @@ even on user-defined classes. ### **!** 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 @@ -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 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** A default string representation of the object. ### **type** -The [Class](#class-class) of the object. +The [Class][] of the object. + +[class]: class.html diff --git a/doc/site/method-calls.markdown b/doc/site/method-calls.markdown index 85158243..3ab73dfe 100644 --- a/doc/site/method-calls.markdown +++ b/doc/site/method-calls.markdown @@ -123,7 +123,7 @@ We also have a slew of infix operators—they have operands on both sides. They are: :::wren - == != < > <= >= .. ... | & + - * / % + == != < > <= >= .. ... | & + - * / % is 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 @@ -138,6 +138,13 @@ objects, but they are method calls like other operators. [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 Another familiar syntax from math class is *subscripting* using square brackets diff --git a/test/language/is/is.wren b/test/core/object/is.wren similarity index 100% rename from test/language/is/is.wren rename to test/core/object/is.wren diff --git a/test/language/is/nonclass_on_right.wren b/test/core/object/nonclass_on_right.wren similarity index 100% rename from test/language/is/nonclass_on_right.wren rename to test/core/object/nonclass_on_right.wren diff --git a/test/language/method/operators.wren b/test/language/method/operators.wren index e36e306f..baa645aa 100644 --- a/test/language/method/operators.wren +++ b/test/language/method/operators.wren @@ -14,6 +14,7 @@ class Foo { !=(other) { "infix != " + other } &(other) { "infix & " + other } |(other) { "infix | " + other } + is(other) { "infix is " + other } ! { "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 is "a") // expect: infix is a