Skip to content

Commit

Permalink
Show constructor to use receiver type to enable direct object propert…
Browse files Browse the repository at this point in the history
…y access, Show and Eq doc examples (arrow-kt#835)
  • Loading branch information
ersin-ertan authored and RawToast committed Jul 18, 2018
1 parent e6f8964 commit 2dbb8b2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ interface Show<in A> {
companion object {

/**
* Construct a [Show] instance from a function `(A) -> String`
* Construct a [Show] instance from a function `A.() -> String`
*
* @param fshow function that defines a textual representation for type [A].
* @returns a [Show] instance that is defined by the [fshow] function.
*/
inline operator fun <A> invoke(crossinline fshow: (A) -> String): Show<A> = object : Show<A> {
inline operator fun <A> invoke(crossinline fshow: A.() -> String): Show<A> = object : Show<A> {
override fun A.show(): String =
fshow(this)
}
Expand Down
5 changes: 5 additions & 0 deletions modules/docs/arrow-docs/docs/docs/typeclasses/eq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ Eq.any().run { Some(1).eqv(Option.just(1)) }
Eq.any().run { Eval.later { 1 }.eqv(Eval.later { 1 }) }
```

```kotlin:ank
// using invoke constructor
val intEq = Eq<Int> { a, b -> a == b }
```

See [Deriving and creating custom typeclass]({{ '/docs/patterns/glossary' | relative_url }}) to provide your own `Eq` instances for custom datatypes.

### Data Types
Expand Down
6 changes: 6 additions & 0 deletions modules/docs/arrow-docs/docs/docs/typeclasses/show/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ import arrow.typeclasses.*
Show.any().run { Option.just(1).show() }
```

```kotlin:ank
// using invoke constructor
class Person(val firstName: String, val lastName: String)
val personShow = Show<Person> { "Hello $firstName $lastName" }
```

See [Deriving and creating custom typeclass]({{ '/docs/patterns/glossary' | relative_url }}) to provide your own `Show` instances for custom datatypes.


Expand Down

0 comments on commit 2dbb8b2

Please sign in to comment.