Jump to page: 1 24  
Page
Thread overview
Add support implicit conversion between types
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
Benjamin Thaut
Sep 06, 2013
Peter Alexander
Sep 06, 2013
Dicebot
Sep 06, 2013
Benjamin Thaut
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
Peter Alexander
Sep 06, 2013
Dicebot
Sep 06, 2013
Benjamin Thaut
Sep 07, 2013
Peter Alexander
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
Dicebot
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
monarch_dodra
Sep 06, 2013
H. S. Teoh
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
Dicebot
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
H. S. Teoh
Sep 06, 2013
Dmitry Olshansky
Sep 07, 2013
Simen Kjaeraas
Sep 06, 2013
H. S. Teoh
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
H. S. Teoh
Sep 06, 2013
deadalnix
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
deadalnix
Sep 06, 2013
Benjamin Thaut
Sep 06, 2013
Flamaros
Sep 06, 2013
ilya-stromberg
Sep 06, 2013
deadalnix
Sep 06, 2013
ilya-stromberg
Sep 07, 2013
Simen Kjaeraas
Sep 07, 2013
ilya-stromberg
Sep 08, 2013
Simen Kjaeraas
Sep 06, 2013
Jonathan M Davis
Sep 06, 2013
monarch_dodra
Sep 07, 2013
Maxim Fomin
Sep 07, 2013
monarch_dodra
Sep 07, 2013
Ramon
September 06, 2013
Do you have any plans to support implicit conversion between types?

I have some code like this:

struct Foo
{
	this(int i)
	{
		//do something useful
	}
}

void bar(Foo f)
{
	//do something else
}

void main()
{
	Foo f = 5;//works
	
	bar(f);//works
	
	bar(Foo(5));//works
	
	bar(5);//Error: function app.bar (Foo f) is not callable using
argument types (int)
}

So, D can't use constructor to convert "int" to "Foo" implicitly.
Can we add "implicit" keyword to allow do this:

struct Foo
{
	implicit this(int i)
	{
		//do something useful
	}
}

C++ allows this, but have "explicit" keyword.
C# doesn't allow this, but have operator overloading for both implicit and explicit cases.
September 06, 2013
Am 06.09.2013 12:33, schrieb ilya-stromberg:
> Do you have any plans to support implicit conversion between types?
>
> I have some code like this:
>
> struct Foo
> {
>      this(int i)
>      {
>          //do something useful
>      }
> }
>
> void bar(Foo f)
> {
>      //do something else
> }
>
> void main()
> {
>      Foo f = 5;//works
>
>      bar(f);//works
>
>      bar(Foo(5));//works
>
>      bar(5);//Error: function app.bar (Foo f) is not callable using
> argument types (int)
> }
>
> So, D can't use constructor to convert "int" to "Foo" implicitly.
> Can we add "implicit" keyword to allow do this:
>
> struct Foo
> {
>      implicit this(int i)
>      {
>          //do something useful
>      }
> }
>
> C++ allows this, but have "explicit" keyword.
> C# doesn't allow this, but have operator overloading for both implicit
> and explicit cases.

I also made almost the same suggestion some time ago. I would support a feature like this.

-- 
Kind Regards
Benjamin Thaut
September 06, 2013
On Friday, 6 September 2013 at 10:33:07 UTC, ilya-stromberg wrote:
> Do you have any plans to support implicit conversion between types?

Implicit conversions open up a whole can of worms for the sake of a small amount of convenience.

I'm not sure it's a fair trade.
September 06, 2013
On Friday, 6 September 2013 at 11:04:31 UTC, Peter Alexander wrote:
> Implicit conversions open up a whole can of worms for the sake of a small amount of convenience.
>
> I'm not sure it's a fair trade.

Agreed. It was quite common source of troubles in my C++ days and have never offered much convenience.
September 06, 2013
Am 06.09.2013 13:27, schrieb Dicebot:
> On Friday, 6 September 2013 at 11:04:31 UTC, Peter Alexander wrote:
>> Implicit conversions open up a whole can of worms for the sake of a
>> small amount of convenience.
>>
>> I'm not sure it's a fair trade.
>
> Agreed. It was quite common source of troubles in my C++ days and have
> never offered much convenience.

Its only a source of troubles in C++ because it is the default behavior. But if you design a library it can make the usage of your api easier and also you have a few more options to stay backwards compatible with your old api.
September 06, 2013
On Friday, 6 September 2013 at 11:04:31 UTC, Peter Alexander wrote:
> On Friday, 6 September 2013 at 10:33:07 UTC, ilya-stromberg wrote:
>> Do you have any plans to support implicit conversion between types?
>
> Implicit conversions open up a whole can of worms for the sake of a small amount of convenience.
>
> I'm not sure it's a fair trade.

I think we should be able to do it, but in unconvenient ways.

Like alias this + a property.
September 06, 2013
Am 06.09.2013 13:44, schrieb deadalnix:
> On Friday, 6 September 2013 at 11:04:31 UTC, Peter Alexander wrote:
>> On Friday, 6 September 2013 at 10:33:07 UTC, ilya-stromberg wrote:
>>> Do you have any plans to support implicit conversion between types?
>>
>> Implicit conversions open up a whole can of worms for the sake of a
>> small amount of convenience.
>>
>> I'm not sure it's a fair trade.
>
> I think we should be able to do it, but in unconvenient ways.
>
> Like alias this + a property.

But that doesn't work for construction. Also its not possible to do it for primitive types (especially string comes to mind)
September 06, 2013
On Friday, 6 September 2013 at 11:32:11 UTC, Benjamin Thaut wrote:
> Am 06.09.2013 13:27, schrieb Dicebot:
>> On Friday, 6 September 2013 at 11:04:31 UTC, Peter Alexander wrote:
>>> Implicit conversions open up a whole can of worms for the sake of a
>>> small amount of convenience.
>>>
>>> I'm not sure it's a fair trade.
>>
>> Agreed. It was quite common source of troubles in my C++ days and have
>> never offered much convenience.
>
> Its only a source of troubles in C++ because it is the default behavior. But if you design a library it can make the usage of your api easier and also you have a few more options to stay backwards compatible with your old api.

Totally agree. The default implicit conversion in C++ is a mistake.

The conversion must be explicit by default (like in D now).
The programmer should use implicit conversion ONLY IF IT SAFE (like ftom "int" to "long"). For any other cases programmer should use explicit conversion (default).

Actually, this simple rule comes from C#: it have explicit conversions by default and ability to explicitly add implicit conversion. And I never had problems with it.
September 06, 2013
On Friday, 6 September 2013 at 11:32:11 UTC, Benjamin Thaut wrote:
> Its only a source of troubles in C++ because it is the default behavior. But if you design a library it can make the usage of your api easier and also you have a few more options to stay backwards compatible with your old api.

It's not just that single arg constructors are by default implicit. Implicit conversions complicate things, like template argument deduction and overload resolution.
September 06, 2013
On Friday, 6 September 2013 at 11:32:11 UTC, Benjamin Thaut wrote:
> Its only a source of troubles in C++ because it is the default behavior. But if you design a library it can make the usage of your api easier and also you have a few more options to stay backwards compatible with your old api.

Probably. But what is the gain? `foo(Foo(5))` looks better than `foo(5)` to me in every possible way.

For example, use case that justifies operator overloading (despite the danger) in my eyes is ability to replace built-in types with custom ones. What is the similar rationale for implicit conversion?
« First   ‹ Prev
1 2 3 4