1 day ago
On 10/4/24 12:12, Dennis wrote:
> 
> 
> This doesn't work with mutable parameters btw, because pointers to class types are only covariant when they are const. (Why is that? I don't know.)


```d
Throwable a;
Object* b = &a;

*b = new Object; // modifies `a`

assert(typeid(a) is typeid(Object));
throw a; // throwing something that is not a Throwable
```

Technically it would be enough that the pointer itself does not allow writes, but `const` is transitive.
1 day ago

On Wednesday, 2 October 2024 at 08:55:15 UTC, Manu wrote:

>

Does anyone understand why this doesn't work?

void f(T)(const(T)[] x, const(T)* y) {}
void test()
{
    int*[] x;
    const int* y;
    f(x, &y);
} /* Error:
template `f` is not callable using argument types `!()(int*[],
const(int*)*)`
        Candidate is: `f(T)(const(T)[] x, const(T)* y)` */

Should this work? It looks like it should work to me.

Hi Manu, what did you do, any progress? So why don't you use 2 aliases like T, R? For example:

template func(T : const T, R : const R)
{
  void func(const(T)[] x, const(R)* y)/*
  void func(T[] x, R* y)//*/
  {
    typeid(x).writeln(": ", T.stringof);
    typeid(y).writeln(": ", R.stringof);
  }
}

import std.stdio;
void main()
{
  const
      int[] x;
  //const
      int y;
    
  func(x, &y); /* Output:
       const(int)[]: int
       const(int)*: int  */

  const
      int* [] a;
  //const
      int* b;
  
  func(a, &b); /* Output
       const(const(int)*)[]: const(int)*
       const(const(int)*)*: int*  */
}

SDB79

1 2
Next ›   Last »