Jump to page: 1 2 3
Thread overview
Implicit conversion to mutable if no indirections?
Sep 02, 2022
Ali Çehreli
Sep 02, 2022
rikki cattermole
Sep 02, 2022
Ali Çehreli
Sep 02, 2022
rikki cattermole
Sep 03, 2022
Mathias LANG
Sep 03, 2022
Daniel N
Sep 03, 2022
Nick Treleaven
Sep 03, 2022
Dmitry Olshansky
Sep 04, 2022
Nick Treleaven
Sep 04, 2022
Salih Dincer
Sep 04, 2022
Ali Çehreli
Sep 04, 2022
Mathias LANG
Sep 04, 2022
Meta
Sep 06, 2022
Quirin Schroll
Sep 06, 2022
Loara
Sep 06, 2022
Quirin Schroll
Sep 07, 2022
Loara
Sep 06, 2022
Paul Backus
Sep 06, 2022
Nick Treleaven
Sep 07, 2022
Simen Kjærås
September 02, 2022
An issue I have is non-mutable expressions without indirections not converting to mutable. I talked about this before and mentioned Unqual at DConf 2022 to explain how it solves this.

The problem happens in template code:

void main() {
  const i = 42;  // const, so foo's T will be const below
  foo(i);
}

void foo(T)(T value) {
  T result;
  ++result;  // Compilation error; but should it work?
}

I feel like it should work because 'result' is a local int.

But I am aware that we can't deduce T to be 'int' because we would be losing that qualifier and further template deductions would be wrong. :/

Anyway... That's one thing that bugs me occasionally and causes bugs like the following one, which I think is caused by a missing Unqual:

  https://issues.dlang.org/show_bug.cgi?id=23319

At least, there is a moral here at least for myself: Don't forget to test your templates with const, etc.

Ali
September 03, 2022
```d
void main() {
	auto foo = const Foo("whatever");
	bar(foo);
}

struct Foo {
	char[] data;
}

void bar(T)(T value) {
	T thing = value;
	thing.data[0] = 'a';
}
```

Do you want this to error at compile time or at runtime due to writing to read only memory?
September 02, 2022
On 9/2/22 12:05, rikki cattermole wrote:

> struct Foo {
>      char[] data;
> }

> Do you want this to error at compile time or at runtime due to writing
> to read only memory?

Fair enough but that type has an indirection. I "feel" it should work for fundamental types, etc. without indirections but as I said, I understand it will ruin template type deduction as well as function overloading which I forgot to mention.

This thread is here to remind us to test our templates for const, etc. :)

Ali

September 03, 2022
On 03/09/2022 7:19 AM, Ali Çehreli wrote:
> Fair enough but that type has an indirection. I "feel" it should work for fundamental types, etc. without indirections but as I said, I understand it will ruin template type deduction as well as function overloading which I forgot to mention.

Yeah that's the problem isn't it, its exceptions in the type system that you have to add to get it to work for basic types only. For other types like slices, pointers, structs and classes it gets dangerous.
September 02, 2022

On 9/2/22 2:58 PM, Ali Çehreli wrote:

>

An issue I have is non-mutable expressions without indirections not converting to mutable. I talked about this before and mentioned Unqual at DConf 2022 to explain how it solves this.

The problem happens in template code:

void main() {
  const i = 42;  // const, so foo's T will be const below
  foo(i);
}

void foo(T)(T value) {
  T result;
  ++result;  // Compilation error; but should it work?
}

I feel like it should work because 'result' is a local int.

result is a local const int. That's because T is a const int.

IFTI uses the exact type passed, not a different type.

>

But I am aware that we can't deduce T to be 'int' because we would be losing that qualifier and further template deductions would be wrong. :/

It's not just that, there is no syntax to say to IFTI, "if you match type T, I really want you to use type U". It would actually be nice, but I don't know how it can be done.

The one exception to the "exact type" rule is for arrays and pointers, which automatically convert to their tail versions. This is a special case for the benefit of range code.

Perhaps the same rules should be done for value types?

-Steve

September 03, 2022

On Saturday, 3 September 2022 at 01:50:05 UTC, Steven Schveighoffer wrote:

>

The one exception to the "exact type" rule is for arrays and pointers, which automatically convert to their tail versions. This is a special case for the benefit of range code.

Perhaps the same rules should be done for value types?

-Steve

I think it's also done to reduce the number of template instantiations.
And I agree, it should be done for value types.

September 03, 2022

On Saturday, 3 September 2022 at 01:50:05 UTC, Steven Schveighoffer wrote:

>

It's not just that, there is no syntax to say to IFTI, "if you match type T, I really want you to use type U". It would actually be nice, but I don't know how it can be done.

-Steve

C++ solved this... maybe we could add this feature to D?

https://en.cppreference.com/w/cpp/language/class_template_argument_deduction#:~:text=x%7B2.0%2C%201%7D%3B-,User%2Ddefined%20deduction%20guides,-The%20syntax%20of

September 03, 2022

On Saturday, 3 September 2022 at 01:50:05 UTC, Steven Schveighoffer wrote:

>

It's not just that, there is no syntax to say to IFTI, "if you match type T, I really want you to use type U". It would actually be nice, but I don't know how it can be done.

The following works, the pragma only prints once.

void f(T)(const T v)
{
	pragma(msg, T); // int
	T result;
	++result;
}

void main()
{
	const int c;
	f(c);
	f(int());
}
September 03, 2022

On 9/3/22 7:09 AM, Daniel N wrote:

>

On Saturday, 3 September 2022 at 01:50:05 UTC, Steven Schveighoffer wrote:

>

It's not just that, there is no syntax to say to IFTI, "if you match type T, I really want you to use type U". It would actually be nice, but I don't know how it can be done.

C++ solved this... maybe we could add this feature to D?

https://en.cppreference.com/w/cpp/language/class_template_argument_deduction#:~:text=x%7B2.0%2C%201%7D%3B-,User%2Ddefined%20deduction%20guides,-The%20syntax%20of

Yeah, this would be useful.

-Steve

September 03, 2022

On 9/3/22 10:15 AM, Nick Treleaven wrote:

>

On Saturday, 3 September 2022 at 01:50:05 UTC, Steven Schveighoffer wrote:

>

It's not just that, there is no syntax to say to IFTI, "if you match type T, I really want you to use type U". It would actually be nice, but I don't know how it can be done.

The following works, the pragma only prints once.

void f(T)(const T v)
{
     pragma(msg, T); // int
     T result;
     ++result;
}

void main()
{
     const int c;
     f(c);
     f(int());
}

This solves the OP's problem, but I am certain that is just a toy example. What I want to see is the parameter properly typed. Either left as const if it's a reference type or stripped of const if it's a value type.

-Steve

« First   ‹ Prev
1 2 3