2017-10-16 1 views
0

AppleScript 오류 메시지는 다중 매개 변수 처리기에 제공된 인수 집합이 내부적으로 목록으로 표시된다는 것을 의미합니다. 목록 또는 기록 인수를 위해 모든 핸들러를 리팩토링의 짧은AppleScript : 호출 된 처리기의 인수 벡터를 목록으로 얻을 수 있습니까?

, 우리가 할 수

  1. 실행시, 알아보기, 주어진 핸들러 객체에 의해 예상되는 인수의 수?
  2. 런타임시, 다중 매개 변수 처리기 (또는 처리기 내에서 정의 된 nameSpace의 레코드 표현)에 제공된 argv 목록의 인수를 구하십시오.
+0

1) 아니오 - 2) 아니오 – vadian

+0

(1)인가하지 물론, 기술적으로 불가능 - 우리가 확실히 실행시 오류 메시지 문자열을 긁어 수 있습니다 -하지만 청소기 뭔가 좋은 것입니다.하지 – houthakker

답변

0

(2.) (런타임에 인수 목록을 얻음)에 대한 경로를 찾지 못했지만, 가치가있는 부분에 대해 (처리기의 구성 요소가 0), 실행 시간에 핸들러의 장점을 배울 수 있습니다.

-- 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 

관련 문제