May 19, 2011
On 5/19/2011 10:02 PM, Steven Schveighoffer wrote:
> On Thu, 19 May 2011 09:43:14 -0400, Matthew Ong <ongbp@yahoo.com> wrote:
>

According to Jonathan,
 In the case of template mixins, you're essentially copying and pasting code. Yes. Liked what you said, with optimization at compiled time.

I am not trying to ask D to use JVM. (Please Note, that is why I choose D and also Go for the system level programming over C++ or Java JFFI.

>
> Java does *not* use templates, they are generics. A generic seems like,
> but is vastly different from a template.
>
> A template *re-generates* the code as if you substituted the type for
> the given template parameter.

> A generic generates one object code file with a base class, and then
> simply enforces the type constraints at compile time when *using* the
> generic. Under the hood, the generic is compiled with the base class as
> the given type. It probably avoids doing any type checking at runtime as
> well (since it knows the compiler will check the type for it).
Can someone really say why this is a bad bad idea for memory with some automated
plumbing being done like in ActiveX.


> The huge advantages of templates over generics are:
Yes. I do agree that template are NOT exactly generics,

>
> 1. The generated type can be fully optimized (footprint and code) for
> the given type parameters.
Yes. All compiler from source to binary has some sort of binary optimization. That is a really good thing. Javac has also those feature.
Please do as much optimization as D can be.

> 2. You can execute different code paths depending on the parameter types.
> 3. You do not have to select a common base class/interface for a group
> of types. That is, you can select any set of types that will work with
> your template. With generics, all possible types need to have a common
> base.
That is a good good thing to have, it allows the runtime binary to be reduced in time where only the data type is different. I understand that Java has a JVM to do that but I believe similar thing has been also
proven by ActiveX/Com+/DCom. I think they termed it Object Brokering.

> 4. Boxing/unboxing is not necessary for non-object types.
>
> The advantage of generics are that you only have one compiled version.
> This is actually very important for bytecode-based languages such as
> Java and C#.
But perhaps some location about dynamic loading and unlaoding of DLL be part
of D automatically. That is proven technology in the Linux Kernel for Device
Driver module.


>> LinkedList<String> list=new LinkedList<String>();
>> LinkedList<Integer> list=new LinkedList<Integer>();
>>
>> // Can also apply for Account.
>> LinkedList<Account> list=new LinkedList<Account>();
>>
>>
>> But there is a single LinkedList Class File(single binary).
>
> Yes, it's a LinkedList with the type given as Object.
>
> -Steve


-- 
Matthew Ong
email: ongbp@yahoo.com

May 19, 2011
On 2011-05-19 15:43, Matthew Ong wrote:
> On 5/19/2011 2:32 AM, Jacob Carlborg wrote:
>>
>> Template mixins are not exactly like copy and paste code.
>>
>> * You can mixin the same template, at the same location, twice, taking
>> different parameters
>>
>> * Mixed in methods doesn't overload on existing methods in the scope
>> where the mixin is used
>>
>> I think all this is due to mixins being mixed-in in it's own scope.
>>
> Hi Jacob,
>
> The last message confused me. Please clarify.
>  >not exactly like copy and paste code.

I can try to example by giving an example:

template Foo (int i) {
    void foo () {}
}

class Bar {
    mixin Foo!(3);
    mixin Foo!(4);
}

The above code works and the two methods "foo" will not conflict. You can refer to them via the template:

Foo!(3).foo;

Second example:

template Foo () {
    void foo (int i) {}
}

class Bar {
    mixin Foo;
    void foo () {}
}

If template mixins where like copy and paste the first example would result in an error. In the second example you would have two methods named "foo" overloading each other. But that's not what's happening.


> Ok...So, does it meant that the compiler share some sort of binary?
>
> In java using template, the same LinkedList binary is shared for both
> String class type and also Integer class type.
>
> LinkedList<String> list=new LinkedList<String>();
> LinkedList<Integer> list=new LinkedList<Integer>();
>
> // Can also apply for Account.
> LinkedList<Account> list=new LinkedList<Account>();
>
>
> But there is a single LinkedList Class File(single binary).
>
> mixin template does seemed to be better than #define (uncheck by
> compiler directly) it can be any text. M4 is a tool exactly that purpose.
>
>
>


-- 
/Jacob Carlborg
1 2
Next ›   Last »