Skip to content

Instantly share code, notes, and snippets.

@k33g
Last active August 29, 2015 14:05
Show Gist options
  • Save k33g/1dfb5bf477b7e121422d to your computer and use it in GitHub Desktop.
Save k33g/1dfb5bf477b7e121422d to your computer and use it in GitHub Desktop.
module decorators

struct checkResult = {
  expected,
  found,
  result
}

function checkType = |t| -> |v| -> checkResult(t: getName(), v: getClass(): getName(), v oftype t)
let isInteger = checkType(Integer.class)
let isDouble = checkType(Double.class)

function check = |paramsTypeTesters...| {

  return |returnValueTypeTester| {
    return |func| {
      return |args...| {
        paramsTypeTesters: size(): times(|index| {
          let test = paramsTypeTesters: get(index)(args: get(index))
          require(
            test: result(),
            "\nParameter " + args: get(index) + " is not of required type!" +
            "\nExpected " + test: expected() +
            "\nFound " + test: found()
          )
        })
        let res = func: invokeWithArguments(args)
        let test = returnValueTypeTester(res)
        require(
          test: result(),
          "\nReturn value " + res + " is not of required type!" +
          "\nExpected " + test: expected() +
          "\nFound " + test: found()
        )
        return res
      }
    }
  }
}

@check(isInteger, isDouble) (isDouble)
function test = |a,b| {
  return a + b
}

function main = |args| {

  println(test(5,6))
  
  #Exception in thread "main" java.lang.AssertionError: 
  #Parameter 6 is not of required type!
  #Expected java.lang.Double
  #Found java.lang.Integer
  
  #println(test(5.0,6))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment