Eigenstate: myrddin-dev mailing list

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generic Impls: Landed.


They are only somewhat tested, but they seem to work so far. I wouldn't be
surprised to find that there were bugs in exporting across files, though.

As you might expect, the syntax is pretty much the same as you'd have for
any other impl, just with '@t' for the type. The algorithm for selecting
types is as follows:

	- Exact types always win.
	- From there, the best match for a generic type is selected.

So, what's the best match? This is computed by finding a for each generic
implmenetation, and picking the best unique one. If there is an ambiguity,
then your compilation will fail.

The score is the number matching specifiers (pointer, array, slice) that
the types have in common before the first parameter. For sequence types
like tuples, their score is equal to the sum of all their members.

So, an example of use would be something along the lines of:

	trait copyable @a =
		dup    : (x : @a -> @a)
	;;

	/* fallback for value type */
	impl copyable @a =
		dup = {x
			-> x
		}
	;;

	/* duplicate slices elementwise */
	impl copyable @sl[:] =
		dup = {orig
			var sl

			sl = [][:]
			for v in orig
				sl = std.slpush(sl, v)
			;;
			-> sl
		}
	;;

	/* duplicate pointers by value */
	impl copyable @p# =
		dup = {orig
			-> std.mk(orig#)
		}
	;;

	const main = {
		var x, y, z
		
		x = dup(123)	/* uses fallback impl */
		y = dup([1,2,3][:])	/* uses slice impl */
		z = dup(&123)	/* uses pointer impl */
	}

-- 
    Ori Bernstein