Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upAd-hoc types #235
Conversation
bvssvni
added some commits
May 27, 2016
bvssvni
merged commit 45f8e93
into
PistonDevelopers:master
May 29, 2016
bvssvni
deleted the
bvssvni:ad_hoc_types
branch
May 29, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
bvssvni commentedMay 27, 2016
•
edited
Closes #236
This PR adds ad-hoc types to Dyon. It is a very simple type check that works well with dynamic modules, where ordinary type definitions are difficult to use since dynamic modules might be loaded in different orders and with different imports. Ad-hoc types makes it possible to have some extra type checks within the design space of Dyon.
How to use Ad-Hoc Types
Ad-hoc types in Dyon means you can write a name in front of the type of an argument, and the type checker will treat it as a distinct type. You can use any name that is not a built-in type in Dyon:
If you omit the type, Dyon will use
{}by default. It is common to use CamelCase for such types:The inner type and the ad-hoc type works with each other, so you can pass
Personto{}and vice versa:Restricted algebra with Ad-hoc types
One usage of ad-hoc types is to add physical units safety to your program. For this reason, the ad-hoc types disallow
+or-between an ad-hoc type and a built-in type:When you use
+or-, the result will be inferred to be the same ad-hoc type as both arguments.Multiplication is not allowed because this often changes the physical unit. To do multiplication, you need a function that unwraps the type before multiplication: