Why every value is a string

Why patrun stores and matches values as text, takes only maps, and holds a Go pattern's data as a pointer to a string.

A pattern's values in patrun are text. A number or a boolean in a JavaScript pattern is stored as the text JavaScript gives it, and matched as text. The Go port goes further and takes only map[string]string.

Why text #

Matching by text keeps the rules simple. { a: 1 } matches a pattern made with { a: '1' }, and there is no question of whether 1 equals '1', or true equals 'true': each is its text, and texts are equal or they are not. The order of a list puts whole numbers in numeric order, which is the one place a value's number shows.

The cost is that text is not a number. 10 as a pattern value is the text 10, and does not match 10.0. Where a pattern needs numbers, the interval matcher reads the text back as a number when it matches, so =10 matches both.

Why only maps #

A pattern and a query are maps: their own properties are their keys and values, and nothing else counts. An array, a class instance, or a Map has properties of its own, but they are not what the caller means by a pattern, so patrun refuses them as patterns and matches nothing for them as queries. What an object inherits never counts, so a property added to Object.prototype by other code cannot change a match. Inside, nothing a router keeps inherits from Object.prototype either.

add and remove refuse what they cannot take by throwing, since they change the router and a mistake there should be loud. find, get, and list only ask, so they answer that nothing matches. What patterns and queries hold lists what each does.

Why Go uses strings and pointers #

JavaScript turns every value into text before storing it, so the Go port takes text at the type level: map[string]string. Formatting a number is the caller's work, with strconv.

Go has no undefined, so the port holds data as a *string, and nil means no data, as null and undefined do in JavaScript. The walk of the tree is then the same in both ports. The helper func s(v string) *string { return &v } hides the pointer where a pattern is added.

A string is less than JavaScript's data, which may be any value. A Go program that needs more stores a key, and looks the key up in its own table.

This page describes patrun 9.0.0 for JavaScript and 0.4.0 for Go. Edit this page · This page as Markdown