Traits with Auxiliary Types
[Thread Prev] | [Thread Next]
- Subject: Traits with Auxiliary Types
- From: Ori Bernstein <ori@xxxxxxxxxxxxxx>
- Date: Wed, 16 Dec 2015 00:24:08 -0800
- To: myrddin-dev@xxxxxxxxxxxxxx
Traits with auxiliary types have landed, allowing a trait to be keyed off of
one type, but parameterized off of multiple types. This is a step towards
allowing iterators to work with the builtin foreach loop.
In general, the syntax is:
trait foo @primary -> @list, @of, @secondary
And the impl follows the same pattern. They are still used as
@a::foo
Proper documentation forthcoming. Another design, where all type parameters
were equal participants in the type, was considered, but it screws with
inference since there is no way to figure out which of the possible infinite
sea of return types is meant in cases like:
for x in std.bychars(str)
I may revisit this, though, if I can find a not-too-nasty solution to that
problem. For now, though, this is the sort of thing that has been enabled:
use std
trait iter @a -> @b =
next : (x : @a# -> std.option(@b))
;;
impl iter byte[:] -> char =
next = {s
var c
if s#.len == 0
-> `std.None
else
(c, s#) = std.striter(s#)
-> `std.Some c
;;
}
;;
impl iter int -> int=
next = {i
if i# > 100
-> `std.None
else
-> `std.Some i#++
;;
}
;;
const main = {
var str = "foobar"
var int : int = 73
while true
match next(&str)
| `std.Some c: std.put("c={}\n", c)
| `std.None: break
;;
;;
while true
match next(&int)
| `std.Some i: std.put("i={}\n", i)
| `std.None: break
;;
;;
}
Adding the built in trait comes next. I'm debating on the signature,
but currently leaning towards:
trait iterable @a -> @b =
next : (x : @a#, outret : @b# -> bool)
;;
since this removes a dependency on the standard library, without
requiring the user to come up with a bullshit value to return at the
end of iteration, in the way that the tuple option might.
--
Ori Bernstein
| Re: Traits with Auxiliary Types | Ori Bernstein <ori@xxxxxxxxxxxxxx> |
- Prev by Date: The website is open for pull requests.
- Next by Date: Re: Traits with Auxiliary Types
- Previous by thread: The website is open for pull requests.
- Next by thread: Re: Traits with Auxiliary Types
- Index(es):