Eigenstate: myrddin-dev mailing list

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

Re: Traits with Auxiliary Types


I should also note, I'm not currently a fan of the syntax. Toying with replacements,
but for now it compiles and runs; The semantics are the important bit here.

On Wed, 16 Dec 2015 00:24:08 -0800, Ori Bernstein <ori@xxxxxxxxxxxxxx> wrote:

> 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
> 


-- 
    Ori Bernstein

References:
Traits with Auxiliary TypesOri Bernstein <ori@xxxxxxxxxxxxxx>