Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

DATATYPE-NAME?

datatype-name?: func [val [string!]] [datatype? get/any load val]

CAST

cast: func [value type] [to type value]

CAN-CAST?

can-cast?: func [value type] [not error? try [to type value]]]

CAST function - Convert a value referenced by a word to a new datatype. Dialected

    cast: func [ ; convert coerce
        "Convert a value referenced by a word to a new datatype."
        input [block!] "A word, and the target datatype."
        /local words type val
    ] [
        parse input [
            some [
                copy words to 'to skip set type any-type! (
                    ;while [pos: find words 'and] [remove pos]
                    remove-each word words [word = 'and]
                    foreach word words [
                        val: get/any word
                        set/any word to either word? type [get type] [type] val
                    ]
                )
            ]
        ]
    ]
    comment {
        a: "A-0001"
        b: #B002
        c: 300
        d: <H1>
        e: 'test
        cast [a to issue!]      print mold :a
        cast [b to tag!]        print mold :b
        cast [c to decimal!]    print mold :c
        cast [a b c to string!] print remold [:a :b :c]
        cast [a b and c to issue!] print remold [:a :b :c]
        cast [
            a b and c to tag!
            and
            d and e to issue!
        ] print remold [:a :b :c :d :e]
        cast [a b c to <x>  d e to "x"] print remold [:a :b :c :d :e]
    } 

Find datatype words

datatypes: has [types attrs value word] [
	word: 'datatype!
	types: copy []
	attrs: second system/words
	foreach item first system/words [
		if all [
			not unset? first attrs
			any [
				all [string? :word find value word]
				all [
					not string? :word
					datatype? get :word
					(get :word) = type? first attrs
				]
			]
		] [
			append types item
		]
		attrs: next attrs
	]
	sort types
]
print mold datatypes
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS