www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - implicit constructors

reply Trass3r <un known.com> writes:
Wasn't there some discussion quite some time ago about introducing an  
implicit/ implicit tag similar to C++'s explicit, just the other way  
around?

I know there's alias this, but what if you don't store the value directly?
struct A
{
	int store;

	this(int a)
	{
		store = a << 16;
		...
	}
}

A a = 5;
Nov 24 2011
next sibling parent reply Trass3r <un known.com> writes:
Seems like this is a CTFE-only issue?

import std.stdio;
struct A
{
	int store;

	this(int a)
	{
		store = a << 3;
		//...
	}
}

A a = 5; // Error: cannot implicitly convert expression (5) of type int to  
A

void main()
{
	A a = 5; // fine
	writeln(a.store); // prints 40
}
Nov 24 2011
parent Trass3r <un known.com> writes:
 Seems like this is a CTFE-only issue?
No.
 import std.stdio;
 struct A
 {
 	int store;

 	this(int a)
 	{
 		store = a << 3;
 		//...
 	}
 }
// A a = 5; // Error: cannot implicitly convert expression (5) of type int to A void foo(A a) { }
 void main()
 {
 	A a = 5; // fine
 	writeln(a.store); // prints 40
foo(5); // Error: cannot implicitly convert expression (5) of type int to A
 }
Nov 24 2011
prev sibling next sibling parent reply Trass3r <un known.com> writes:
anyone?
Nov 26 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 11/27/2011 12:14 AM, Trass3r wrote:
 anyone?
kk. struct A{ struct S{A opAssign(int rhs){return *(cast(A*)&this)=A(rhs);}} union{ struct{ // members int store; } S _x; // dummy struct } this(int a) { store = a << 16; //... } alias _x this; } void main(){ A x; x = 2; assert(x.store == 2 << 16); }
Nov 26 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 11/27/2011 01:54 AM, Timon Gehr wrote:
 On 11/27/2011 12:14 AM, Trass3r wrote:
 anyone?
kk. struct A{ struct S{A opAssign(int rhs){return *(cast(A*)&this)=A(rhs);}} union{ struct{ // members int store; } S _x; // dummy struct } this(int a) { store = a << 16; //... } alias _x this; } void main(){ A x; x = 2; assert(x.store == 2 << 16); }
A a = 5; Does not work though.
Nov 26 2011
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 11/27/2011 01:55 AM, Timon Gehr wrote:
 On 11/27/2011 01:54 AM, Timon Gehr wrote:
 On 11/27/2011 12:14 AM, Trass3r wrote:
 anyone?
kk. struct A{ struct S{A opAssign(int rhs){return *(cast(A*)&this)=A(rhs);}} union{ struct{ // members int store; } S _x; // dummy struct } this(int a) { store = a << 16; //... } alias _x this; } void main(){ A x; x = 2; assert(x.store == 2 << 16); }
A a = 5; Does not work though.
(Except in function scope, where it does.)
Nov 26 2011
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 11/27/2011 01:56 AM, Timon Gehr wrote:
 On 11/27/2011 01:55 AM, Timon Gehr wrote:
 On 11/27/2011 01:54 AM, Timon Gehr wrote:
 On 11/27/2011 12:14 AM, Trass3r wrote:
 anyone?
kk. struct A{ struct S{A opAssign(int rhs){return *(cast(A*)&this)=A(rhs);}} union{ struct{ // members int store; } S _x; // dummy struct } this(int a) { store = a << 16; //... } alias _x this; } void main(){ A x; x = 2; assert(x.store == 2 << 16); }
A a = 5; Does not work though.
(Except in function scope, where it does.)
NVM, I misunderstood the issue (I thought there were never implicit constructors. If there were not, my program would implement them.)
Nov 26 2011
prev sibling parent Trass3r <un known.com> writes:
 Wasn't there some discussion quite some time ago about introducing an  
 implicit/ implicit tag similar to C++'s explicit, just the other way  
 around?

 I know there's alias this, but what if you don't store the value  
 directly?
 struct A
 {
 	int store;

 	this(int a)
 	{
 		store = a << 16;
 		...
 	}
 }

 A a = 5;
http://d.puremagic.com/issues/show_bug.cgi?id=7019
Nov 29 2011