2011-03-08 81 views
2

在我的ml程序中,我使用嵌套結構來構造我的代碼。我正在爲這些結構定義簽名 - 但我不能真正擁有嵌套的簽名。嵌套簽名的語法?

例子:

structure Example = 
struct 
    structure Code = 
    struct 
    datatype mytype = Mycons of string 
    end 
end 

爲了這個,我想要做這樣的事情:

signature EXAMPLE = 
sig 
    signature CODE = (* or stucture Code - doesn't matter *) 
    sig 
    datatype mytype 
    end 
end 

現在,這並不正常工作;我得到語法錯誤。我的問題:

  1. 這是一個壞主意嗎?如果是這樣,爲什麼?
  2. 我該怎麼做?如何將嵌套簽名應用於嵌套結構?

回答

3

具有嵌套結構時簽名中的語法需要一些習慣。

當試圖如果簽名中的結構,你做這樣的

signature JSON = 
sig  
    type t 

    .. some signature stuff 

    structure Converter : sig  
    type json 
    type 'a t 

    ... Converter specification stuff 
    ... using type json as the parent signatures type t  
    end where type json = t  
end 

見的這一個簡單的例子,這些霍夫曼[.sml] [.sig]文件指定簽名,並有一個看看樹[.sig]文件的一個更復雜的例子。請記住,您需要在您的結構中提及您的簽名規範,否則將簽名放在第一位毫無意義。