[Jprogramming] Design-by-contract / type testing? ( Odd behavior with sdconnect)
Sherlock, Ric
R.G.Sherlock at massey.ac.nz
Mon Jun 15 08:15:27 HKT 2009
> From: Raul Miller
> Yuvaraj Athur Raghuvir wrote:
> > Is there a J idiom / verb that implements a design-by-contract or
> > input partameter type checking?
>
> Do you mean something like this?
>
> contract=:1 :'[ assert at u'
> rawtypesmatch=: -:&(3!:0)
>
> isboxed=: a:&rawtypesmatch contract
>
Extending Raul's idea:
ranksmatch=: -:&(#@$)
rankstypesmatch=: ranksmatch *. rawtypesmatch
isIntAtom=: 3&rankstypesmatch contract
isIntList=: 3 4&rankstypesmatch contract
isFloatList=: 3.1 4.2&rankstypesmatch contract
isIntAtom 9
9
isIntAtom 9 2
|assertion failure: assert
| isIntAtom 9 2
isIntAtom 9.4
|assertion failure: assert
| isIntAtom 9.4
isIntList 7
|assertion failure: assert
| isIntList 7
isIntList ,7
7
isFloatList 4 5.1
4 5.1
These are problems though:
isIntAtom 1
|assertion failure: assert
| isIntAtom 1
isFloatList 4 5.0
|assertion failure: assert
| isFloatList 4 5
Might be safer to go back to using isInteger etc. than just checking matching type.
More information about the Programming
mailing list