phunkie/phunkie-adt 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

phunkie/phunkie-adt

Composer 安装命令:

composer require phunkie/phunkie-adt

包简介

README 文档

README

"In computer programming, especially functional programming and type theory, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types." — good old Wikipedia

Sum Types

In computer science sum type, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use.

Let's say we want to create a type Weekday. We want it to be limited to the possible 7 days in the week: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.

We could do that in a number of ways using typical imperative or Object Oriented techniques. We could use enums, constants grouped under an interface or inheritance.

At the moment PHP does not have enums, so that one is not a choice.

Grouping constants in a interface is a popular alternative, but it does not give any type safety.

<?php
interface WeekDay {
    const SUNDAY = 'Sunday',
          MONDAY = 'Monday',
          TUESDAY = 'Tuesday',
          WEDNESDAY = 'Wednesday',
          THURSDAY = 'Thursday',
          FRIDAY = 'Friday',
          SATURDAY = 'Saturday';
}

When passing a weekday to a function I cannot specify the Weekday type as a type hint.

Finally we could try extending a super class or implementing a common interface.

<?php
interface Weekday {}
final class Sunday implements Weekday { }
final class Monday implements Weekday { }
//...

Now we can type hint Weekday we passing it around:

<?php
function orderPizza(Weekday $weekday, Pizza $pizza)
{
    switch (get_class($weekday)) {
        case Wednesday::class : return new WednesdayPromotionOrder($pizza);
        default : return new Order($pizza);
    }
}

There is, however, no way in PHP to guarantee that the interface won't be extended beyond the ones distributed with the library. Nothing stops a Pluto fan from:

<?php
final class Plutoday implements Weekday { }

And all of a sudden we can now order pizza in weird days. But fear not! Sum types is there to save us from that madness.

First we create a type constructor Weekday. PHP only way to create hintable types is through classes. So, let's use that.

<?php
abstract class Weekday implements TypeConstructor { use SumType; }

Then we seal it. Let's use ImmutableSealed to add immutablity while we are at it

<?php
abstract class Weekday extends ImmutableSealed implements TypeConstructor { use SumType; }

ImmutableSealed requires us to tell what is the types the type constructor can create

<?php
abstract class Weekday extends ImmutableSealed implements TypeConstructor, SumTypeTag {
    const sealedTo = [ Sunday::class, Monday::class, Tuesday::class,
        Wednesday::class, Thursday::class, Friday::class, Saturday::class];
    public function __construct() { $this->applySeal(); }
}

We still need to create the sum types:

<?php
final class Sunday extends Weekday { use SumType; const typeConstructor = Weekday::class; }
final class Monday extends Weekday { use SumType; const typeConstructor = Weekday::class; }
//...
final class Saturday extends Weekday { use SumType; const typeConstructor = Weekday::class; }

If we try to extend Weekday outside the seal we get a type error.

<?php
final class Plotoday extends Weekday { use SumType; const typeConstructor = Weekday::class; }

new Plutoday; // results in:
              // TypeError has been thrown with message: "Weekday is sealed and cannot be extended outside seal."

TODO:

  • Exhaustive analysis
  • Generics/Kinds: First order, higher order
  • Implementing Show, Eq, Ord

Final Notes

  • Phunkie is alpha software and may not be used in production

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-08-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固