τau / ref /

ref.tau

source
/Users/niconex/Documents/tau/stdlib/ref/ref.tau
1# ref - a cell holding a value that a closure has to change.2#3# A closure reads the variables of the function around it, but it cannot write4# to them: assigning to one of those names makes a local of its own, which5# starts from the value read on the right of the assignment and shadows the6# name from there on. So this counts to three and then stops counting:7#8#	mk = fn() {9#		n = 010#		return fn() { n = n + 1; return n }   # always 111#	}12#13# What a closure can change is the inside of something it captured, because14# the name keeps meaning the same thing. A cell is that something:15#16#	mk = fn() {17#		n = ref.New(0)18#		return fn() { n.v = n.v + 1; return n.v }   # 1, 2, 3, ...19#	}20#21# Two closures made in the same place share the cell, so one can write what22# the other reads. That also means a cell handed to a tau routine is shared23# with it and nothing guards it: for values that travel between routines use24# a pipe, which is what pipes are for.2526# New returns a cell holding v, readable and writable as its field .v.27New = fn(v) {28	c = new()29	c.v = v30	return c31}