Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 04, 2013 Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Attachments:
| I was wondering if its possible to do interfaces the way #golang does it but in #dlang. Here is my first try: https://gist.github.com/rjmcguire/6431542. Any help on making this smaller would be awesome. Cheers, R |
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote:
> I was wondering if its possible to do interfaces the way #golang does it
> but in #dlang.
>
> Here is my first try: https://gist.github.com/rjmcguire/6431542.
> Any help on making this smaller would be awesome.
>
> Cheers,
> R
Here is another example use, checking if a type can convert itself to ubyte[]:
void main() {
int i = 0x34342343;
writebytes(i);
}
interface IRawBytes { ubyte[] bytes(); }
void writebytes(T)(T item) if (Implements!(T, IRawBytes)) {
import std.stdio : writeln;
writeln(item.bytes);
}
ubyte[] bytes(ref int i) {
ubyte* ptr;
ptr = cast(ubyte*)&i;
return ptr[0..i.sizeof];
}
In the above example you get a nice error message if int does not have the "bytes" ufcs function.
missing: ubyte[] int.bytes()
int, does not implement interface: traits.IRawBytes
traits.d(18): Error: template traits.writebytes does not match any function template declaration. Candidates are:
...
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 06:26:08 UTC, Rory McGuire wrote: > On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote: >> I was wondering if its possible to do interfaces the way #golang does it >> but in #dlang. >> >> Here is my first try: https://gist.github.com/rjmcguire/6431542. >> Any help on making this smaller would be awesome. >> >> Cheers, >> R > > Here is another example use, checking if a type can convert itself to ubyte[]: You can play with it here: http://dpaste.dzfl.pl/d7a727fd I still need to implement the part that checks aggregates. |
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 06:27:54 UTC, Rory McGuire wrote:
> On Wednesday, 4 September 2013 at 06:26:08 UTC, Rory McGuire wrote:
>> On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote:
>>> I was wondering if its possible to do interfaces the way #golang does it
>>> but in #dlang.
>>>
>>> Here is my first try: https://gist.github.com/rjmcguire/6431542.
>>> Any help on making this smaller would be awesome.
>>>
>>> Cheers,
>>> R
>>
>> Here is another example use, checking if a type can convert itself to ubyte[]:
>
> You can play with it here: http://dpaste.dzfl.pl/d7a727fd
>
> I still need to implement the part that checks aggregates.
hmmm, turns out it works on aggregates too.
works for structs, classes and basic types such as int and long.
Makes me wonder; is this in the standard library.
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote:
> I was wondering if its possible to do interfaces the way #golang does it
> but in #dlang.
>
> Here is my first try: https://gist.github.com/rjmcguire/6431542.
> Any help on making this smaller would be awesome.
>
> Cheers,
> R
Will this break, if I implement the free standing functions in a different module that is not in scope of the Implements! template?
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath Attachments:
| yes, it does seem to break if the Implements template func is in a different module to the free standing func.
hmph, how to get around that.
On Wed, Sep 4, 2013 at 9:04 AM, Tobias Pankrath <tobias@pankrath.net> wrote:
> On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote:
>
>> I was wondering if its possible to do interfaces the way #golang does it but in #dlang.
>>
>> Here is my first try: https://gist.github.com/**rjmcguire/6431542<https://gist.github.com/rjmcguire/6431542>
>> .
>> Any help on making this smaller would be awesome.
>>
>> Cheers,
>> R
>>
>
> Will this break, if I implement the free standing functions in a different module that is not in scope of the Implements! template?
>
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Attachments:
| So the general idea would be to add:
mixin("import "~ mod ~";");
into the check() inner function but mod needs to come from somewhere.
I haven't been able to make it as a template yet.
A template should get evaluated in the calling context.
On Wed, Sep 4, 2013 at 10:44 AM, Rory McGuire <rjmcguire@gmail.com> wrote:
> yes, it does seem to break if the Implements template func is in a different module to the free standing func.
>
> hmph, how to get around that.
>
>
> On Wed, Sep 4, 2013 at 9:04 AM, Tobias Pankrath <tobias@pankrath.net>wrote:
>
>> On Wednesday, 4 September 2013 at 00:56:55 UTC, Rory McGuire wrote:
>>
>>> I was wondering if its possible to do interfaces the way #golang does it but in #dlang.
>>>
>>> Here is my first try: https://gist.github.com/**rjmcguire/6431542<https://gist.github.com/rjmcguire/6431542>
>>> .
>>> Any help on making this smaller would be awesome.
>>>
>>> Cheers,
>>> R
>>>
>>
>> Will this break, if I implement the free standing functions in a different module that is not in scope of the Implements! template?
>>
>
>
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 08:44:45 UTC, Rory McGuire wrote:
> yes, it does seem to break if the Implements template func is in a
> different module to the free standing func.
>
> hmph, how to get around that.
I don't think it is possible. You can possibly have several modules with free standing functions with same signature which will match UFCS - how one may determine in general case which one to check against? Only way is to locally clone imports from the scope `Implements` is called from and I am not aware of any mechanism to do it.
Well, one can always switch to string mixins of course and invade caller scope but that is not clean by any means.
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rory McGuire | On Wednesday, 4 September 2013 at 08:56:10 UTC, Rory McGuire wrote:
> A template should get evaluated in the calling context.
No, in D templates use declaration scope (unless they are template mixins).
|
September 04, 2013 Re: Little demo of allowing basic types to implement interfaces. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot Attachments:
| thanks, yes, I just found that in TDPL. knew it was templates but forgot about template mixins.
Do you know how to get a default parameter like __MODULE__ or __LINE__ to
be used from the calling site?
I've tried but I think my DMD is broken because it doesn't even work when I
subclass Exception().
On Wed, Sep 4, 2013 at 11:02 AM, Dicebot <public@dicebot.lv> wrote:
> On Wednesday, 4 September 2013 at 08:56:10 UTC, Rory McGuire wrote:
>
>> A template should get evaluated in the calling context.
>>
>
> No, in D templates use declaration scope (unless they are template mixins).
>
|
Copyright © 1999-2021 by the D Language Foundation