-
-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added documentation for intersection types
- Loading branch information
1 parent
74eb1a0
commit 490b0b4
Showing
3 changed files
with
35 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: Combining Types | ||
layout: gem-single | ||
name: dry-types | ||
sections: | ||
- intersection | ||
- sum | ||
order: 11 | ||
--- | ||
|
||
Types can be combined to create new types, using [intersection types](docs::combining-types/intersection) | ||
to further restrict values, and [sum types](docs::combining-types/sum) to allow more acceptable values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
title: Intersection | ||
layout: gem-single | ||
name: dry-types | ||
order: 8 | ||
--- | ||
|
||
Intersection types are specified using the `&` operator. It combines two | ||
compatible types into a single one with properties from each. | ||
|
||
One example is a `Hash` that allows any keys, but requires one of them to be named `id`: | ||
|
||
```ruby | ||
Id = Types::Hash.schema(id: Types::Integer) | ||
HashWithId = Id & Types::Hash | ||
|
||
Id[{id: 1}] # => {:id=>1} | ||
Id[{id: 1, message: 'foo'}] # => {:id=>1} | ||
Id[{message: 'foo'}] # => Dry::Types::MissingKeyError: :id is missing in Hash input | ||
|
||
HashWithId[{ message: 'hello' }] # => Dry::Types::MissingKeyError: :id is missing in Hash input | ||
HashWithId[{ id: 1, message: 'hello' }] # => {:id=>1, :message=>"hello"} | ||
``` |