2017-10-16 45 views
0

AppleScript錯誤消息似乎暗示提供給多參數處理程序的參數集在內部表示爲列表。AppleScript:我們可以獲得被調用處理程序的參數向量作爲列表嗎?

短重構的所有處理採取列表或記錄論點,我們可以:

  1. 瞭解,在運行時,由給定處理器對象預期參數的數量?
  2. 在運行時,獲取提供給多參數處理程序的某種argv參數列表(或者可能是處理程序中定義的名稱空間的記錄表示)?
+0

1)否 - 2)無 – vadian

+0

(1)當然不是,技術上是不可能的 - 我們可以在運行時肯定刮的錯誤消息字符串 - 但一些清潔劑將是一件好事。 – houthakker

回答

0

我發現沒有路由到(2.)(在運行時獲取參數列表),但是它的價值是什麼,這裏有一個函數(如果處理程序的arity是0),這允許我們在運行時學習一個處理程序的arity。

-- RUN-TIME TEST: 
-- HOW MANY ARGUMENTS DOES A HANDLER EXPECT ? -------------------------------- 

-- arityAndMaybeValue :: Handler -> {arity: Int, nothing: Bool, just: Any} 
on arityAndMaybeValue(h) 
    try 
     set v to mReturn(h)'s |λ|() 
     {arity:0, nothing:false, just:v} 
    on error errMsg 
     {arity:length of splitOn(",", errMsg), nothing:true} 
    end try 
end arityAndMaybeValue 

-- TEST ---------------------------------------------------------------------- 
on run 
    -- Arities for zipWith and helloWorld handlers 

    {arityAndMaybeValue(zipWith), arityAndMaybeValue(helloWorld)} 

    -- If the arity was 0, then a return value is is given 
    -- in the 'just' key of the record returned. 
    -- otherwise, the 'nothing' key contains the value `true` 

    --> {{arity:3, nothing:true}, {arity:0, nothing:false, just:"hello world!"}} 
end run 

-- SAMPLE HANDLERS TO TEST FOR ARITY AT RUN-TIME ----------------------------- 

-- helloWorld ::() -> String 
on helloWorld() 
    "hello world!" 
end helloWorld 

-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] 
on zipWith(f, xs, ys) 
    set lng to min(length of xs, length of ys) 
    set lst to {} 
    tell mReturn(f) 
     repeat with i from 1 to lng 
      set end of lst to |λ|(item i of xs, item i of ys) 
     end repeat 
     return lst 
    end tell 
end zipWith 


-- GENERIC FUNCTIONS --------------------------------------------------------- 

-- splitOn :: Text -> Text -> [Text] 
on splitOn(strDelim, strMain) 
    set {dlm, my text item delimiters} to {my text item delimiters, strDelim} 
    set xs to text items of strMain 
    set my text item delimiters to dlm 
    return xs 
end splitOn 

-- Lift 2nd class handler function into 1st class script wrapper 
-- mReturn :: Handler -> Script 
on mReturn(f) 
    if class of f is script then 
     f 
    else 
     script 
      property |λ| : f 
     end script 
    end if 
end mReturn 

相關問題