Skip to content

Commit 72b015d

Browse files
cdepillabouthdgarrood
authored andcommitted
Add section on Deriving to the Differences from Haskell page (documentationjs#103)
* Changing spelling of "Purescript" to "PureScript" * Add section on Deriving to the Differences from Haskell page. * Fix explanation of deriving from feedback received on PR.
1 parent 0ebe8c1 commit 72b015d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

language/Differences-from-Haskell.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,39 @@ instance arbitraryUnit :: Arbitrary Unit where
161161

162162
Overlapping instances are still disallowed, like in Haskell. The instance names are used to help the readability of compiled JavaScript.
163163

164+
### Deriving
165+
166+
Unlike Haskell, PureScript doesn't have deriving functionality when declaring
167+
data types. For example, the following code does not work in PureScript:
168+
169+
```haskell
170+
data Foo = Foo Int String deriving (Eq, Ord)
171+
```
172+
173+
However, PureScript does have `StandaloneDeriving`-type functionality:
174+
175+
```purescript
176+
data Foo = Foo Int String
177+
178+
derive instance eqFoo :: Eq Foo
179+
derive instance ordFoo :: Ord Foo
180+
```
181+
182+
Examples of type classes that can be derived this way include `Eq`, `Functor`,
183+
and `Ord`. See
184+
[here](https://github.com/purescript/documentation/blob/master/language/Type-Classes.md#type-class-deriving)
185+
for a list of other type classes.
186+
187+
Using generics, it is also possible to use generic implementations for type
188+
classes like `Bounded`, `Monoid`, and `Show`. See
189+
[here](https://github.com/purescript/documentation/blob/master/guides/Generic.md)
190+
for a list of other type classes that have generic implementations, as well as
191+
an explanation of how to write generic implementations for your own type
192+
classes.
193+
164194
### Orphan Instances
165195

166-
Unlike Haskell, orphan instances are completely disallowed in Purescript. It is a compiler error to try to declare orphan instances.
196+
Unlike Haskell, orphan instances are completely disallowed in PureScript. It is a compiler error to try to declare orphan instances.
167197

168198
When instances cannot be declared in the same module, one way to work around it is to use [newtype wrappers](http://stackoverflow.com/questions/22080564/whats-the-practical-value-of-all-those-newtype-wrappers-in-data-monoid).
169199

0 commit comments

Comments
 (0)