generic-traits


License

License

GroupId

GroupId

io.estatico
ArtifactId

ArtifactId

generic-traits_2.11
Last Version

Last Version

0.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

generic-traits
generic-traits
Project URL

Project URL

https://github.com/estatico/generic-traits
Project Organization

Project Organization

io.estatico
Source Code Management

Source Code Management

https://github.com/estatico/generic-traits

Download generic-traits_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/io.estatico/generic-traits_2.11/ -->
<dependency>
    <groupId>io.estatico</groupId>
    <artifactId>generic-traits_2.11</artifactId>
    <version>0.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/io.estatico/generic-traits_2.11/
implementation 'io.estatico:generic-traits_2.11:0.1.0'
// https://jarcasting.com/artifacts/io.estatico/generic-traits_2.11/
implementation ("io.estatico:generic-traits_2.11:0.1.0")
'io.estatico:generic-traits_2.11:jar:0.1.0'
<dependency org="io.estatico" name="generic-traits_2.11" rev="0.1.0">
  <artifact name="generic-traits_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.estatico', module='generic-traits_2.11', version='0.1.0')
)
libraryDependencies += "io.estatico" % "generic-traits_2.11" % "0.1.0"
[io.estatico/generic-traits_2.11 "0.1.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
org.typelevel : macro-compat_2.11 jar 1.1.1
com.chuusai : shapeless_2.11 jar 2.3.2

provided (2)

Group / Artifact Type Version
org.scala-lang : scala-compiler jar 2.11.8
org.scala-lang : scala-reflect jar 2.11.8

test (1)

Group / Artifact Type Version
org.scalatest : scalatest_2.11 jar 3.0.0

Project Modules

There are no modules declared in this project.

Generic Traits

Build Status Gitter Maven Central

Automatic generic derivation for traits

Usage

For traits which are not coproducts (e.g. not part of a sealed trait/case class family), this library allows you to derive shapeless Generic and LabelledGeneric instances from accessor fields. These instances allow us to operate on our traits generically; for instance, we can automatically derive type class instances for them.

Simply importing io.estatico.generic.traits._ will bring in the implicit macro used to derive instances. See the example usage below.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import io.estatico.generic.traits._

trait Foo {
  def bar: String
  def baz: Int
}

// Exiting paste mode, now interpreting.

scala> shapeless.Generic[Foo]
res0: shapeless.Generic[Foo]{type Repr = shapeless.::[String,shapeless.::[Int,shapeless.HNil]]} = $anon$1@38245121

scala> val foo = new Foo { val bar = "a" ; val baz = 1 }
foo: Foo{val bar: String; val baz: Int} = $anon$1@1f0ed694

scala> val repr = shapeless.Generic[Foo].to(foo)
repr: shapeless.::[String,shapeless.::[Int,shapeless.HNil]] = a :: 1 :: HNil

scala> val foo = shapeless.Generic[Foo].from(repr)
foo: Foo = $anon$1$$anon$2@10423297

scala> (foo.bar, foo.baz)
res1: String = (a,1)

scala> shapeless.LabelledGeneric[Foo]
res2: shapeless.LabelledGeneric[Foo]{type Repr = shapeless.::[String with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("bar")],String],shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged
[String("baz")],Int],shapeless.HNil]]} = shapeless.LabelledGeneric$$anon$1@1d3983be

scala> val repr = shapeless.LabelledGeneric[Foo].to(foo)
repr: shapeless.::[String with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("bar")],String],shapeless.::[Int with shapeless.labelled.KeyTag[Symbol with shapeless.tag.Tagged[String("baz")],Int],shapeless.HNil]] = a :
: 1 :: HNil

scala> val foo = shapeless.LabelledGeneric[Foo].from(repr)
foo: Foo = $anon$2$$anon$3@2a03c5d

scala> (res1.bar, res1.baz)
res3: (String, Int) = (a,1)
io.estatico

Estatico Studios LLC

Versions

Version
0.1.0
0.0.1