diff options
author | Zahary Karadjov <zahary@gmail.com> | 2017-03-29 16:15:30 +0300 |
---|---|---|
committer | Zahary Karadjov <zahary@gmail.com> | 2017-03-29 16:15:30 +0300 |
commit | a74ad869e905737f144037f4ad5000eaed7a5bd2 (patch) | |
tree | 4c53830221f679e6d03f5d8aef6b3de904cc7c1a /doc/manual/generics.txt | |
parent | 01207b6cfda148c513d5b3fe7db024d1b5881a95 (diff) | |
download | Nim-a74ad869e905737f144037f4ad5000eaed7a5bd2.tar.gz |
requested code review changes
Diffstat (limited to 'doc/manual/generics.txt')
-rw-r--r-- | doc/manual/generics.txt | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/doc/manual/generics.txt b/doc/manual/generics.txt index bb5d0ab2c..87fcb7828 100644 --- a/doc/manual/generics.txt +++ b/doc/manual/generics.txt @@ -197,9 +197,6 @@ supply all type parameters of the generic type, because any missing ones will be inferred to have the equivalent of the `any` type class and thus they will match anything without discrimination. -To help you write more concise implicitly generic procs, the Nim's system -module includes the named types `T1` through `T9` which are bind once aliases -of the `auto` type. Concepts -------- @@ -436,17 +433,35 @@ in the place of each missing generic param. Please note that generic concepts such as `Enumerable[T]` can be matched against concrete types such as `string`. Nim doesn't require the concept type to have the same number of parameters as the type being matched. -In order to express such a requirement, you'll need to rely on a type -mapping operator such a `genericHead` or `stripGenericParams` within the -concept body: +If you wish to express a requirement towards the generic parameters of +the matched type, you can use a type mapping operator such as `genericHead` +or `stripGenericParams` within the body of the concept to obtain the +uninstantiated version of the type, which you can then try to instantiate +in any required way. For example, here is how one might define the classic +`Functor` concept from Haskell and then demonstrate that Nim's `Option[T]` +type is an instance of it: .. code-block:: nim import future, typetraits type Functor[A] = concept f - f.value is A - map(f, A -> T1) is genericHead(f.type)[T1] + type MatchedGenericType = genericHead(f.type) + # `f` will be a value of a type such as `Option[T]` + # `MatchedGenericType` will become the `Option` type + + f.val is A + # The Functor should provide a way to obtain + # a value stored inside it + + type T = auto + map(f, A -> T) is MatchedGenericType[T] + # And it should provide a way to map one instance of + # the Functor to a instance of a different type, given + # a suitable `map` operation for the enclosed values + + import options + echo Option[int] is Functor # prints true Concept derived values |