Difference between revisions of "Isa"

From MiniScript Wiki
Jump to navigation Jump to search
(Added "see also")
(→‎isa (operator): Other types as RHS)
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:
  
 
== isa (operator) ==
 
== isa (operator) ==
<c>isa</c> is a [[:Category:Keywords|keyword]] binary [[operators|operator]] which tests the left-hand operator for membership in the type defined by the right-hand side.  That right-hand operator could be a user-defined map (to which the left-hand side might belong via the <c>__isa</c> chain), or it could be one of the built-in types [[number]], [[string]], [[list]], [[map]], or [[funcRef]].
+
<c>isa</c> is a [[:Category:Keywords|keyword]] binary [[operators|operator]] which tests the left-hand operator for membership in the type defined by the right-hand side.  That right-hand operand could be a user-defined map (to which the left-hand side might belong via the <c>__isa</c> chain), or it could be one of the [[:Category:Data Types|built-in types]]: [[number]], [[string]], [[list]], [[map]], or [[funcRef]].
 +
 
 +
=== Examples for built-in types ===
 +
 
 +
<ms>"Hello" isa string    // returns 1
 +
"Hello" isa number    // returns 0
 +
123 isa number        // returns 1
 +
"123" isa number      // returns 0
 +
[1,2,3] isa list      // returns 1
 +
{"uno":1,"dos":2} isa map    // returns 1
 +
@print isa funcRef    // returns 1
 +
</ms>
 +
 
 +
=== Examples for user-defined types ===
 +
 
 +
<ms>
 +
MyDefinedType = {}
 +
MyOtherDefinedType = {}
 +
 
 +
obj = new MyDefinedType
 +
 
 +
obj isa MyDefinedType        // returns 1 - direct type
 +
obj isa map                  // returns 1 - related by inheritance
 +
 
 +
obj isa MyOtherDefinedType  // returns 0 - because unrelated
 +
</ms>
 +
 
 +
=== Other types as RHS ===
 +
 
 +
As of MiniScript version <c>1.6.1</c>, it's possible to use ''any'' value as a right hand side of an <c>isa</c> operator. When such values are not types, the operator returns [[false]], except when it's a [[null]] in which case <c>isa</c> checks for equality with [[null]] (this behavior might currently be affected by [https://github.com/JoeStrout/miniscript/issues/84 a bug]).
  
 
== __isa  (map entry) ==
 
== __isa  (map entry) ==

Latest revision as of 08:43, 17 November 2023

Because this wiki software does not allow topic names to begin with an underscore, this page contains two different topics.

isa (operator)

isa is a keyword binary operator which tests the left-hand operator for membership in the type defined by the right-hand side. That right-hand operand could be a user-defined map (to which the left-hand side might belong via the __isa chain), or it could be one of the built-in types: number, string, list, map, or funcRef.

Examples for built-in types

"Hello" isa string    // returns 1
"Hello" isa number    // returns 0
123 isa number        // returns 1
"123" isa number      // returns 0 
[1,2,3] isa list      // returns 1
{"uno":1,"dos":2} isa map    // returns 1
@print isa funcRef    // returns 1

Examples for user-defined types

MyDefinedType = {}
MyOtherDefinedType = {}

obj = new MyDefinedType

obj isa MyDefinedType        // returns 1 - direct type
obj isa map                  // returns 1 - related by inheritance

obj isa MyOtherDefinedType   // returns 0 - because unrelated

Other types as RHS

As of MiniScript version 1.6.1, it's possible to use any value as a right hand side of an isa operator. When such values are not types, the operator returns false, except when it's a null in which case isa checks for equality with null (this behavior might currently be affected by a bug).

__isa (map entry)

__isa is a special entry in a map that refers to the map's base class. It is part of MiniScript's support for object-oriented programming.

See also


This article is a stub. You can help the MiniScript Wiki by expanding it.