Thread overview
How should I return multiple const values from a function?
Jan 02, 2023
Charles Hixson
Jan 02, 2023
Paul Backus
Jan 02, 2023
Charles Hixson
Jan 03, 2023
Paul Backus
Jan 03, 2023
Charles Hixson
Jan 03, 2023
Salih Dincer
Jan 03, 2023
Salih Dincer
Jan 02, 2023
Charles Hixson
January 02, 2023
I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them.  I don't want the returned values to be able to be used to modify the values held in the table.  const out is an illegal parameter type.  dup doesn't work on ints.

So I tried to return a tuple, but cast(const) doesn't work on strings.  (Error: cannot implicitly convert expression `tuple(k, v)` of type `Tuple!(string, string)` to `Tuple!(const(string), const(string))`  Sometimes it remembers that I've done the cast, and sometimes it doesn't.  The code was: return    tuple (cast(const)(Key.init), cast(const)(Val.init));  I've no idea why it was talking about type `Tuple!(string, string)`, since in the test the types were (string, uint).

I feel like I'm overlooking something obvious, but I've no idea what.  I really just wanted to return dups, but those don't work if one of the types might be an int.  (Though I should have been trying to return idups.  O, well.)

-- 
Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.

January 02, 2023
On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote:
> I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them.

It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you.
January 02, 2023
On 1/2/23 15:14, Paul Backus via Digitalmars-d-learn wrote:
> On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote:
>> I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them.
>
> It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you.

I don't really know where to start.  dup doesn't reliably work on template parameters.  Assignment returns things that may have internal pointers.  The code is 5-6 lines of testing this approach or that, and every single one of them has failed.  Some approaches will work with strings, but not with ints, some with ints but not with strings, some may return pointers to internal data.  What I'm trying to do is return data that will be const or immutable from a function.  They syntax I wanted was something like:

bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true;    }

but that fails six or seven different ways.

-- 
Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.

January 02, 2023
On 1/2/23 15:14, Paul Backus via Digitalmars-d-learn wrote:
> On Monday, 2 January 2023 at 22:53:13 UTC, Charles Hixson wrote:
>> I want to return values of the template parameter type, so there doesn't seem to be any way to dup or idup them.
>
> It's hard to say where exactly you're going wrong if you only post the error message, without the code that produced it. If you post your code (or a simplified version with the same problem), I'm sure someone will be able to help you.
O. Key and Val are template parameters, and Key has a less method defined.

-- 
Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.

January 03, 2023
On Monday, 2 January 2023 at 23:25:48 UTC, Charles Hixson wrote:
>
> They syntax I wanted was something like:
>
> bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true;    }

This works for me:

    import std.typecons;

    auto func(Key, Value)(Key k, Value v)
    {
        return Tuple!(const(Key), const(Value))(k, v);
    }

    void main()
    {
        string k;
        uint v;
        auto result = func(k, v);

        static assert(is(typeof(result[0]) == const(string)));
        static assert(is(typeof(result[1]) == const(uint)));
    }
January 02, 2023
On 1/2/23 17:56, Paul Backus via Digitalmars-d-learn wrote:
> return Tuple!(const(Key), const(Value))(k, v); 

Great!  OK, now the code is:

    auto    findFirst ()
    {    if    (root is null)
        {    Key k    =    Key.init;
            Val v    =    Val.init;
            return Tuple!(const(Key), const(Val))(k, v);
        }
        Node    n    =    root;
        while (n.lft !is null)    n    =    n.lft;
        auto k    =    cast(const)(n.key);
        auto v    =    cast(const)(n.val);
        return Tuple!(const(Key), const(Val))(k, v);
    }    //nd    bool    findFirst (out Key k, out Val v)
I couldn't show that previously because the various things I was trying were too different.

-- 
Javascript is what you use to allow third part programs you don't know anything about and doing you know not what to run on your computer.

January 03, 2023

On Tuesday, 3 January 2023 at 01:56:10 UTC, Paul Backus wrote:

>

On Monday, 2 January 2023 at 23:25:48 UTC, Charles Hixson wrote:

>

They syntax I wanted was something like:

bool func (const out Key k, const out Val v) { k = this.key.dup; v = this.val.dup; return true;    }

This works for me:

template Fun(Key, Value)
{
  import std.typecons;

  alias Tup = Tuple!(const(Key), const(Value));

  enum Fun = (Key k, Value v) => Tup(k, v);/*
  auto Fun(Key k, Value v) {
   return Tup(k, v);
  }//*/
}
string k;
uint v;

auto res = Fun(k, v);

static assert(is(typeof(res[0]) == const(string))); // false
static assert(is(typeof(res[1]) == const(uint))); //false

I rewrote the code to return the enum. However, static asserts return false. What could this cause?

P.S. Actually the code works when we don't use enum.

SDB@79

January 03, 2023

On Tuesday, 3 January 2023 at 07:41:46 UTC, Salih Dincer wrote:

>

P.S. Actually the code works when we don't use enum.

My mistake, I forgot that enum cannot be inferred!

Very very sorry, suppose I didn't intervene 😀 Also this has worked:

void main() {
  template Fun(Key, Value)
  {
    import std.typecons;
    alias Tup = Tuple!(const(Key), const(Value));
    enum Fun = (Key k, Value v) => Tup(k, v);
  }

  string k;
  uint v;

  auto res = Fun!(string, uint)(k, v);

  assert(
    is(typeof(res[0]) == const(string)) &&
    is(typeof(res[1]) == const(uint))
  );
}

SDB@79