Type detection
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.
JavaScript
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>
PHP
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