跳轉到內容

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!
  1. a b c d e 該資料型別沒有字面量表示
  2. a b Rebol 支援插入符號表示法
  3. Rebol 支援 ISO 8601
  4. Rebol 十進位制數是 64 位 IEEE-754 二進位制浮點數
  5. Rebol 支援RFC 5322
  6. Rebol 支援RFC 3986

型別轉換

[編輯 | 編輯原始碼]

如上所述,Rebol 中沒有隱式型別轉換,即當我們想要將一個值轉換為特定資料型別時,必須顯式進行轉換,並且該轉換必須是可行的。以下是如何將字串轉換為整數

>> to integer! "145"
== 145

此規則似乎與以下工作程式碼相矛盾:

>> 1 + 2.0
== 3.0

該程式碼必須將整數 1 轉換為小數,然後才能將其新增到小數 2.0 中。但是,Rebol 直譯器在到達 + 運算子之前不會轉換引數。是 + 運算子能夠接受和轉換該型別的組合(整數和小數),然後執行其工作並返回結果。

一般來說,當我們進行型別轉換時,to 函式是第一個需要考慮的函式。to 函式的type 引數可以是一個示例值,而不是資料型別。

>> to 1 "145"
== 145
華夏公益教科書