Skip to content

Commit

Permalink
add experimental refraction module (#10)
Browse files Browse the repository at this point in the history
Squashed:

* add experimental refraction module
* review round 1
* do not require caller to import std.traits
* more doc and unit test tweaks
* review round 2
* review round 3
* review round 4
* in code, use __MODULE__ instead of spelling it out
* avoid breaking the 80th column wall as much as possible
  • Loading branch information
jll63 committed Jul 20, 2020
1 parent c3cce75 commit 400b487
Show file tree
Hide file tree
Showing 4 changed files with 772 additions and 1 deletion.
24 changes: 24 additions & 0 deletions README.md
Expand Up @@ -21,6 +21,7 @@ Bolts is a utility library for the D programming language which contains a numbe
* **iz**: super non-eponymous template that provides a lot of the functionality that's in the traits module with a different sytax that allows their usage in meta functions as well.
* **experimental**: contains experimental features
*signatures: working implementation of type signatures
*refraction: generate mixin strings to replicate a function, with some changes

Most functions here operate on any compile time entity. For example `isUnaryOver` works in both these situatons:

Expand Down Expand Up @@ -82,3 +83,26 @@ source/bolts/experimental/signatures.d(310,5): Error: static assert: "Type MyRa
source/bolts/experimental/signatures.d(464): <-- Signature InputRange!(int) defined here.
source/bolts/experimental/signatures.d(471): <-- Checked here."
```

## Refraction (experimental):

It is sometimes necessary to create a function which is an exact copy of
another function. Or sometimes it is necessary to introduce a few variations,
while carrying all the other aspects. Because of function attributes, parameter
storage classes and user-defined attributes, this requires building a string
mixin. In addition, the mixed-in code must refer only to local names, if it is
to work across module boundaires. This module facilitates the creation of such
mixins.

For example, this creates a function that has a different name and return type,
but retains the 'pure' attribute from the original function:

```d
pure int answer() { return 42; }
mixin(
refract!(answer, "answer").withName("realAnswer")
.withReturnType("real")
.mixture);
static assert(is(typeof(realAnswer()) == real));
static assert(functionAttributes!realAnswer & FunctionAttribute.pure_);
```
11 changes: 10 additions & 1 deletion dub.json
Expand Up @@ -9,5 +9,14 @@
"targetPath": "bin",
"toolChainRequirements": {
"frontend": ">=2.086"
}
},
"configurations": [
{
"dflags": [
"-mixin=mixins.txt"
],
"name": "unittest",
"targetType": "library"
}
],
}

0 comments on commit 400b487

Please sign in to comment.