2017 © Pedro Peláez
 

library newtype

A handy function for creating opaque types

image

ajf/newtype

A handy function for creating opaque types

  • Wednesday, January 24, 2018
  • by ajf
  • Repository
  • 1 Watchers
  • 14 Stars
  • 8 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

newType - Create an opaque type with a single line of code

newType() is a handy function for creating opaque types, that is, a type that merely wraps another type. It takes two parameters: the name of your new wrapper type, and the name of the type you want to wrap. You get a class with a constructor taking a value of the wrapped type, and an ->unbox() method to get out the value of the wrapped type., (*1)

Require it with composer require ajf/newtype to use it. It's PHP 7-only, since PHP 7 is the first version of PHP with scalar type declarations. It would be possible to backport this, though., (*2)

An example of where you might use this:, (*3)

<?php

namespace JaneBlogges\WonderfulApp;

use function ajf\newType\newType;

// Makes the new opaque type!
newType(FilePath::class, 'string');

function moveFile(FilePath $sourcePath, FilePath $destinationPath): bool {
    return rename($sourcePath->unbox(), $destinationPath->unbox());
}

moveFile(new FilePath('foo'), new FilePath('bar'));

Use it for that extra bit of type safety!, (*4)

If you're after a mere alias that doesn't require explicit conversion to and from, check out PHP's built-in class\_alias function - though beware that only works with classes and not primitive types., (*5)

The name comes from Haskell's newtype declaration, which does the same thing:, (*6)

newtype FilePath = FilePath String

It's similar to Hack's newtype declaration as well, although that works a little differently., (*7)

The Versions

24/01 2018

dev-master

9999999-dev http://github.com/TazeTSchnitzel/newType

A handy function for creating opaque types

  Sources   Download

LGPLv3 LGPL-3.0-or-later

The Requires

  • php >=7.0.0

 

The Development Requires

php7

24/01 2018

v1.0.3

1.0.3.0 http://github.com/TazeTSchnitzel/newType

A handy function for creating opaque types

  Sources   Download

LGPL-3.0-or-later

The Requires

  • php >=7.0.0

 

The Development Requires

php7

03/05 2016

v1.0.2

1.0.2.0 http://github.com/TazeTSchnitzel/newType

A handy function for creating opaque types

  Sources   Download

LGPLv3

The Requires

  • php >=7.0.0

 

The Development Requires

php7

29/01 2016

v1.0.1

1.0.1.0 http://github.com/TazeTSchnitzel/newType

A handy function for creating opaque types

  Sources   Download

LGPLv3

The Requires

  • php >=7.0.0

 

The Development Requires

php7

10/11 2015

v1.0

1.0.0.0 http://github.com/TazeTSchnitzel/newType

A handy function for creating opaque types

  Sources   Download

LGPLv3

The Requires

  • php >=7.0.0

 

The Development Requires

php7