Code Monkey home page Code Monkey logo

newtypes's Introduction

Newts

Better newtypes for Scala based on the following two articles:

Quick Start

addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-M10" cross CrossVersion.full)
resolvers += Resolver.bintrayRepo("alexknvl", "maven")
libraryDependencies += "com.alexknvl"  %%  "newtypes" % "0.3.0"

Use this fork of scalameta/paradise until https://github.com/scalameta/paradise/pull/207 is merged in if you need companion object support and can't use paradise-3.0.0-M10 (it supports only 2.12.3+). Clone it and publishM2 in sbt, then change the paradise plugin to:

addCompilerPlugin("org.scalameta" % "paradise" % "3.0.0-alex" cross CrossVersion.full)

Why newts?

Features AnyVal @newtypes.opaque @newtypes.translucent
isInstanceOf Yes No No
Generics box AnyRef subtypes Yes No No
Generics box primitives Yes Yes Yes
Primitives box No Yes No
Up-cast works No No Yes
Down-cast works No No No
Any methods Yes Yes Yes
Overrides Yes No No
Virtual dispatch Yes No No
Typeclass-based dispatch Yes Yes Yes
Wrap List elements O(N) O(1) (using subst) O(1) (using subst)
Unwrap List elements O(N) O(1) (using subst and Is) O(1) (automatic widening)
Supports HK parameters Yes Yes Yes
Supports existential parameters Yes Yes Yes

What does it do?

@opaque type ArrayWrapper[A] = Array[A]

@translucent type Flags = Int

becomes

type ArrayWrapper[A] = ArrayWrapper.Type[A]
object ArrayWrapper {
  type Base$$1
  trait Tag$$1 extends Any
  type Type[A] <: Base$$1 with Tag$$1
  
  object Impl {
    def apply[A](value: Array[A]): Type[A] = value.asInstanceOf[Type[A]]
    def unwrap[A](value: Type[A]): Array[A] = value.asInstanceOf[Array[A]]
    def subst[F[_], A](value: F[Array[A]]): F[Type[A]] = value.asInstanceOf[F[Type[A]]]
  }
}

type Flags = Flags.Type
object Flags {
  trait Tag$$1 extends Any
  type Type <: Int with Tag$$1
  object Impl {
    def apply(value: Int): Type = value.asInstanceOf[Type]
    def unwrap(value: Type): Int = value.asInstanceOf[Int]
    def subst[F[_]](value: F[Int]): F[Type] = value.asInstanceOf[F[Type]]
  }
}

License

Code is provided under the MIT license available at https://opensource.org/licenses/MIT, as well as in the LICENSE file.

newtypes's People

Contributors

coltfred avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.