aslike 0.4.0

Duck-typing with check interface

To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:


Build Status codecov Dub Downloads License

This lib can be helpful if you want to declare function or method what must get some object as argument, and it object must contains all methods what you want, and if you don't want use classic OOP and/or want use structs. If you use classic OOP and your functions and/or methods already gets interface as argument you can wrap structs to auto-implement objects.


struct Like(T) if (is(T == interface)) -- wrapper for T, it have methods and private delegate fields matches all methods from T and method for filling delegates from object fillDelegatesFrom(bool nullCheck=true, X)(ref X src).

class LikeObj(T) : T -- wrapper for T, as Like!T, but class.

class LikeObjCtx(T, S) : LikeObj!T -- wrapper for T, as LikeObj!T, but with S __context field (usable for short lifetime structs).

Like!T as(T, bool nullCheck=true, X)(ref X obj) if (is(T == interface)) -- wrap object obj into Like!T if it possible (fill all delegate fields in returned Like!T with methods pointers from obj). If X is class or interface and if nullCheck is true enforce obj is not null.

T asObj(T, bool nullCheck=true, X)(ref X obj) if (is(T == interface)) -- wrap object obj into LikeObj!T if it possible.

T asObjCtx(T, bool nullCheck=true, X)(auto ref X obj) if (is(T == interface)) -- copy obj into private field of LikeObjCtx!(T, X) and wrap this field, instead of original obj.

T toObj(T)(auto ref const Like!T th) @property -- make new wrapper object (LikeObj!T -- without saving th) and fill delegates from th delegates.


Overridable method with any compatible type: example

Cache virtual methods table lookup for improving multiple call methods performance: cache example

Use struct when method or function gets interface instance as argument: wrap to object example


With -betterC flag compatibles only struct Like and function as. Like objects emulates inheritance through alias this, but only if interface have exact one parent. See example


Like!T struct and LikeObj!T class have reference symantics respect to wrapped object.

Be careful when wrap objects allocated on stack: when program exit from scope, reference to object saved in delegates will be broken.

  • deviator
0.4.0 2020-Dec-12
0.3.0 2020-Aug-12
0.2.0 2020-Aug-12
0.1.1 2020-Jul-11
0.1.0 2020-Jul-11
Show all 6 versions
Download Stats:
  • 0 downloads today

  • 0 downloads this week

  • 0 downloads this month

  • 24 downloads total

Short URL: