Re: Traits seem to be working.
[Thread Prev] | [Thread Next]
- Subject: Re: Traits seem to be working.
- From: Ori Bernstein <ori@xxxxxxxxxxxxxx>
- Date: Sat, 21 Feb 2015 13:31:04 -0800
- To: Ryan Gonzalez <rymg19@xxxxxxxxx>
- Cc: myrddin-dev@xxxxxxxxxxxxxx
At the moment, no. Now that I think about it, this would also be equivalent
to shorthand for generic impls. In other words:
trait clonable @a =
dup = {v : @a
-> a
}
;;
would be equivalent to:
trait clonable @a =
dup : (v : @a -> @a)
;;
/* applies to all types */
impl clonable @a =
dup = {v
-> v
}
;;
Which leads to questions on how to pick the /most/ specific
specialization. A nice can of worms there.
On Sat, 21 Feb 2015 14:56:35 -0600
Ryan Gonzalez <rymg19@xxxxxxxxx> wrote:
> On that note...
>
> Is it possible to provide a default value for a function in a trait?
>
> On Sat, Feb 21, 2015 at 12:28 PM, Ori Bernstein <ori@xxxxxxxxxxxxxx> wrote:
>
> > Go me. I was too lazy to announce this when I got it working, but I
> > realized
> > last night that I should really send out a mail.
> >
> > As of a few weeks ago, I got traits working -- You can now define traits
> > for types, and expect them to actually compile and run. Traits are close
> > to concepts in C++, and similar to compile time only interfaces in certain
> > other languages. An example would be:
> >
> > /*
> > Define a hashable trait. This will allow any type that
> > has an implementation of this trait to get hashed.
> > */
> > trait hashable @a =
> > hash : (val : @a -> uint32)
> > ;;
> >
> > /* hashing implementation for integers */
> > impl hash int =
> > hash = {val
> > -> val * Bigprime
> > }
> > ;;
> >
> > /* hashing implementation for strings */
> > impl hash byte[:] =
> > hash = {str
> > var h = 1
> > for b in str
> > h = 33 * h * (b castto(int32))
> > ;;
> > }
> > ;;
> >
> > /* use the hashing */
> > const main = {
> > std.put("hash of 123: %i\n", hash(123))
> > std.put("hash of "abc": %i\n", hash("abc"))
> > /* this will error out, saying there's no trait implemented */
> > /* std.put("hash of 'c': \n", hash('c')) */
> > }
> >
> >
> > There are currently a few limitations on traits that I would like to
> > lift. The biggest one is that currently, it only works on concrete types.
> > You can't, for example, implement:
> >
> > impl hash @a::(numeric,integral) = ... ;;
> >
> > and get a hash implementation for every numeric type.
> >
> > This feature probably means that the API of some libraries could probably
> > be adjusted.
> >
> >
>
>
> --
> Ryan
> If anybody ever asks me why I prefer C++ to C, my answer will be simple:
> "It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was
> nul-terminated."
> Personal reality distortion fields are immune to contradictory evidence. -
> srean
> Check out my website: http://kirbyfan64.github.io/
| Traits seem to be working. | Ori Bernstein <ori@xxxxxxxxxxxxxx> |
| Re: Traits seem to be working. | Ryan Gonzalez <rymg19@xxxxxxxxx> |
- Prev by Date: Re: Traits seem to be working.
- Next by Date: What are the future plans for Myrddin?
- Previous by thread: Re: Traits seem to be working.
- Next by thread: What are the future plans for Myrddin?
- Index(es):