October 04
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.
October 05

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

October 09
On Sat, 5 Oct 2024 at 10:26, Salih Dincer via Digitalmars-d < digitalmars-d@puremagic.com> wrote:

> On Wednesday, 2 October 2024 at 08:55:15 UTC, Manu wrote:
> > Does anyone understand why this doesn't work?
> > ```d
> > 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:
>
> ```d
> 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
>


I did nothing. I just did work-around and moved on... but I think this is
definitely a bug.
Just another random edge case in a sea of annoying edge cases! :P


October 09

On Wednesday, 9 October 2024 at 07:02:32 UTC, Manu wrote:

>

I did nothing. I just did work-around and moved on... but I think this is
definitely a bug.
Just another random edge case in a sea of annoying edge cases! :P

If it's not too special, in what application will you use this? Please consider me as an amateur who is new to D. Because I am a programmer who almost never uses const. I have a hard time understanding what the benefits of what you are doing are. Okay, this code (single template parameter) looks very appealing to me too:

template S(T)
{
  auto S(const(T)[] data, const(T) *sum)
    => Result(data, sum);

  struct Result {
    const(T)[] data;

    const(T) *total;
    auto sum() const
      => *total;

    string id;
    auto toString() const
      => format("%s: %s, %s%s", id, sum,
                      T.stringof, data);
  }
}

import std;
void main()
{
  auto arr = [1, 2, 3, 4, 5];
  auto sum = arr.sum;
  auto s = arr.S(&sum);
       s.id = "Sum of the Elements";
  s.writeln;// Sum of the Elements: 15, int[1, 2, 3, 4, 5]
  ++sum;
  assert(s.sum == 16);
}

But even if sum cannot be changed in the returned Result, since the data array is already copied, doesn't the fact that it is const change anything?

SDB@79

1 2
Next ›   Last »