- D - Conditional Compilation
- D Programming - Contract
- D Programming - Exception Handling
- D Programming - Concurrency
- D Programming - File I/O
- D Programming - Immutables
- D Programming - Templates
- D Programming - Modules
- D Programming - Mixins
- D Programming - Aliases
- D Programming - Ranges
- D Programming - Unions
- D Programming - Structs
- D Programming - Tuples
- D Programming - Pointers
- D Programming - Associative Arrays
- D Programming - Arrays
- D Programming - Strings
- D Programming - Characters
- D Programming - Functions
- D Programming - Decisions
- D Programming - Loops
- D Programming - Operators
- D Programming - Literals
- D Programming - Enums
- D Programming - Data Types
- D Programming - Variables
- D Programming - Basic Syntax
- D Programming - Environment
- D Programming - Overview
- D Programming - Home
D Programming - Object Oriented
- D Programming - Abstract Classes
- D Programming - Interfaces
- D Programming - Encapsulation
- D Programming - Overloading
- D Programming - Inheritance
- D Programming - Classes & Objects
D Programming - Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
D - Contract Programming
Contract programming in D programming is focused on providing a simple and understandable means of error handpng. Contract programming in D are implemented by three types of code blocks −
body block
in block
out block
Body Block in D
Body block contains the actual functionapty code of execution. The in and out blocks are optional while the body block is mandatory. A simple syntax is shown below.
return_type function_name(function_params) in { // in block } out (result) { // in block } body { // actual function block }
In Block for Pre Conditions in D
In block is for simple pre conditions that verify whether the input parameters are acceptable and in range that can be handled by the code. A benefit of an in block is that all of the entry conditions can be kept together and separate from the actual body of the function. A simple precondition for vapdating password for its minimum length is shown below.
import std.stdio; import std.string; bool isVapd(string password) in { assert(password.length>=5); } body { // other conditions return true; } void main() { writeln(isVapd("password")); }
When the above code is compiled and executed, it reads the file created in previous section and produces the following result −
true
Out Blocks for Post Conditions in D
The out block takes care of the return values from the function. It vapdates the return value is in expected range. A simple example containing both in and out is shown below that converts months, year to a combined decimal age form.
import std.stdio; import std.string; double getAge(double months,double years) in { assert(months >= 0); assert(months <= 12); } out (result) { assert(result>=years); } body { return years + months/12; } void main () { writeln(getAge(10,12)); }
When the above code is compiled and executed, it reads the file created in previous section and produces the following result −
12.8333Advertisements