Type detection: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Perl 6}}: copy pasto)
m (J draft)
Line 5: Line 5:


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.
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.
== {{header|J}} ==

Presumably this satisfies the task requirements...

<lang J> echo 'one'
one
echo 1
1</lang>

== JavaScript ==
== JavaScript ==



Revision as of 16:31, 23 November 2015

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

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