Browse files

More type checker usecases

  • Loading branch information...
1 parent f2a7d3f commit 73599762e2805740a42b66ade61917f40690d59a @gcao committed Aug 13, 2012
Showing with 37 additions and 5 deletions.
  1. +37 −5 examples/design_by_contract.rb
View
42 examples/design_by_contract.rb
@@ -58,15 +58,15 @@ class ContractExample < Aspector::Base
##############################
-# Use CHECKTYPE_LEVEL/ASSERTION_LEVEL=none/info/warn/fail
+# Use CHECKTYPE_LEVEL/ASSERTION_LEVEL=none/info/warn/fail default to none
# to enable/disable type check and pre/post conditions etc
# Make it possible to enable/disable for specific class/module
class A
include Kontract # include Hooks and Types
include Kontract::Hooks
include Kontract::Assert
include Kontract::HooksAndAssert # This is same as above 2 lines
- include Kontract::Types # Contains AnyType, Null, More, ArrayOf etc
+ include Kontract::Types # Contains AnyType, Anything, DuckType, More, ArrayOf etc
# define custom environment variable for enabling/disabling type checks
# Those can be accessed or changed any time, except if it is set to disabled,
@@ -80,22 +80,27 @@ class A
# the rest are ignored, and result are checked against the result type of
# that statement.
+ # AnyType vs Anything
+ # AnyType matches one value of any type
+ # Anything matches any number of values of any type
+
# On failed type check, raise Contractor::TypesDoNotMatch, or ArgumentError?
checktype Float => AnyType # Do not care return type
checktype Float # Do not care return type
checktype Float, Fixnum # Do not care return type
- checktype Return(Float)
+ checktype Anything => Float # Anything means that do not check type and number of values
checktype DuckType(:do_something) => Float
checktype DuckType(Float){|arg| arg >= 0}
# Below two are the same
checktype Float, Fixnum => Float # Most typical method signature
- checktype [Float, Null], Fixnum => Float # Allows NULL in first argument which is the default behavior
+ checktype [Float, NIL], Fixnum => Float # Allows nil in first argument which is the default behavior
- checktype [Float], Fixnum => Float # Does not allow NULL in first argument
+ checktype Float, Fixnum => Float {|f, i| puts "Matched and parsed into #{f}, #{i}" }
+ checktype [Float], Fixnum => Float # Does not allow nil in first argument
checktype Float, More # meth(first, *rest)
checktype Float, More(Float) # meth(first, *rest) first and rest are all floats
@@ -120,3 +125,30 @@ def buy price
end
end
+# Central place for defining environment variable, special types etc
+# Special types could be tested
+module Project::TypeCheck
+ include Contor
+
+ def self.included target
+ target.include Contor
+ target.checktype_env 'PROJECT_CHECKTYPE_LEVEL'
+ end
+
+ SpecialType = DuckType(:do_something)
+
+ CommonSignature = Contor.def_signature A => B
+end
+
+class Project::A
+ include Project::TypeCheck
+
+ checktype SpecialType
+ def meth input
+ end
+
+ checktype CommonSignature
+ def meth1 input
+ end
+end
+

0 comments on commit 7359976

Please sign in to comment.