2008年3月15日

為各種資料型別分類


分門別類,是一種整理知識的表現。對REBOL程式員來說,為各種資料型別(datatype)分類相當重要,因為相同種類的資料型別,往往會有相當類似的操作方式,甚至可以使用完全一樣的函數來進行操作。如果你知道每個型別屬於哪一類,那麼在寫REBOL程式時,很容易就可以發揮聯想,猜測有哪些函數可以用。

我們可以利用兩種方式,看看REBOL內部是如何為這些資料型別分類的:

  1. typeset!
  2. spec-of

【typeset】

typeset!(資料集)本身就是一種資料型別,用來將某些資料型別結合起來,成為一種「類似」資料型別的新組合。你可以利用下面的方式,看看REBOL預先定義哪些資料集:

>> ? typeset!

你會看到上面列出8種資料集,分別是any-block!、any-function!、any-string!、any-type!、any-word!、number!、scalar!、series!。你可以再次利用?函數,來看它們各自的詳細說明,例如:

>> ? number!
NUMBER! is a datatype of value: make typeset! [integer! decimal! [percent!]

意思是,NUMBER!是由integer!、decimal!、percent!這三種型別所組成的資料集。

下面就這八種資料集做整理:

Number! 有三種

  • Integer! (整數)
  • Decimal! (小數)
  • Percent! (百分比)

Scalar!

  • Integer! Decimal! Percent! (同Number!)
  • Money! (精準數)
  • Char! (字元)
  • Pair! (值對)
  • Tuple! (值組)
  • Time! (時間)
  • (註:我認為應該也要包含Date!)

Any-Word! (泛單字)

  • Word! (單字)
  • Set-word! (設字)
  • Get-word! (取字)
  • Lit-word! (原字)
  • Refinement! (修飾)

Any-string! (泛字串)

  • String! (字串)
  • Binary! (二元)
  • File! (檔案)
  • Email! (電郵)
  • Url! (網址)
  • Tag! (標籤)
  • Issue! (期號)
  • (註:我認為應該也要包含Bitset!)

Any-block! (泛區塊)

  • Block! (區塊)
  • Paren! (括號區塊)
  • Map! (對應表)
  • Path! (路徑)
  • Set-path! (設徑)
  • Get-path! (取徑)
  • Lit-path! (原徑)

Series! (系列)

  • 包含「泛字串」與「泛區塊」的全部類別
  • Bitset! (位元集)
  • Vector! (向量)
  • Image! (影像)

Any-function! (泛函數)

  • Native! (原生函數)
  • Action! (動作函數)
  • Routine! (常式)
  • Rebcode! (彙編碼)
  • Op! (運算子)
  • Closure!
  • Function! (普通函數)

Any-type!

  • 全部(50+個)資料型別

【spec-of】

除了上述的方式,利用spec-of,搭配probe,也可以看出各種類別的分類,而且更全面。 作法:

>> probe spec-of block!

下面列出用這種方式觀察出來的分類。

Block

  • Block!
  • Get-path!
  • Lit-path!
  • Map!
  • Paren!
  • Path!
  • Rebcode! (註:這有點怪)
  • Set-path!
  • Struct! (註:這有點特別)

Function

  • Action!
  • Closure!
  • Function!
  • Native!
  • Op!
  • Routine!

Internal (內部使用)

  • End!
  • Frame!
  • Handle!
  • Library!
  • Unset!

Object

  • Error!
  • Module!
  • Object!
  • Port!
  • Task!
  • Utype!

Opt-Object

  • Event!
  • Gob!
  • Typeset!

Scalar

  • Char!
  • Date!
  • Decimal!
  • Integer!
  • Logic!
  • Money!
  • None!
  • Pair!
  • Path!
  • Percent!
  • Time!
  • Tuple!

String

  • Binary!
  • Bitset!
  • Email!
  • File!
  • Issue!
  • String!
  • Tag!
  • Url!

Symbol

  • Datatype!

Vector

  • Image!
  • Vector!

Word

  • Get-word!
  • Lit-word!
  • Refinement!
  • Set-word!
  • Word!

沒有留言: