« Home

Rhombus 用標記運算自動排序資料 [programming-0005]

最近在研究 Shrubbery Notation 怎麼實現 operator 優先序的過程中(卡關中),又發現 Rhombus 的 meta 功能中有不少有趣的東西,比如現在要介紹的功能:標記運算。要使用這個功能,必須在開頭宣告

#lang rhombus/and_meta

或是

#lang rhombus/static/and_meta

語言。接著定義

annot.macro 'AscendingIntList':
  'converting(fun (ints :: List.of(Int)) :: List: ints.sort())'

這會在我們寫下

[3, 1, 2] :: AscendingIntList

時,得到 [1, 2, 3] 而不是 [3, 1, 2],因為這個標記會自動進行排序運算!