Skip to main content

Coming from Clojure

2 min read Last updated
On this page

Most Clojure forms work the same in Phel, so this page lists what is different: the runtime, namespaces, interop, and the few forms Phel changes or leaves out.

What works the same

  • Core forms, destructuring, threading macros, #(...) function literals, multi-arity and variadic functions.
  • Vectors, maps, sets, keywords as functions, sequence functions, lazy sequences, and transducers.
  • atom, vars, binding (the var must be ^:dynamic), with-redefs, protocols, records, multimethods, and hierarchies.
  • try/catch, ex-info, ratios and big numbers, macros with syntax-quote, #"regex", #_, and Clojure truthiness: only nil and false are falsy, unlike PHP.
(def users [{:name "Alice" :active true} {:name "Bob" :active false}])

(->> users
     (filter :active)
     (map :name)
     (into #{}))
; => #{"Alice"}

What is different

ClojurePhelNotes
JVM, JARs, classpathPHP 8.5+Compiles to PHP and runs with your PHP binary
deps.edn, LeiningenComposer (composer.json) + phel-config.phpcomposer require phel-lang/phel-lang
lein repl, lein test, uberjarphel repl, phel test, phel buildphel build compiles to PHP
(:require [foo.bar :as b])(:require foo.bar :as b)The vector is optional. clojure.string is phel.string. See Namespaces
(:import (java.time Instant))(:use DateTimeImmutable):use imports PHP classes
Java interop (Math/pow 2 10)(php/pow 2 10)Any PHP function through php/. See PHP Interop
(.method obj), (Class/static), (Class.)sameWorks on PHP objects and classes
#?(:clj x :default y)#?(:phel x :default y)Platform key is :phel; #?@ also works
(memoize f) on a defn(defn ^:memoize f ...)memoize also exists
core.async, channelsasync/await, future-call, promise, pmapFiber-based, no CSP. See Async
agents, refs, STMnoneatom is the only mutable reference
clojure.specphel.schemaValidation, coercion, generation. See Schema
(* Long/MAX_VALUE 2) throwspromotes to BigIntPHP ints promote on overflow
inline protocol in defrecorddefinterface inline, defprotocol via extend-typeSee Protocols and interfaces
custom reader macrosnoneTagged literals only, see Reader conditionals
ClojureScriptnonePHP is the only target
CIDER, CalvanREPL and LSP serversSee Editor support

Protocols and interfaces

defprotocol works with extend-type, but you cannot implement a protocol inline in defstruct or defrecord. For an inline implementation, use definterface:

(definterface Greetable
  (greet [this]))

(defstruct person [name]
  Greetable
  (greet [this] (str "Hello, " name)))

(greet (person "Alice")) ; => "Hello, Alice"

See Interfaces.

Reader conditionals

#?() and #?@() use :phel and :default as platform keys, so one .cljc file can serve both languages. There are no custom reader macros; #inst, #uuid, #regex, and #php are built in, and register-tag from phel.reader adds more. See Reader conditionals.