Thread overview
fun with tuples/argument conversion function
May 11, 2007
Robert Fraser
May 11, 2007
BCS
May 11, 2007
Robert Fraser
May 12, 2007
Daniel Keep
May 11, 2007
Hi all,

Right now I'm trying to write a function that, given an arbitrary number of arguments as D types concerts them into intermediate types that will be pasrd to the JNI. For example, given a char[], it will use the JNI to construct the relevant java.lang.string, and pass back the jobject.

The problem is that I can't define two tuples in the function template. For example, I can't write a function:
void dTypesToJavaTypes(InTypes..., OutTypes...)(InTypes inVars, out OutTypes outVars)

The number of in parameters will always be the same as the number of out parameters, and all the types should be known at compile-time, and though I'd rather not force the caller to know the out types, there's probably no way around that. Any suggestions? Maybe an out tuple and in varargs?
May 11, 2007
Reply to Robert,

> Hi all,
> 
> Right now I'm trying to write a function that, given an arbitrary
> number of arguments as D types concerts them into intermediate types
> that will be pasrd to the JNI. For example, given a char[], it will
> use the JNI to construct the relevant java.lang.string, and pass back
> the jobject.
> 
> The problem is that I can't define two tuples in the function
> template. For example, I can't write a function:
> 
> void dTypesToJavaTypes(InTypes..., OutTypes...)(InTypes inVars, out
> OutTypes outVars)
> 
> The number of in parameters will always be the same as the number of
> out parameters, and all the types should be known at compile-time, and
> though I'd rather not force the caller to know the out types, there's
> probably no way around that. Any suggestions? Maybe an out tuple and
> in varargs?
> 

alternate between inType and outType

void dTypesToJavaTypes(everything...)(everything stuff)
{
 foreach(i, thing, stuff)
 {
    static if(i & 0x01 == 0)
    {
       use stuff[i] and stuff[i+1]
    }
 }
}


nested templates:
template dTypes(InType...)
{
  void ToJavaTypes(OutType...)(InTypes..., OutTypes...)(InTypes inVars, out OutTypes outVars)
  {
       }
}

dTypes!(int).ToJavaTypes!(Jint)(i, ji);


May 11, 2007
Heh; nice thinking there! Thanks!

BCS Wrote:

> Reply to Robert,
> 
> > Hi all,
> > 
> > Right now I'm trying to write a function that, given an arbitrary number of arguments as D types concerts them into intermediate types that will be pasrd to the JNI. For example, given a char[], it will use the JNI to construct the relevant java.lang.string, and pass back the jobject.
> > 
> > The problem is that I can't define two tuples in the function template. For example, I can't write a function:
> > 
> > void dTypesToJavaTypes(InTypes..., OutTypes...)(InTypes inVars, out
> > OutTypes outVars)
> > 
> > The number of in parameters will always be the same as the number of out parameters, and all the types should be known at compile-time, and though I'd rather not force the caller to know the out types, there's probably no way around that. Any suggestions? Maybe an out tuple and in varargs?
> > 
> 
> alternate between inType and outType
> 
> void dTypesToJavaTypes(everything...)(everything stuff)
> {
>   foreach(i, thing, stuff)
>   {
>      static if(i & 0x01 == 0)
>      {
>         use stuff[i] and stuff[i+1]
>      }
>   }
> }
> 
> 
> nested templates:
> template dTypes(InType...)
> {
>    void ToJavaTypes(OutType...)(InTypes..., OutTypes...)(InTypes inVars,
> out OutTypes outVars)
>    {
> 
>    }
> }
> 
> dTypes!(int).ToJavaTypes!(Jint)(i, ji);
> 
> 

May 12, 2007
Robert Fraser wrote:
> Heh; nice thinking there! Thanks!
> 
> BCS Wrote:
>> alternate between inType and outType
>>
>> void dTypesToJavaTypes(everything...)(everything stuff)
>> {
>>   foreach(i, thing, stuff)
>>   {
>>      static if(i & 0x01 == 0)
>>      {
>>         use stuff[i] and stuff[i+1]
>>      }
>>   }
>> }
>>
>>
>> nested templates:
>> template dTypes(InType...)
>> {
>>    void ToJavaTypes(OutType...)(InTypes..., OutTypes...)(InTypes inVars,
>> out OutTypes outVars)
>>    {
>> 
>>    }
>> }
>>
>> dTypes!(int).ToJavaTypes!(Jint)(i, ji);
>>
>>
> 

The only other way I can think of doing it would be to make a template that, given the D type expands to the JNI type.  So...

template jniType(T)
{
    static if( is( T == char[] ) )
        alias javaString jniType;
    else // ...
}

Then you need a template to apply this to a tuple...

template jniTypes(T, Ts...)
{
    alias Tuple!(jniType!(T), jniTypes!(Ts)) jniTypes;
}

And now you can do this:

void dTypesToJavaTypes(InTypes...)(InTypes inVars,
        out jniTypes!(InTypes) outVars)
{
    // Do stuff
}

It's messy, but the nice thing is that it makes your templates easier to use.

That said, if you can get away with it, I'd go with BCS' approach. :)

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/