[英]Can I declare mutually recursive functions with attributes in F#?
所以; 這段代碼編譯得很好(雖然我不建議運行它...):
let rec firstFunc () =
secondFunc ()
and secondFunc () =
firstFunc ()
但! 此代碼不會:
let rec firstFunc () =
secondFunc ()
[<CompiledName "SecondFunc">]
and secondFunc () =
firstFunc ()
有辦法解決這個限制嗎?
你可以在and
之后添加屬性,它似乎編譯得很好。
let rec firstFunc () =
secondFunc ()
and [<CompiledName "SecondFunc">] secondFunc () =
firstFunc ()
如果您只需要一個函數的CompiledName屬性,而不是兩個函數,您可以這樣做:
[<CompiledName "SecondFunc">]
let rec secondFunc () =
firstFunc ()
and firstFunc () =
secondFunc ()
但如果你需要它,我還沒有找到解決方案。
要跟進Pierre Irrmann的回答,如果您希望在單獨的行中查看屬性,還可以執行以下操作:
let rec firstFunc () =
secondFunc ()
and [<CompiledName("SecondFunction")>]
secondFunc () =
firstFunc ()
甚至:
let rec firstFunc () =
secondFunc ()
and
[<CompiledName("SecondFunction")>]
secondFunc () =
firstFunc ()
唯一的要求是secondFunc ()
聲明及其屬性必須縮進至少一個空格。 所以即使這樣也可行:
let rec firstFunc () =
secondFunc ()
and
[<CompiledName("SecondFunction")>]
secondFunc () =
firstFunc ()
不過,我並不特別推薦最后一個選項。 我已經測試了它並且它可以工作,但它看起來很難看。 最好縮進整個縮進級別(四個或兩個空格,無論你使用什么),而不是“可愛”並在這種情況下縮進一個空格。
到目前為止,我設法得到的最好的是:
let rec firstFunc () =
fakeSecondFunc ()
and fakeSecondFunc () =
firstFunc ()
[<CompiledName "SecondFunc">]
let secondFunc () =
fakeSecondFunc ()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.