Generic Impls: Thinking Out Loud
[Thread Prev] | [Thread Next]
- Subject: Generic Impls: Thinking Out Loud
- From: Ori Bernstein <ori@xxxxxxxxxxxxxx>
- Date: Thu, 24 Dec 2015 19:49:09 -0800
- To: myrddin-dev@xxxxxxxxxxxxxx
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
| Re: Generic Impls: Thinking Out Loud | Ryan Gonzalez <rymg19@xxxxxxxxx> |
- Prev by Date: Re: aes ecb implementation
- Next by Date: [feature wishl] stdin and stdout
- Previous by thread: Re: aes ecb implementation
- Next by thread: Re: Generic Impls: Thinking Out Loud
- Index(es):