Rebol 编程/语言特性/类型
一般而言,数据类型是值的集合以及可以对值执行的一些操作。了解值的类型的主要目的是了解我们可以对该值执行的操作。
以下代码在 Rebol 中是合法的
>> x: 1 == 1 >> x: "2" == "2"
此属性可以通过以下说法来描述:Rebol 是一种动态类型的语言。一些 Rebol 用户将其描述为:数据类型与 Rebol 值相关联,而不是与变量相关联。
以下代码在 Rebol 中是不合法的
>> x: 1 + "2" ** Script Error: Cannot use add on string! value ** Near: x: 1 + "2"
这被称为类型安全,是由以下事实造成的:在表达式求值时,整数 1 或字符串 "2" 不会自动转换为其他类型。
Rebol 有 45 种基本数据类型,用于处理现实世界的概念,例如 URL、电子邮件地址、电话号码、货币金额。通过对每个值进行类型标记,Rebol 限制了程序可以对值执行的操作。
丰富的类型集促进了 Rebol 脚本的可读性,以及块解析(如其他地方所述)。
使用 money! 数据类型,可以轻松地进行一些基本的会计工作
>> AU$1.00 + AU$0.50 == AU$1.50
Rebol 甚至可以确保我们使用相同的货币
>> AU$1.00 + US$0.02 ** Script Error: AU$1.00 not same denomination as US$0.02 ** Near: AU$1.00 + US$0.02
日期计算和以下代码一样简单
>> now - 1-Jan-2005 == 251
其中now恰好是今天的日期,即 2005 年 9 月 9 日。
数据类型也是我们指定函数只能接受特定值的依据。一个期望处理文件的函数不希望看到 email! 类型的输入。
print-file: func [
file [file!]
][
print read file
]
>> print-file support@wikibooks.org ** Script Error: print-file expected file argument of type: file ** Near: print-file support@wikibooks.org
数据类型安全意味着 Rebol 保护我们免于混合不同类型,这可能会导致意外错误。但是,我们可以指定函数是多态的,即能够接受多个基本数据类型作为其参数。
type? 是多态函数的一个示例,它将接受任何数据类型的参数,并返回其数据类型作为结果。
>> type? http://www.rebol.com == url!
Rebol 数据类型可以分为两组:虚拟数据类型和基本数据类型。虚拟数据类型是作为基本数据类型组的共同 "祖先" 的数据类型。
| 类型名称 | 组 |
|---|---|
| any-block! | block! path! set-path! lit-path! paren! |
| any-function! | function! native! action! op! routine! |
| any-string! | string! file! email! url! tag! binary! issue! |
| any-type! | 所有类型 |
| any-word! | word! set-word! get-word! refinement! |
| number! | integer! decimal! |
| series! | any-block! any-string! list! |
| 类型名称 | 示例 |
|---|---|
| action! | [n 1] |
| binary! | #{7265626F6C} 64#{cmVib2w=} |
| bitset! | #[bitset! #{00}] |
| block! | [r e b o l] |
| char! | #"r"[n 2] |
| datatype! | #[datatype! none!] |
| date! | 24-may-2005 30/Nov/2005 2008-11-30[n 3] 2008/Nov/30 30-Nov-2005/10:30-7:00 |
| decimal! | 1e-15 1.5[n 4] 1,5 -1e6 |
| email! | mail@rebol.com[n 5] |
| error! | |
| event! | |
| file! | %rebol.exe %/media/cdrom/file %another%20file %"file too" |
| function! | |
| get-word! | :rebol |
| hash! | |
| image! | |
| integer! | 17 |
| issue! | #158 #rebol |
| library! | [n 1] |
| list! | |
| lit-path! | 'rebol/words/type? |
| lit-word! | 'rebol |
| logic! | #[true] #[false] |
| money! | $2.50 US$4.75 DKK$37.75 -EUR$10 |
| native! | [n 1] |
| none! | #[none] |
| object! | |
| op! | [n 1] |
| pair! | 100x200 -1x-2 |
| paren! | () |
| path! | rebol/words/print |
| port! | |
| refinement! | /case |
| routine! | [n 1] |
| set-path! | rebol/words/x |
| set-word! | rebol |
| string! | "Rebol"[n 2] |
| struct! | #[struct! [] []] |
| symbol! | |
| tag! | <html> |
| time! | 12:45:22 |
| tuple! | 120.80.45 40.35.12.50 |
| unset! | #[unset!] |
| url! | http://www.rebol.com ftp://www.rebol.com[n 6] |
| word! | rebol word? a+b word! |
- ↑ a b c d e 该数据类型没有字面量表示
- ↑ a b Rebol 支持插入符号表示法
- ↑ Rebol 支持 ISO 8601
- ↑ Rebol 十进制数是 64 位 IEEE-754 二进制浮点数
- ↑ Rebol 支持RFC 5322
- ↑ Rebol 支持RFC 3986
如上所述,Rebol 中没有隐式类型转换,即当我们想要将一个值转换为特定数据类型时,必须显式进行转换,并且该转换必须是可行的。以下是如何将字符串转换为整数
>> to integer! "145" == 145
此规则似乎与以下工作代码相矛盾:
>> 1 + 2.0 == 3.0
该代码必须将整数 1 转换为小数,然后才能将其添加到小数 2.0 中。但是,Rebol 解释器在到达 + 运算符之前不会转换参数。是 + 运算符能够接受和转换该类型的组合(整数和小数),然后执行其工作并返回结果。
一般来说,当我们进行类型转换时,to 函数是第一个需要考虑的函数。to 函数的type 参数可以是一个示例值,而不是数据类型。
>> to 1 "145" == 145