Eigenstate: myrddin-dev mailing list

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

Generic Impls: Thinking Out Loud


So, poking around code, I found myself wanting to be able to write something
along the lines of:

	trait hashable @a =
		hash	: (@a -> int32)
	;;


	/* hash any type shallowly: hash &a */
	impl hashable @a# =
		hash	= {val
			-> hashbytes(val[:sizeof(@a)])
		}
	;;

	/* hash any slice using its appropriate trait */
	impl hashable @a[:]
		hash = {sl
			var h

			h = 0
			for v in sl
				h ^= hash(&v)
			;;
			-> h
		}
	;;

	/* ...and similar... */

This would also allow useful things like:

	type enumerated(@a::iterable) =
		idx	: std.size
		it	: @a
	;;

	impl iterable enumerated(@a::iterable) -> (@a, std.size)
		__iternext__ = {enum, out
			var val
			if __iternext__(&enum.it, &val)
				-> (val, it.idx++)
			else
				-> false
			;;
		}
		...
	}
			

However, this seems like it would open up.. uh.. interesting choices.
I'd rather avoid going down the complex C++ path, so it makes me uneasy
when I realize I'd have to start defining rules for a "best match" for
a trait implementation :/

And things like

	impl foo (@a, int) = ...;;
	impl foo (int, @a) = ...;;

exist to make it more iteresting.

Other than C++ with its SFINAE, are there languages out there that have
a good model for doing this?

-- 
    Ori Bernstein

Follow-Ups:
Re: Generic Impls: Thinking Out LoudRyan Gonzalez <rymg19@xxxxxxxxx>