Type detection: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|REXX}}: added the REXX language.)
Line 98: Line 98:


* [http://php.net/manual/en/function.is-array.php is_array()]
* [http://php.net/manual/en/function.is-array.php is_array()]

== {{header|Python}} ==
Built-in function type()
<pre>
>>> type('foo')
<class 'str'>
>>> type(12345)
<class 'int'></pre>

Testing types
<pre>
>>> type('foo') is str
True
>>> type(123.0) is not int
True
>>> type([]) is list
True
>>> type({}) is dict
True</pre>


=={{header|REXX}}==
=={{header|REXX}}==

Revision as of 01:56, 11 July 2016

Type detection is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This draft task needs a purpose, a description and some way to tell whether examples satisfy or do not satisfy it.

The task is to show a function/procedure that processes a block of text by printing it. The function takes one parameter (ideally) that describes the text. Demonstrate by calling the function twice, each time passing in a different type.

This can be done with pattern matching, multi-methods, dynamic type detection, structs with a tag, etc. The objective is write one [eg library] function that processes text from multiple sources (such as a string/char *, socket, file, etc). If not practical, show how the caller would coerce a type that can be passed to the library function.

J

Presumably this satisfies the task requirements...

<lang J> echo 'one' one

  echo 1

1</lang>

JavaScript

[1]

console.log(typeof('foo')); // Returns string
console.log(typeof(12345)); // Returns number

OASYS Assembler

<lang oasys_oaa>

The following method checks if a global variable or property is an
object type. Does not work with locals and arguments.

[&OBJ#,^]

 ,^<,^<<    ; Remember old value
 ,^<*>      ; Create new object
 ,^<<DES    ; Destroy the object
 ,^<<EX     ; Check if variable has been cleared
 />1RF      ; It is clear
 :>0RF      ; It is not clear

</lang>


Perl 6

Perl 6 is a dynamic language that has gradual, duck typing. It provides introspection methods through its comprehensive MOP (Meta Object Protocol) making it easy to do type detection, subroutine signatures and multi-dispatch. Perl 6 types have two general flavors: content types and container types. Different container types have varying restrictions on what sort of content they can contain and in return provide specialized methods to operate on those contents. Content types give the compiler hints on how to best handle the information, what storage requirements it may have, what operators will work with it, etc.

This is really a very broad and kind of hand-wavey overview of Perl 6 types. For much more indepth coverage see Perl 6 Synopsis S02: Bits and Pieces: Built-In Data Types

<lang perl6>sub type ($t) { say $t.perl, "\tis type: ", $t.WHAT }

  1. some content types

.&type for 1, 2.0, 3e0, 4i, π, Inf, NaN, 'String';

  1. some primitive container types

.&type for $, [ ], @, { }, %, (5 .. 7), (8 ... 10), /0/, {;}, sub {}, ( );

  1. undefined things

.&type for Any, Nil;

  1. user defined types

class my-type { };

my my-type $object;

$object.&type;</lang>

Output:
1	is type: (Int)
2.0	is type: (Rat)
3e0	is type: (Num)
<0+4i>	is type: (Complex)
3.14159265358979e0	is type: (Num)
Inf	is type: (Num)
NaN	is type: (Num)
"String"	is type: (Str)
Any	is type: (Any)
$[]	is type: (Array)
$[]	is type: (Array)
{}	is type: (Hash)
{}	is type: (Hash)
5..7	is type: (Range)
(8, 9, 10).Seq	is type: (Seq)
/0/	is type: (Regex)
-> ;; $_? is raw { #`(Block|61385680) ... }	is type: (Block)
sub () { #`(Sub|62948936) ... }	is type: (Sub)
$()	is type: (List)
Any	is type: (Any)
Nil	is type: Nil
my-type	is type: (my-type)

PHP

[2]

echo gettype('foo'); // Returns string
echo gettype(12345); // Returns integer

Specific tester functions

Python

Built-in function type()

>>> type('foo')
<class 'str'>
>>> type(12345)
<class 'int'>

Testing types

>>> type('foo') is str
True
>>> type(123.0) is not int
True
>>> type([]) is list
True
>>> type({}) is dict
True

REXX

These are some of the tests that can be performed on REXX variables (values) to determine which   type   they are. <lang rexx>/*REXX program displays what "type" a variable is (based on the variable's value). */ signal on noValue /*trap for undefined REXX variables. */ y=1938  ; call showType y /* ╔═════════════════════════════════╗ */ y=77.1  ; call showType y /* ║ All REXX variables are stored as║ */ y=  ; call showType y /* ║ character strings, even numbers.║ */ y=' '  ; call showType y /* ║ If a variable string is numeric,║ */ y='abc'  ; call showType y /* ║ all comparisons (IF statements) ║ */ y='ABC'  ; call showType y /* ║ that are made with numbers are ║ */ y='aBc'  ; call showType y /* ║ compared numerically. If not ║ */ y='1515'x  ; call showType y /* ║ numeric, the string is compared ║ */ y='10 11'x  ; call showType y /* ║ character by character after ║ */ y='00 0001'b ; call showType y /* ║ leading blanks are removed. ║ */ y='1'b  ; call showType y /* ╚═════════════════════════════════╝ */

                         call showType yyy      /*note:  the variable YYY is undefined.*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ noValue: say ' REXX variable ' condition('D') " is undefined."; exit /*──────────────────────────────────────────────────────────────────────────────────────*/ showType: procedure; parse arg x; arg xU /*obtain true value and also uppercase.*/

          @='      value is';          say @ x
                                       say @ 'of length'  length(x)
          if  x==               then say @ "null."
          if  x\== & x=       then say @ "all blank."
          if  datatype(x, 'N')    then say @ "numeric (decimal)."
                                  else say @ "a character string (not numeric)."
          if  datatype(x, 'W')    then say @ "an integer (a whole number)."
          if  datatype(x, 'N') &,
             \datatype(x, 'W')    then say @ "not an integer."
          if  datatype(x, 'N') &,
              pos('E', xu)\==0    then say @ "a number in exponential format."
          if  datatype(x, 'A')    then say @ "an alphanumeric string."
          if  datatype(x, 'U')    then say @ "all uppercase (Latin) letters."
          if  datatype(x, 'L')    then say @ "all lowercase (Latin) letters."
          if \datatype(x, 'L') &,
             \datatype(x, 'U') &,
              datatype(x, 'M')    then say @ "of mixed case (Latin) letters."
          if  datatype(x, 'B')    then say @ "binary."
          if  datatype(x, 'X')    then say @ "hexadecimal."
          if  datatype(x, 'S')    then say @ "a REXX symbol."
          say copies('b1'x,50)                  /*fence character used for a separator.*/
          return</lang>

output   when using the internal data:

      value is 1938
      value is of length 4
      value is numeric (decimal).
      value is an integer (a whole number).
      value is an alphanumeric string.
      value is hexadecimal.
      value is a REXX symbol.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is 77.1
      value is of length 4
      value is numeric (decimal).
      value is not an integer.
      value is a REXX symbol.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is
      value is of length 0
      value is null.
      value is a character string (not numeric).
      value is binary.
      value is hexadecimal.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is
      value is of length 3
      value is all blank.
      value is a character string (not numeric).
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is abc
      value is of length 3
      value is a character string (not numeric).
      value is an alphanumeric string.
      value is all lowercase (Latin) letters.
      value is hexadecimal.
      value is a REXX symbol.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is ABC
      value is of length 3
      value is a character string (not numeric).
      value is an alphanumeric string.
      value is all uppercase (Latin) letters.
      value is hexadecimal.
      value is a REXX symbol.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is aBc
      value is of length 3
      value is a character string (not numeric).
      value is an alphanumeric string.
      value is of mixed case (Latin) letters.
      value is hexadecimal.
      value is a REXX symbol.
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is §§
      value is of length 2
      value is a character string (not numeric).
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is ►◄
      value is of length 2
      value is a character string (not numeric).
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is ☺
      value is of length 1
      value is a character string (not numeric).
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      value is ☺
      value is of length 1
      value is a character string (not numeric).
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
      REXX variable  YYY  is undefined.

zkl

<lang zkl>fcn processText(data_or_fileName){ // unknown

  if (data_or_fileName.isType(String)) // == .isType("")
     data_or_fileName=File(data_or_fileName,"rb").read(); //-->Data
  text:=data_or_fileName.text;  //-->String
  doTheActualTextProcessing(text);

} fcn doTheActualTextProcessing(text){ println(text) }</lang> If an int is passed in, (123).text --> "123", other objects might throw an exception.

How to use: <lang zkl>processText("foo.txt"); processText(Data(Void,"This is some text")); // fake up a class that holds a string: cs:=class{ var text }; cs.text="this is more text"; processText(cs);</lang>

Output:
this is foo.txt

This is some text
this is more text