March 05, 2007
Lionello Lunesu Wrote:

> SomeType st = 2;//construction
> 
> No need for constructors ;)

OK, To explain why I don't want to allow this syntax I need to explain som of how the quan library works.

Its main purpose is to catch errors in dimensional analysis.

So for example :

assume some types representing Force, Mass and Acceleration.

Now I say

Mass m;
Acceleration;
Force f = m * a; // OK this is dimensionally correct according to Physics.

However If I say:

Force f = m / a; // Error. This is dimensionally not correct and the library is designed to not allow this to compile.

In fact the dimension of all physical quantities can be represented as combinations of powers of the base dimensions  e.g( in The SI system):

length, mass, time, temperature, current, substance, intensity.

Force for example is mass^1, length ^1, time ^-2 etc.
(Just to jog the memory of Physics lessons)
(And behind the scenes this is how the library works, by computing the resulting dimension in a calculation using metaprogramming)

Also there are so called dimensionless quantities and these are represented in the library by inbuilt types.
e.g int, double etc are seen by the library as dimensionless types, and certain calculations Do result in dimensionless types.

E.g:

Length L1, L2;
double ratio  = L1/ L2; // OK the result is dimensionless so is a numeric type.


Therefore I don't allow the syntax:

Length L= 1;

As it is dimensionally incorrect to convert a numeric type to a physical quantity

I do however allow the syntax

Length L(1);

It is specifically allowed to allow value initialisation of a physical_quantity from a numeric. (At some point you need to load the initial value and experience of quan library shows this works and is a compact way to express it)

In C++ these 2 forms of initialisation can be differentiated by  making the constructor explicit, which is basically how I deal with it in C++.

I can get the explicit syntax in D I think by using a static OpCall, but I cant avoid the implicit construction :-(

Finally The physical quantity class is actually more complicated and can be constructed with dimensionally equivalent quantities but with other units and so on. I am not sure how I would tackle that problem in D, but at the moment I am stuck on this one.

regards
Andy Little




March 05, 2007
Lionello Lunesu Wrote:

> Instead of a constructor, create a "static opCall". opCall is the overload for "(..)" so you can instantiate your type similar to C++:
> 
> struct SomeType {
>    int member = 0;// default initializer here
>    static SomeType opCall( int whatever ) {
>      SomeType st;
>      st.member = whatever;//custom initialize
>      return st;
>    }
>    //...
> }
> 
> SomeType st = 2;//construction
> 
> No need for constructors ;)

Aha. If I chnge struct to class I am getting something close to what I want:
import std.stdio;
class X{

      static X opCall( double n )
      {
         X xx;
         xx.x = n;
         return xx;
      }

private:
   double x;
}


int main(char[][] args)
{
  X x = X(3); //ok
//###############
  X x = 3; // lError ine 21
  //#############
 X y = x; // OK
   writefln("%s",x.x);
   return 0;
}

gives:

class1.d(21): Error: cannot implicitly convert expression (3) of type int to cla
ss1.X
Error: cannot cast int to class1.X
(Which is what I want :-) )
That sorts that case out.

I'm back on the road for the moment :-)

regards
Andy Little




March 05, 2007
On Mon, 05 Mar 2007 20:15:04 +1100, Daniel Keep <daniel.keep.lists@gmail.com> wrote:

>
>
>Walter Bright wrote:
>> Uno wrote:
>>>> Its not possible it seems to do e.g this:
>>>>
>>>> X  x(3);
>>>>
>>>> rather you have to do:
>>>>
>>>> X x = new X(3);
>>>
>>> Yep, I don't like that syntax too. Everywhere news.. And although D has many great features such small things prevent me to switch to D.
>>>
>> 
>> You can do:
>> 
>>     auto x = X(3);
>> 
>> and x will be put on the stack.
>
>Surely you mean
>
>  scope x = X(3);
>
>Or did scope get rolled back into the auto keyword again while I wasn't looking? >_<

I think he means structs:

struct X{
	static X opCall( int n_in){
		X x;
		return x;
	}
 }

void main()
{
	auto x = X(1); //allocates x of type X on stack and assigns
the result of X.opCall(1) to it.

	X x1 = 1; // does the same thing to x1

	x1 = cast(X)2; 	//calls X.opCall(2) and assigns the result to
x1;
}

Scope classes are allocated on stack but still require 'new'.

class X
{
	this(int n_in)
	{
	}

	~this()
	{
	}
}

void test()
{
`	scope auto x = new X(1); // x allocated on stack
} //~this called on x on scope exit

void main()
{
	test();
}
March 05, 2007
On Mon, 05 Mar 2007 12:10:53 +0200, Max Samukha <samukha@voliacable.com> wrote:

>On Mon, 05 Mar 2007 20:15:04 +1100, Daniel Keep <daniel.keep.lists@gmail.com> wrote:
>
>>
>>
>>Walter Bright wrote:
>>> Uno wrote:
>>>>> Its not possible it seems to do e.g this:
>>>>>
>>>>> X  x(3);
>>>>>
>>>>> rather you have to do:
>>>>>
>>>>> X x = new X(3);
>>>>
>>>> Yep, I don't like that syntax too. Everywhere news.. And although D has many great features such small things prevent me to switch to D.
>>>>
>>> 
>>> You can do:
>>> 
>>>     auto x = X(3);
>>> 
>>> and x will be put on the stack.
>>
>>Surely you mean
>>
>>  scope x = X(3);
>>
>>Or did scope get rolled back into the auto keyword again while I wasn't looking? >_<
>
>I think he means structs:
>
>struct X{
>	static X opCall( int n_in){
>		X x;
>		return x;
>	}
> }
>
>void main()
>{
>	auto x = X(1); //allocates x of type X on stack and assigns
>the result of X.opCall(1) to it.
>
>	X x1 = 1; // does the same thing to x1
>
>	x1 = cast(X)2; 	//calls X.opCall(2) and assigns the result to
>x1;
>}
>
>Scope classes are allocated on stack but still require 'new'.
>
>class X
>{
>	this(int n_in)
>	{
>	}
>
>	~this()
>	{
>	}
>}
>
>void test()
>{
>`	scope auto x = new X(1); // x allocated on stack
>} //~this called on x on scope exit
>
>void main()
>{
>	test();
>}

Correction: surely not classes are allocated but objects.
March 05, 2007
> 
> Walter Bright wrote:
>> Uno wrote:
>>>> Its not possible it seems to do e.g this:
>>>>
>>>> X  x(3);
>>>>
>>>> rather you have to do:
>>>>
>>>> X x = new X(3);
>>> Yep, I don't like that syntax too. Everywhere news.. And although D
>>> has many great features such small things prevent me to switch to D.
>>>
>> You can do:
>>
>>     auto x = X(3);
>>
>> and x will be put on the stack.
> 
> Surely you mean
> 
>   scope x = X(3);
> 
> Or did scope get rolled back into the auto keyword again while I wasn't
> looking? >_<
>

http://www.digitalmars.com/d/memory.html#stackclass
March 05, 2007
Walter Bright wrote:
> Uno wrote:
>>> Its not possible it seems to do e.g this:
>>>
>>> X  x(3);
>>>
>>> rather you have to do:
>>>
>>> X x = new X(3);
>>
>> Yep, I don't like that syntax too. Everywhere news.. And although D has many great features such small things prevent me to switch to D.
>>
> 
> You can do:
> 
>     auto x = X(3);
> 
> and x will be put on the stack.

Uh, I think this one should go on the eater-eggs list!?

I've read the docs on "Allocating Class Instances On The Stack" but I had no idea you could instantiate classes without 'new'! I love it!

L.
March 05, 2007
Lionello Lunesu wrote:
> Walter Bright wrote:
>> Uno wrote:
>>>> Its not possible it seems to do e.g this:
>>>>
>>>> X  x(3);
>>>>
>>>> rather you have to do:
>>>>
>>>> X x = new X(3);
>>>
>>> Yep, I don't like that syntax too. Everywhere news.. And although D has many great features such small things prevent me to switch to D.
>>>
>>
>> You can do:
>>
>>     auto x = X(3);
>>
>> and x will be put on the stack.
> 
> Uh, I think this one should go on the eater-eggs list!?
> 
> I've read the docs on "Allocating Class Instances On The Stack" but I had no idea you could instantiate classes without 'new'! I love it!


Uhm I think you'll still need a struct + static opCall for this to work.

L.
March 05, 2007
> You can do:
> 
> 	auto x = X(3);
> 
> and x will be put on the stack.

I didn't know that shortcut.. thanks! Now it looks much better and it's acceptable for me, but c++ form is still shorter and more elegant (of course it's disputable).

March 05, 2007
"Andy Little" <andy@servocomm.freeserve.co.uk> wrote in message news:esgps5$2116$1@digitalmars.com...
> Aha. If I chnge struct to class I am getting something close to what I
> want:
> import std.stdio;
> class X{
>
>      static X opCall( double n )
>      {
>         X xx;

^^^ Don't forget to 'new' the instance of X here!

>         xx.x = n;
>         return xx;
>      }
>
> private:
>   double x;
> }

>
>
> 


March 05, 2007

Kyle Furlong wrote:
>>
>> Walter Bright wrote:
>>> Uno wrote:
>>>>> Its not possible it seems to do e.g this:
>>>>>
>>>>> X  x(3);
>>>>>
>>>>> rather you have to do:
>>>>>
>>>>> X x = new X(3);
>>>> Yep, I don't like that syntax too. Everywhere news.. And although D has many great features such small things prevent me to switch to D.
>>>>
>>> You can do:
>>>
>>>     auto x = X(3);
>>>
>>> and x will be put on the stack.
>>
>> Surely you mean
>>
>>   scope x = X(3);
>>
>> Or did scope get rolled back into the auto keyword again while I wasn't looking? >_<
>>
> 
> http://www.digitalmars.com/d/memory.html#stackclass

Yes, yes; I'm a dimwit.  Just ignore me :P  With all this talk about structs and classes and static opCall, I forgot which one we were talking about :3

	-- Daniel

-- 
Unlike Knuth, I have neither proven or tried the above; it may not even make sense.

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