Thread overview | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 20, 2016 Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful: ---- auto ref front() { // Returns 2 values, each value is returned by reference if possible return (a.front, b.front); } ---- Mir libs will use pointers for now. This is one of reasons why `zip` is slow. The new feature also significantly extends std.range and std.algorithm functionality. This thread was forked from http://forum.dlang.org/post/acdwfbirvoxzrsfyltqd@forum.dlang.org I am not good in DIPs and hope someone is interested in this feature too |
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On Tuesday, 20 December 2016 at 13:47:50 UTC, Ilya Yaroshenko wrote:
> One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful:
>
> ----
> auto ref front()
> {
> // Returns 2 values, each value is returned by reference if possible
> return (a.front, b.front);
> }
> ----
>
> Mir libs will use pointers for now. This is one of reasons why `zip` is slow.
> The new feature also significantly extends std.range and std.algorithm functionality.
>
> This thread was forked from
> http://forum.dlang.org/post/acdwfbirvoxzrsfyltqd@forum.dlang.org
>
> I am not good in DIPs and hope someone is interested in this feature too
Do tuples work for this?
|
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nicholas Wilson | On Tuesday, 20 December 2016 at 14:04:26 UTC, Nicholas Wilson wrote:
> On Tuesday, 20 December 2016 at 13:47:50 UTC, Ilya Yaroshenko wrote:
>> One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful:
>>
>> ----
>> auto ref front()
>> {
>> // Returns 2 values, each value is returned by reference if possible
>> return (a.front, b.front);
>> }
>> ----
>>
>> Mir libs will use pointers for now. This is one of reasons why `zip` is slow.
>> The new feature also significantly extends std.range and std.algorithm functionality.
>>
>> This thread was forked from
>> http://forum.dlang.org/post/acdwfbirvoxzrsfyltqd@forum.dlang.org
>>
>> I am not good in DIPs and hope someone is interested in this feature too
>
> Do tuples work for this?
No, tuples stores either value or pointer. If it stores pointer then it is not safe and it is not CTFE.
|
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On Tuesday, 20 December 2016 at 13:47:50 UTC, Ilya Yaroshenko wrote:
> One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful:
>
> ----
> auto ref front()
> {
> // Returns 2 values, each value is returned by reference if possible
> return (a.front, b.front);
> }
Nested Voldemort structs? Horcruxified, to work around the symbol name length explosion. I always imagined allowing for multiple values (in multiple fields) was part of the point behind them.
For instance, consider the several discrete pieces of data you get from the Result of std.regex matching. (Not all of it is via opIndex.)
|
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On 12/20/2016 5:47 AM, Ilya Yaroshenko wrote: > One good thing for safety and CTFE is allow multiple return value. In > combination with `auto ref` it is _very_ powerful: > > ---- > auto ref front() > { > // Returns 2 values, each value is returned by reference if possible > return (a.front, b.front); > } > ---- http://dlang.org/phobos/std_typecons.html#.tuple auto ref front() { return tuple(a.front, b.front); } |
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright Attachments:
| Walter Bright via Digitalmars-d <digitalmars-d@puremagic.com> napsal Út, pro 20, 2016 v 3∶47 : > On 12/20/2016 5:47 AM, Ilya Yaroshenko wrote: >> One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful: >> >> ---- >> auto ref front() >> { >> // Returns 2 values, each value is returned by reference if >> possible >> return (a.front, b.front); >> } >> ---- > > http://dlang.org/phobos/std_typecons.html#.tuple > > auto ref front() { > return tuple(a.front, b.front); > } https://github.com/dlang/phobos/blob/master/std/typecons.d#L1686 I do not see any auto ref at code, so I do not belive this will work |
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Tuesday, 20 December 2016 at 14:47:28 UTC, Walter Bright wrote:
> On 12/20/2016 5:47 AM, Ilya Yaroshenko wrote:
>> One good thing for safety and CTFE is allow multiple return value. In
>> combination with `auto ref` it is _very_ powerful:
>>
>> ----
>> auto ref front()
>> {
>> // Returns 2 values, each value is returned by reference if possible
>> return (a.front, b.front);
>> }
>> ----
>
> http://dlang.org/phobos/std_typecons.html#.tuple
>
> auto ref front() {
> return tuple(a.front, b.front);
> }
Tuples can not work with auto ref. This is a reason for this thread.
Proof:
-----
import std.typecons: tuple;
int[] a = [1,2,3];
int[] b = [1,2,3];
auto ref front() {
import std.range: front;
return tuple(a.front, b.front);
}
void main()
{
front()[0] = 1000;
import std.stdio;
writeln(front()[0]);
}
-----
Output: 1
-----
As you can see the output should be 1000, but it is 1.
It can be solved with pointers, and Mir will have their own tuples which use pointers. But it is not good because:
1. Pointers are not CTFE-able.
2. Pointers are not safe.
|
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On Tuesday, 20 December 2016 at 14:56:37 UTC, Ilya Yaroshenko wrote:
>
> As you can see the output should be 1000, but it is 1.
>
I was a little confused by what you meant, but the following main is a little more obvious to me
void main()
{
auto c = front();
c[0] = 1000;
import std.stdio;
writeln(c[0]); //prints 1000
writeln(a[0]); //prints 1
}
|
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On Tuesday, 20 December 2016 at 13:47:50 UTC, Ilya Yaroshenko wrote: > One good thing for safety and CTFE is allow multiple return value. In combination with `auto ref` it is _very_ powerful: > > auto ref front() > { > // Returns 2 values, each value is returned by reference if possible > return (a.front, b.front); > } I'd love to have this syntax aswell. > I am not good in DIPs and hope someone is interested in this feature too DIP-32 has been dormant since 2013. I've been waiting for builtin tuples ever since I started using D. I believe the community agrees upon that we want the Python-style syntax to be used. But in order for this to be integrated we must wait out the deprecation of the C-inherited comma expression to become an error. Correct me if I'm wrong. See: - https://wiki.dlang.org/DIP32 - http://forum.dlang.org/post/kj44fs$2iil$1@digitalmars.com Didn't Kenji Hara implement one of his proposals in DMD?...I might remember wrong. |
December 20, 2016 Re: Multiple return value as requirements for safety and performance | ||||
---|---|---|---|---|
| ||||
Posted in reply to jmh530 | On Tuesday, 20 December 2016 at 15:36:27 UTC, jmh530 wrote:
> On Tuesday, 20 December 2016 at 14:56:37 UTC, Ilya Yaroshenko wrote:
>
>>
>> As you can see the output should be 1000, but it is 1.
>>
>
>
> I was a little confused by what you meant, but the following main is a little more obvious to me
>
> void main()
> {
> auto c = front();
> c[0] = 1000;
> import std.stdio;
> writeln(c[0]); //prints 1000
> writeln(a[0]); //prints 1
> }
c is value. You can modify it with tuples. Tuples can be returned.
This thread is about mutiple values returned by _reference_. Tuples can not do it, only pointers, but they are not ctfeable and safe
|
Copyright © 1999-2021 by the D Language Foundation