Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 29, 2019 `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument. It is a very serious problem. Even in C++ despite of all its silliness, T& is a type and can be used as a template argument. Can the language be changed to resolve this problem? |
March 29, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | On Friday, 29 March 2019 at 16:06:09 UTC, Victor Porton wrote:
> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>
> It is a very serious problem.
>
> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>
> Can the language be changed to resolve this problem?
Could you show an example, where an alias parameter is not enough?
|
March 29, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | On Friday, 29 March 2019 at 16:06:09 UTC, Victor Porton wrote:
> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>
> It is a very serious problem.
>
> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>
> Can the language be changed to resolve this problem?
You can't have reference variables in D, and I doubt walter will allow them. It doesn't make much sense to have a type that allows ref if you can't declare a variable with it.
|
March 29, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Friday, 29 March 2019 at 16:18:59 UTC, Alex wrote:
> On Friday, 29 March 2019 at 16:06:09 UTC, Victor Porton wrote:
>> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>>
>> It is a very serious problem.
>>
>> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>>
>> Can the language be changed to resolve this problem?
>
> Could you show an example, where an alias parameter is not enough?
An alias and a type are two different things and have different use cases.
|
March 29, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Friday, 29 March 2019 at 16:18:59 UTC, Alex wrote:
> On Friday, 29 March 2019 at 16:06:09 UTC, Victor Porton wrote:
>> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>>
>> It is a very serious problem.
>>
>> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>>
>> Can the language be changed to resolve this problem?
>
> Could you show an example, where an alias parameter is not enough?
The following does not work:
class C(alias T) { }
C(ref int) a;
|
March 29, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | On Friday, 29 March 2019 at 20:49:20 UTC, Victor Porton wrote:
> On Friday, 29 March 2019 at 16:18:59 UTC, Alex wrote:
>> On Friday, 29 March 2019 at 16:06:09 UTC, Victor Porton wrote:
>>> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>>>
>>> It is a very serious problem.
>>>
>>> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>>>
>>> Can the language be changed to resolve this problem?
>>
>> Could you show an example, where an alias parameter is not enough?
>
> The following does not work:
>
> class C(alias T) { }
> C(ref int) a;
Why do you expect it to work? What exactly are you supposed to do with that?
ref can only be used as parameters and thus it would only ever work like:
class C(alias T)
{
void foo(T bar) { ... }
}
C!(ref int) baz;
Which is just ugly and makes no sense.
And also in that case alias is unnecessary.
class C(T)
{
void foo(T bar) { ... }
}
C!(ref int) baz;
Which still makes no sense.
Is there a reason why you cannot do the following?
class C(T)
{
void foo(ref T bar) { ... }
}
Because why would you ever want the implementation of a function to behave differently depending on whether its parameters are passed by reference or not.
In fact instead there should be a ref overload and one without.
|
March 31, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Porton | On Fri, Mar 29, 2019 at 9:10 AM Victor Porton via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>
> That `ref T` (where T is a type) is not a type is a serious design error, because so `ref T` cannot be used as a template argument.
>
> It is a very serious problem.
>
> Even in C++ despite of all its silliness, T& is a type and can be used as a template argument.
>
> Can the language be changed to resolve this problem?
1. I couldn't possibly agree with you more!!! Sadly that's not how it is.
2. You can use `auto ref` on function arguments of template functions
to capture the ref-ness of the calling argument: void fun(T)(auto ref
T arg)
3. I have often deployed something like this (I just typed this in the email as example, I have no idea if this compiles or runs):
Ref(T)
{
T* value;
this(ref T val) { value = &val; }
ref T opAssign(U)(auto ref U rh) { *value = rh; }
ref T get() { return *value; } inout;
alias get this;
}
...
template isRef(T) = is(T == Ref!U, U);
void fun(T)(T arg)
{
pragma(msg, isRef!T ? "is a ref" : "not a ref");
}
int i;
auto ri = Ref!int(i);
fun(i); // > "not a ref"
fun(ri); // > "is a ref"
Writing that code is like stabbing myself in the hands with rusty forks... I feel emotionally unstabilised, but it can solve your problems in various applications.
|
March 31, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 3/31/2019 3:48 PM, Manu wrote: >> Even in C++ despite of all its silliness, T& is a type and can be >> used as a template argument. >> >> Can the language be changed to resolve this problem? > > 1. I couldn't possibly agree with you more!!! Sadly that's not how it is. Although ref can be captured with C++ templates, it isn't really a type. For example, you cannot create a pointer to a ref. Furthermore, the spec for it is crammed with wacky special cases to make it behave like a storage class instead of a type. I defy anyone to give a list of cases when C++ ref is inferred and when it is not, and if anyone did, I'm sure it would be wrong. Yes, it's that bad. > Writing that code is like stabbing myself in the hands with rusty > forks... I'm convinced that if C++ ref didn't exist, and we added it to D, you'd hate it :-) > I feel emotionally unstabilised, but it can solve your > problems in various applications. Please show a use case. I can't think of one. |
March 31, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rubn | On 3/29/2019 12:55 PM, Rubn wrote:
> You can't have reference variables in D, and I doubt walter will allow them. It doesn't make much sense to have a type that allows ref if you can't declare a variable with it.
You can't declare a pointer to a ref in C++, either. It's not a real type.
|
April 01, 2019 Re: `ref T` should be a type!! | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Monday, 1 April 2019 at 00:18:40 UTC, Walter Bright wrote:
> On 3/29/2019 12:55 PM, Rubn wrote:
>> You can't have reference variables in D, and I doubt walter will allow them. It doesn't make much sense to have a type that allows ref if you can't declare a variable with it.
>
> You can't declare a pointer to a ref in C++, either. It's not a real type.
It's a type with a restriction, you can literally use it everywhere else you can use a type. A reference is just a pointer with a dress on, having a naked pointer go with a dressed pointer doesn't make much sense.
Taking the address of a reference returns a pointer to the object the reference points to, not a pointer to the reference. It's impossible to get the address of the actual reference (* without some hack).
But sure it's not a type because you can't have a pointer to it.
std::is_same_v<int, int&>;
|
Copyright © 1999-2021 by the D Language Foundation