Posted By

codernator on 12/28/12


Tagged

api functionalprogramming


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

thastyle


Tony Morris Compile-Check API Challenge


 / Published in: C#
 

URL: http://blog.tmorris.net/understanding-practical-api-design-static-typing-and-functional-programming/

This is a first stab at defining a tic-tac-toe API that is compile-time safe, as outlined in Tony's blog: "Understanding Practical API Design, Static Typing and Functional Programming" (http://blog.tmorris.net/understanding-practical-api-design-static-typing-and-functional-programming/).

Although the game engine API is strongly typed, it's design implicitly introduces potential run-time errors in its consumption. Still, I like where it is going.

The trick to this is the use of Nullable. Since the output of the "move" function can't simultaneously satisfy the input types of most API functions (move, whoWon, TakeBack), I deemed a conversion function necessary. Using structs to define board state and Nullable, I could guarantee that a type-safe, non-null value is passed to the each of the API methods. What I can't guarantee is that the API consumer won't misuse the Nullable instances. Presumably, the IGameEngine API implementation is buried in a DLL and the setters on Board, WinnerBoard, PlayableBoard, and UndoBoard are internal only, so a consumer of the API can't construct invalid instances of those data types.

Report this snippet  

You need to login to post a comment.