DEV Community

Discussion on: Design By Contract, Immutability, Side Effects and Gulag

Collapse
 
jamieghassibi profile image
JamieGhassibi • Edited

This is great, but code becomes more cluttered and violates separation of concerns. Is there anyway to move pre- and post-conditions to the end of a file where all tests can be collected, then somehow inject the code into the functions, perhaps via macro? Think of it like decorators. For example:

import contra

func fn1(x: int): int = x - 1
func fn2(x: int): int = x - 1

# Bottom of file
contra fn1: # Like block-label syntax
    preconditions x > 1, x < 20
    postconditions result > 0

contra fn2:
    preconditions x > 1, x < 20
    postconditions result > 0

And this would run exactly as if you had written the following:

import contra

func fn1(x: int): int =
    preconditions x > 1, x < 20
    postconditions result > 0
    x - 1

func fn2(x: int): int =
    preconditions x > 1, x < 20
    postconditions result > 0
    x - 1

I am new to Nim, so I don't know if the above syntax or similar is possible via a module import.

Cheers~