Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
May 27, 2011 How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Hi All, The main aim here is to find out how to model similar syntax within D. Due to the nature of the architecture of the library that I have designed in Java and heavily leans towards interface generics. It works well with java. Yes. I am aware about Tuple to allow me to do multiple value return. The point is Not about returning multiple, but how to moduel public interface DefType1<T1> { // Please note this is like interface template public Throwable getError(); public T1 getValue(); public String getDesc(); } // Elsewhere. public class RetVal1<T1> implements DefType1<T1> { private RetVal1() { } public static <T1> RetVal1<T1> make(T1 value) { RetVal1<T1> obj = new RetVal1<T1>(); obj.mValue = value; return obj; } private Throwable mError; public void setError(Throwable error) { mError = error; } public Throwable getError() { return mError; } private T1 mValue; public T1 getValue() { return mValue; } private String mDesc; public String getDesc() { return mDesc; } } // Yet, else where class Account{...} public static RetVal1<Account> methodA(int num, String str) { // similar to Instantiation template ... // do something. return RetVal2.<Account>make(num, str); } void main(string[] args){ RetVal1<Account> ret = methodA(1, "abc"); // some method call that return if(ret.getError()==null){ Account acc=ret.getValue1(); prnln("amount=" + acc.getAmount()); } } http://www.digitalmars.com/d/2.0/template.html#Constraint Struct, Union, and Interface Templates ... Analogously to class templates, struct, union and interfaces can be transformed into templates by supplying a template parameter list. However there is no example shown. In D the syntax should more or less look similar: interface DefType1(T1) { // No issue here public: Throwable getError(); T1 getValue(); String getDesc(); } public class RetVal1(T1) : DefType1(T1) { // ### or IS IT: DefType1!(T1) ?? ... public static RetVal1(T1) make(T1)(T1 value) { // static method template RetVal1(T1) obj = new RetVal1(T1)(); // ### or IS IT: new RetVal1!(T1)(); obj.mValue = value; return obj; } ... } public static RetVal1(Account) methodA(int num, String str) { //### How to do that?? Compilation error. Account acc=new Account(); ... // do something. return RetVal2.(Account)make(ac); //### How to do that?? compilation error also } RetVal1(Account) ret = methodA(1, "abc"); // Compilation error also. if(ret.getError()==null){ Account acc=ret.getValue1(); prnln("amount=" + acc.getAmount()); } ------------------------------------------------------------------------ Kindly show some compilable and working code sample. Thanks very much. -- Matthew Ong email: ongbp@yahoo.com |
May 27, 2011 Re: How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On 2011-05-27 07:55, Matthew Ong wrote: > Hi All, > > The main aim here is to find out how to model similar syntax within D. > > Due to the nature of the architecture of the library that I have > designed in Java and heavily leans towards interface generics. > It works well with java. > > Yes. I am aware about Tuple to allow me to do multiple value return. > The point is Not about returning multiple, but how to moduel > > public interface DefType1<T1> { // Please note this is like interface > template > public Throwable getError(); > public T1 getValue(); > public String getDesc(); > } > // Elsewhere. > > public class RetVal1<T1> implements DefType1<T1> { > > private RetVal1() { > } > > public static <T1> RetVal1<T1> make(T1 value) { > RetVal1<T1> obj = new RetVal1<T1>(); > obj.mValue = value; > return obj; > } > > private Throwable mError; > > public void setError(Throwable error) { > mError = error; > } > > public Throwable getError() { > return mError; > } > private T1 mValue; > > public T1 getValue() { > return mValue; > } > private String mDesc; > > public String getDesc() { > return mDesc; > } > } > > // Yet, else where > class Account{...} > > public static RetVal1<Account> methodA(int num, String str) { // similar > to Instantiation template > ... // do something. > return RetVal2.<Account>make(num, str); > } > > void main(string[] args){ > RetVal1<Account> ret = methodA(1, "abc"); // some method call that return > if(ret.getError()==null){ > Account acc=ret.getValue1(); > prnln("amount=" + acc.getAmount()); > } > } > http://www.digitalmars.com/d/2.0/template.html#Constraint > Struct, Union, and Interface Templates > ... > Analogously to class templates, struct, union and interfaces can be > transformed into templates by supplying a template parameter list. > However there is no example shown. > In D the syntax should more or less look similar: > > interface DefType1(T1) { // No issue here > public: > Throwable getError(); > T1 getValue(); > String getDesc(); > } > > public class RetVal1(T1) : DefType1(T1) { // ### or IS IT: DefType1!(T1) ?? > ... > public static RetVal1(T1) make(T1)(T1 value) { // static method template > RetVal1(T1) obj = new RetVal1(T1)(); // ### or IS IT: new RetVal1!(T1)(); > obj.mValue = value; > return obj; > } > ... > } > > > public static RetVal1(Account) methodA(int num, String str) { //### How > to do that?? Compilation error. > Account acc=new Account(); > ... // do something. > return RetVal2.(Account)make(ac); //### How to do that?? compilation > error also > } > > RetVal1(Account) ret = methodA(1, "abc"); // Compilation error also. > if(ret.getError()==null){ > Account acc=ret.getValue1(); > prnln("amount=" + acc.getAmount()); > } > ------------------------------------------------------------------------ > Kindly show some compilable and working code sample. > > Thanks very much. > In D the syntax for declaring a template and instantiate a template is not the same. Have a look at the first example of http://www.digitalmars.com/d/2.0/template.htm If don't understand after reading that example please ask again, I don't want to just give away the answer. You'll learn more by reading the documentation and figuring it out by yourself. -- /Jacob Carlborg |
May 27, 2011 Re: How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 5/27/2011 2:32 PM, Jacob Carlborg wrote: > On 2011-05-27 07:55, Matthew Ong wrote: >> > > In D the syntax for declaring a template and instantiate a template is > not the same. Have a look at the first example of > http://www.digitalmars.com/d/2.0/template.htm > If don't understand after reading that example please ask again, I don't > want to just give away the answer. You'll learn more by reading the > documentation and figuring it out by yourself. > Hi Jacob, > In D the syntax for declaring a template and instantiate a template is not the same. I do understand that declaring is with () and instantiate is !(). That is the reason that I am asking // ### or IS IT: DefType1!(T1) ?? RetVal1(T1) obj = new RetVal1(T1)(); // ### or IS IT: new RetVal1!(T1)(); I read that document. There are little but no practical model. Is there any such syntax being used within the probos lib? -- Matthew Ong email: ongbp@yahoo.com |
May 27, 2011 Re: How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On 2011-05-26 23:48, Matthew Ong wrote:
> On 5/27/2011 2:32 PM, Jacob Carlborg wrote:
> > On 2011-05-27 07:55, Matthew Ong wrote:
> >
> >
> > In D the syntax for declaring a template and instantiate a template is
> > not the same. Have a look at the first example of
> > http://www.digitalmars.com/d/2.0/template.htm
> > If don't understand after reading that example please ask again, I don't
> > want to just give away the answer. You'll learn more by reading the
> > documentation and figuring it out by yourself.
>
> Hi Jacob,
>
> > In D the syntax for declaring a template and instantiate a template
>
> is not the same.
> I do understand that declaring is with () and instantiate is !().
> That is the reason that I am asking
> // ### or IS IT: DefType1!(T1) ??
> RetVal1(T1) obj = new RetVal1(T1)(); // ### or IS IT: new RetVal1!(T1)();
>
> I read that document. There are little but no practical model.
>
> Is there any such syntax being used within the probos lib?
struct S(T)
{
this(T val)
{
this.val = val;
}
T val;
}
auto s = S!(int)(42);
or if S were a class
auto s = new S!(int)(42);
The parens are optional when there's only one template argument, so it could be S!int(42) and new S!int(42) instead.
There is no ! in the template definition, but you always use it when instantiating, unless it's inferred (which can be done with functions but not types). e.g.
T func(T)(T val)
{
return val + 2;
}
auto v = func!int(5);
auto w = func(5);
assert(v == w);
- Jonathan M Davis
|
May 27, 2011 Re: How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On 5/27/2011 2:48 PM, Matthew Ong wrote: > On 5/27/2011 2:32 PM, Jacob Carlborg wrote: >> On 2011-05-27 07:55, Matthew Ong wrote: Never mind, I found it. http://www.dsource.org/projects/tutorials/wiki/InterfaceTemplateExample -- Matthew Ong email: ongbp@yahoo.com |
May 27, 2011 Re: How to use interface template? How to model interface template properly in D. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthew Ong | On 5/27/2011 2:48 PM, Matthew Ong wrote: > On 5/27/2011 2:32 PM, Jacob Carlborg wrote: Thanks very much. -- Matthew Ong email: ongbp@yahoo.com |
Copyright © 1999-2021 by the D Language Foundation