A better way to deal with overloading?

Patrick Schluter via Digitalmars-d digitalmars-d at puremagic.com
Fri Jan 27 21:46:07 PST 2017


On Saturday, 28 January 2017 at 01:55:10 UTC, Profile Anaysis 
wrote:
> On Friday, 27 January 2017 at 11:16:09 UTC, Patrick Schluter 
> wrote:
>> On Thursday, 26 January 2017 at 00:02:03 UTC, Profile Anaysis 
>> wrote:
>>> [...]
>>
>> It's funny (or sad) that C has compound types since C99 and 
>> that they are good.
>> Your  foo(|a,b,|c1,c2,3||,|e|,|f,g,c|) writes as
>>
>> foo((T1){a,b,{c1,c2,c3}}, (T2){e}, (T3){f,g,c});
>>
>> of course, the lack of object orientation and other things 
>> makes it easier in C.
>
>
> Yeah, this seems similar to what I am saying. The only 
> difference is that the cast is not required because the types 
> can be deduced for functions. I don't see anything in the 
> spec(https://gcc.gnu.org/onlinedocs/gcc-4.2.2/gcc/Compound-Literals.html) showing function calls using "compound literals though", which was my suggestion.

The typecast in the compound statement is required because the 
initializer expression is ambiguous because of int promotion rules

struct aa { short a; char c; } and struct bb { int a; int b} have 
exactly the same initializer but are completely different types.
  { 5, 41 } applies to both without problem. In C the cast is 
required so that the compiler can generate the anonymous object 
(object in th C standard sense) in the right type. When the 
initialiser is used in an variable definition context then the 
type is deduced from the definition itself (no auto type 
deduction in C, so the type can be deduced).
In D the compound syntax doesn't exist, but it is not required, 
because there is already a syntax for something very similar. The 
type must be given explicitly for the same reason as in C as D 
follows the same integer promotion rules.
Furthermore, in the context of a function call (which is only a 
subpart of the more general creating/initialising of objects) D 
adds a difficulty that C doesn't : overloads. Imagine a function
f with 2 overloads void f(struct aa); and void f(struct bb);  
which one will be called if we use f({5, 41}); ? (pseudo syntax)
Templates take that issue to another level because there you 
don't even see explicitely the overloads, they are generated 
automagically by the compiler.

Conclusion: a new syntax is not necessary as the current way of 
doing things is already minimal.




More information about the Digitalmars-d mailing list