2017-10-16 46 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 

相关问题