February 08

On Saturday, 3 February 2024 at 02:14:00 UTC, H. S. Teoh wrote:

>

I agree. In my early D code I also used a ton of alias this - it's very convenient, and lets me drop in replacements for existing types without a lot of tedious refactoring work. After a lot of experience working with it, though, I've come to think that in the long term it's not a good idea. While alias this lets you pretend that type T is type U, in the end it's still type T, and there comes a point where the distinction becomes important.

In one of my larger D codebases where I liberally used alias this for a handful of very frequently-used types, it got to a point where I had 3 different types for representing exactly the same thing. It really should have been 1 type, but I had a wrapper type for the underlying static array using alias this, then an extension over this wrapper some additional functionality. But (no) thanks to alias this, these types could interconvert, so for the most part things Just Worked(tm). Except sometimes they can't, then it's a head-scratching exercise of asking "now which of the 3 types is being used here, and which one should it have been?!". This question is a lot harder than it looks when stuff is being passed between templated generic code, where it's not obvious which concrete type is actually at work. And of course, there's the risk of instantiating the same template with 3 different types that ultimately represent exactly the same thing. Needless template bloat.

I really should have just refactored the code to use a single, consistent type throughout.

The above is overtly about implicit conversions as a consequence of using alias this. I largely agree about that particular construct. It can lead to conversions occurring almost anywhere a value of a type containing the alias this occurs, i.e. pervasive implicit conversion.

Another place where implicit conversions occur is in initialization in a variable declaration, where a single parameter constructor of the variable's type may be implicitly called. I think this is a good thing, and I haven't seen any complaints about this in D despite looking for them. Here implicit conversion is confined to only one kind of context. It is not pervasive.

>

And in general, my experience has been that implicit conversions tend to do more harm than good. They're really tempting in the short term because of the convenience. But in the long term they're really more trouble than they're worth. Living without implicit conversions is inconvenient, but in the long term it helps keep code easier to understand and more maintainable.

Perhaps that's because various implicit conversions have often been defined dangerously in the languages you have used.

I think having some flexibility to define a single parameter constructor of your type to be implicitly called in a narrow context only by flagging it in your type's definition with say @implicit is valuable.

It would only occur when passing a value to a parameter in the same way as it would be already be implicitly called when initializing a variable of your type to such a value. So the context for conversion would be limited, and have the same semantics as initialization does now in D.

It would simply turn parameter passing into limited initialization for your type. The same limited initialization that D already has in variable declarations with initialization.

Furthermore, if the view is that implicit conversion should be limited by default, the semantics of variable initialization could now be limited, and only constructors annotated with @implicit could be implicitly called in initializations too.

February 09
On Tuesday, 6 February 2024 at 10:10:32 UTC, FeepingCreature wrote:
> I just don't think that "this feature can be abused" is a convincing argument. So what? Make it opt-in, mark it as dangerous, then when people use it it's on them.
>
> I don't want implicit conversion to always be enabled, I want it to be enabled by a keyword or UDA. @implicit or something.

At one point something related was viewed with approval. Probably got nixed behind the scenes. Disappointing inflexibility indeed.

https://forum.dlang.org/post/lddjmh$8rn$1@digitalmars.com

February 09
On 2/9/2024 3:59 PM, Carl Sturtivant wrote:
> At one point something related was viewed with approval. Probably got nixed behind the scenes. Disappointing inflexibility indeed.
> 
> https://forum.dlang.org/post/lddjmh$8rn$1@digitalmars.com


Overloading is the distinguishing reason.
February 11
We've considered it many times, and have decided against it due to the consistently negative experience with it in C++. Despite it being opt-in, it gets routinely abused.

Overloading, even with all its restrictions in D, still gets abused to create incomprehensible code. When debugging Phobos code, often the only way I can figure out which overload is used is to insert `pragma(msg)` statements.

Sumtypes and pattern matching are important issues, and I'll talk about that at the upcoming DConf online.
February 14
On Sunday, 11 February 2024 at 19:26:39 UTC, Walter Bright wrote:
> We've considered it many times, and have decided against it due to the consistently negative experience with it in C++. Despite it being opt-in, it gets routinely abused.
>
> Overloading, even with all its restrictions in D, still gets abused to create incomprehensible code. When debugging Phobos code, often the only way I can figure out which overload is used is to insert `pragma(msg)` statements.
>
> Sumtypes and pattern matching are important issues, and I'll talk about that at the upcoming DConf online.

Variant is important to some of us because of its universality. If all conversions are blocked for parameter passing, can a replacement of Variant (say Any) be added to the list to be a part of the language?

This would be so that any literal or value of any type can be passed to a parameter of type Any, enabling naive scripting and experimenting in any context without an import.
February 13
On 2/13/2024 4:38 PM, Carl Sturtivant wrote:
> Variant is important to some of us because of its universality. If all conversions are blocked for parameter passing, can a replacement of Variant (say Any) be added to the list to be a part of the language?
> 
> This would be so that any literal or value of any type can be passed to a parameter of type Any, enabling naive scripting and experimenting in any context without an import.

Isn't that what a sumtype is?
February 14
On Wednesday, 14 February 2024 at 04:41:45 UTC, Walter Bright wrote:
> On 2/13/2024 4:38 PM, Carl Sturtivant wrote:
>> Variant is important to some of us because of its universality. If all conversions are blocked for parameter passing, can a replacement of Variant (say Any) be added to the list to be a part of the language?
>> 
>> This would be so that any literal or value of any type can be passed to a parameter of type Any, enabling naive scripting and experimenting in any context without an import.
>
> Isn't that what a sumtype is?

Informally Any would be the sum of all types. Would your proposal encompass this?
February 14
On 2/14/2024 9:46 AM, Carl Sturtivant wrote:
> Informally Any would be the sum of all types. Would your proposal encompass this?

No, each type would have to be enumerated in the declaration.

The usual way to do an Any type is to:

1. use Object as the common ancestor

2. use iUnknown as the common ancestor with QueryInterface

3. use void*

4. dmd internally uses its own RootObject for Any

5. use templates with a generic type T
February 14
On Wednesday, 14 February 2024 at 19:30:36 UTC, Walter Bright wrote:
> The usual way to do an Any type is to:
>
> 1. use Object as the common ancestor
>
> 2. use iUnknown as the common ancestor with QueryInterface
>
> 3. use void*
>
> 4. dmd internally uses its own RootObject for Any
>
> 5. use templates with a generic type T

The way Phobos currently does it is (essentially) by storing a void* and a TypeInfo internally.
February 15

On Wednesday, 14 February 2024 at 19:30:36 UTC, Walter Bright wrote:

>

On 2/14/2024 9:46 AM, Carl Sturtivant wrote:

>

On Wednesday, 14 February 2024 at 04:41:45 UTC, Walter Bright wrote:

>

On 2/13/2024 4:38 PM, Carl Sturtivant wrote:

>

This would be so that any literal or value of any type can be passed to a parameter of type Any, enabling naive scripting and experimenting in any context without an import.

Isn't that what a sumtype is?

Informally Any would be the sum of all types. Would your proposal encompass this?

No, each type would have to be enumerated in the declaration.

OK, I was asking if Any could explicitly be added to the language for the reasons stated above, as it cannot be implemented in D. How is your remark about Any being a sumtype connected to providing Any in D?