-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Contracts #127
Added Contracts #127
Conversation
LGTM! |
🐑 |
Traversable and Guarded functions remain 👼 |
I think doing the contracts as a higher-level function would probably be better but this may be easier to understand |
We have #20 to address as well. |
@jethrolarson Did you see my alternate example? // Contract Precondition : input -> number
const preconditionsCheck = (input) => {
if (typeof input === 'number') return true
throw new Error('Contract violated: expected int -> int')
}
// Contract Postcondition : output -> number
const postconditionsCheck = (output) => {
if (typeof output === 'number') return true
throw new Error('Contract violated: expected int -> int')
}
// Contract : number -> number
const runWithContract = (fn, arg) => {
preconditionsCheck(arg)
const output = fn(arg)
postconditionsCheck(output)
return output;
}
runWithContract(addOne, 2) // 3
runWithContract(addOne, 'some string') // Contract violated: expected int -> int This uses more of a HOF approach but is much longer and might not be very concise. Any suggestions? |
TypeScript, Facebook Flow |
I imagine something like:
But again, that's getting rather complex so maybe your solution is better |
yes, to understand what a basic contract is, the merged example is sufficient. |
Contract, based on this example, is a dynamic type checker. Haskell and C have static type checkers, for instance. But I suppose you can not say that C is programming by contract, right? You need sound type system to call it programming by contract or I miss something? PS This is amazing project it's just sometimes feel like it oversimplifies some stuff and gives wrong signals. Easy and simple is not the same. For example 3bleu1brown explains complex mathematical subjects simple, but without distortion. See https://www.youtube.com/watch?v=sD0NjbwqlYw. UPD one more really tiny video on subject of misleading https://www.youtube.com/watch?v=IM630Z8lho8 |
@stereobooster Contracts are more general than just type checking — for example, you can have a contract that promises a function will return a positive integer. While this example does involve a type based contract, I don't really see how it's misleading — I'm sure we could come up with plenty of different examples but we don't want to overwhelm the reader and we want to keep things concise. Also, isn't programming/design by contract a bit off topic in the scope of simply defining contracts? As a side, C can't have a static type checker since C doesn't have a type system — do you mean something else? I do feel like there could be improvement on the example — feel free to make any suggestions! I found it a little tricky to exemplify contracts in vanilla ES6 without getting too complicated, so improvements are welcome. |
Like refinement types? For example in Haskell. Or dependent types like in Idris. Or effectful type system like in F* or Koka. i :: {ν :int | 1 ≤ ν ∧ ν ≤ 99}
Standard C code with types #include <stdio.h>
int main()
{
int number;
// printf() dislpays the formatted output
printf("Enter an integer: ");
// scanf() reads the formatted input and stores them
scanf("%d", &number);
// printf() displays the formatted output
printf("You entered: %d", number);
return 0;
} |
@stereobooster I was referring to types in the context of a static type system — everything in C is a number in this context. For example, JavaScript is an untyped language but still has primitives like |
Let me be more specific:
Or:
How is that not a statically typed language? |
@stereobooster Ah I see what you mean — I was thinking about type systems in general in the context of C and how since C doesn't have different values (everything is considered a byte). Just to be clear, you are correct in saying that C is a statically typed language, I got a bit off track here. Anyways, I digress — if you have any suggestions for improving this example/definition, feel free to post them here! |
Wrote a definition for contracts in programming with an example in ES6. Since there isn't really a way to enforce contracts natively in JavaScript, I tried to write what I thought was a simple example of a way to enforce a contract while still showing what it is exactly. Any suggestions for improvements are welcome!