Skip to content

Instantly share code, notes, and snippets.

View morrisonlevi's full-sized avatar

Levi Morrison morrisonlevi

View GitHub Profile
@morrisonlevi
morrisonlevi / Deque.php
Last active February 12, 2019 16:59
Brainstorming the interfaces for Deque, Queue, and Stack.
<?php
namespace Spl;
/**
* Indicates that a structure is designed to operate efficiently at both the
* front and the back of a dataset.
* @template T
*/
interface Deque
@morrisonlevi
morrisonlevi / Core Interfaces dreaming.php
Last active October 25, 2018 16:24
Maybe it could be like this, someday...
<?php
namespace PHP {
/* new semantic: $var[] = ; will look for ArrayAppendAccess first,
* then it will try ArrayWriteAccess with null key, which may fail.
*/
interface ArrayAppendAccess<-V> {
function offsetAppend(V $value): void;
}
@morrisonlevi
morrisonlevi / CMakeLists.txt
Created July 19, 2018 04:29
CMake 3.12: Transitive OBJECT Libraries issue
cmake_minimum_required(VERSION 3.12)
project(objectlib LANGUAGES CXX)
add_library(box OBJECT box.cc box.hh)
add_library(make_box OBJECT make_box.cc make_box.hh)
target_link_libraries(make_box PUBLIC box)
add_executable(main main.cc)
target_link_libraries(main PUBLIC make_box)
@morrisonlevi
morrisonlevi / CMakeLists.txt
Created July 19, 2018 04:29
CMake 3.12: Transitive OBJECT Libraries issue
cmake_minimum_required(VERSION 3.12)
project(objectlib LANGUAGES CXX)
add_library(box OBJECT box.cc box.hh)
add_library(make_box OBJECT make_box.cc make_box.hh)
target_link_libraries(make_box PUBLIC box)
add_executable(main main.cc)
target_link_libraries(main PUBLIC make_box)
@morrisonlevi
morrisonlevi / out-and-inout-parameters.md
Last active December 6, 2017 18:35
Preliminary draft for out and inout parameters

Out and inout parameters

  • Date: 2017-12-05
  • Author: Levi Morrison levim@php.net
  • Proposed for: PHP 7.NEXT or 8.0
  • Status: Draft

Introduction

Taking parameters by references allows functions to modify variables and those modifications are viewable in the sender. This comes at a price: the variable must be moved to the heap to be reference counted. Additionally, the type of the argument is checked on function entry but not on function exit. Interested parties can return by reference and add a return type but this does not scale beyond one parameter. This RFC proposes out and inout parameters which solve these issues while providing additional safety.

@morrisonlevi
morrisonlevi / Generics.md
Last active November 14, 2017 14:06
Thinking through some issues with adding generic types in PHP.

Let's consider ArrayAccess as a generic type. In order to preseve backwards- compatibility let's default the types to any type. Although I don't like the name mixed this is what documentation uses so I'll use it here:

class ArrayAccess<Element = mixed, Key = mixed> {
    function offsetExists(Key $offset): boolean;
    function offsetGet(Key $offset): Value;
    function offsetSet(?Key $offset, Element $value): void;
    function offsetUnset(Key $offset): void;
diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
index f1a7e38..a9b5a48 100644
--- a/Zend/zend_language_parser.y
+++ b/Zend/zend_language_parser.y
@@ -550,7 +550,6 @@ foreach_variable:
variable { $$ = $1; }
| '&' variable { $$ = zend_ast_create(ZEND_AST_REF, $2); }
| T_LIST '(' array_pair_list ')' { $$ = $3; $$->attr = ZEND_ARRAY_SYNTAX_LIST; }
- | '[' array_pair_list ']' { $$ = $2; $$->attr = ZEND_ARRAY_SYNTAX_SHORT; }
;
@morrisonlevi
morrisonlevi / BCDefinition.md
Last active November 21, 2016 16:20
PHP: What is a backwards compatibility break?

This is the basis for what I think a backwards compatibility break should be defined as and where they are appropriate.

Generally any change which will alter the previous behavior in any way is considered a backwards compatibility break. All backwards compatibility breaks must be voted on as per whatever the current voting rules are.

There are acceptable backwards compatibility breaks for minor releases (again, assuming they pass the vote):

  • Emitting new warnings, notices, and deprecations
  • Elevating a previous warning, notice or deprecation to be more strict (but not to an exception)
  • Adding a concrete method, property or constant to an existing class or trait
  • Adding an abstract method is not acceptable
@morrisonlevi
morrisonlevi / constexpr_bernstein_hash.cc
Last active November 14, 2015 23:03
A compile-time Bernstein hash example using constexpr (c++11 or above).
#include <cstddef>
#include <iostream>
constexpr std::size_t bernstein_hash(char const *s, std::size_t size, std::size_t value = 5381) {
return size ? bernstein_hash(s + 1, size - 1, 33 * value + *s) : value;
}
struct symbol {
std::size_t const len;
std::size_t const hash;
@morrisonlevi
morrisonlevi / Why ES6 arrow functions won't work in PHP.md
Last active February 2, 2018 09:19
Using the syntax for ES6 arrow functions wouldn't work in PHP. Here's why and how we could work around it.

In EcmaScript 2015 (ES6) the expression (x) => x * 2 means to create an anonymous function with one parameter x that will return x * 2. For example:

(x) => x * 2
// is equivalent to:
function(x) { return x * 2; }

A modified example from [documentation by Mozilla Developer Network][1] page demonstrates how they are useful:

var a = [

"Hydrogen",