Jump to page: 1 2 3
Thread overview
Python-like Use of the Comma Expression
Aug 08, 2023
Vijay Nayar
Aug 08, 2023
sighoya
Aug 08, 2023
Vijay Nayar
Aug 08, 2023
sighoya
Aug 09, 2023
Quirin Schroll
Aug 10, 2023
Timon Gehr
Aug 10, 2023
Basile B.
Aug 10, 2023
Timon Gehr
Aug 10, 2023
ryuukk_
Aug 10, 2023
Basile B.
Aug 10, 2023
Timon Gehr
Aug 10, 2023
Basile B.
Aug 11, 2023
Basile B.
Aug 11, 2023
Timon Gehr
Declaration in expression - Re: Python-like Use of the Comma Expression
Aug 11, 2023
Nick Treleaven
Aug 11, 2023
Basile B.
Aug 12, 2023
Basile B.
Aug 12, 2023
Basile B.
Aug 12, 2023
Timon Gehr
Aug 12, 2023
Timon Gehr
Aug 10, 2023
Sergey
August 08, 2023

TL;DR: The comma operator has little use in D, but perhaps it could be used like in Python, to define tuple expressions.

Context:

In D, one can write a comma expression, similar to C and C++: https://dlang.org/spec/expression.html#comma_expression

Comma itself is a low precedence operator, lower than assignment. Thus, consider the following two C examples:

#include <stdio.h>

int main() {
  int a;
  a = 1, 2, 3;
  printf("%d\n", a);  // Outputs: 1
  return 0;
}
#include <stdio.h>

int main() {
  int a;
  a = (1, 2, 3);
  printf("%d\n", a);  // Outputs: 3
  return 0;
}

The usage of the comma operator like this is counter-intuitive, and almost always leads to errors. In fact, in D, using the result of a comma expression is entirely forbidden.

void main()
{
  int b = (1, 2, 3);
  assert(b == 3);
}

// onlineapp.d(3): Error: using the result of a comma expression is not allowed

The comma expression is mostly viewed as a shortcut to write multiple expressions on a single line, offering only a few benefits over just using semi-colons. For example:

int a = 1; int b = 2;
int a = 1, b = 2;  // The type doesn't have to be repeated.

Comparison with Python:

In Python, tuples are a built-in type, defined using parenthesis. E.g. (1, 2, 3) is a tuple with three numbers, while (3, "Bob", 1.2) is a tuple with a number, string, and float. https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

The parenthesis are actually optional in Python, meaning one can create a tuple like 1, 2, 3. Moreover, tuples can be used in assignment expressions as well, as illustrated in the following example:

# Assigns a=1, b=2, c=3
a, b, c = 1, 2, 3

Additionally, tuples can also be used as the return type of functions, e.g.:

# Computes the evaluation of an expression given variable values, and returns
# the result as well as the derivative.
def evalAndDiv(expression, variables):
  ...
  return result, derivative

r,d = evalAndDiv(myExpression, myVariables)

A built-in recognition of tuples also dove-tails nicely with things like foreach loops, which currently have a special implementation for iterating over associative-arrays: https://dlang.org/spec/hash-map.html#aa_example_iteration

Question: Would D benefit from using commas to define tuples rather than triggering an error when the result of a comma-expression is used? Right now, because the errors, the result of a comma expression is a bit of "unused real estate" in the language.

The easy syntax for dealing with tuples of values is especially useful in writing logic for machine-learning algorithms, and has helped Python gain and hold a strong foothold in this area, but where performance is needed, Python libraries like PyTorch turn to C++. Perhaps this could be D instead.

August 09, 2023
We had what was known as comma expression, it was removed in preparation for tuples.

So yes, its already on the todo list for us as a community! Although no time scale for this at the present I believe.
August 08, 2023

On Tuesday, 8 August 2023 at 16:47:25 UTC, Vijay Nayar wrote:

>
# Assigns a=1, b=2, c=3
a, b, c = 1, 2, 3

Don't like it because if a and b predefined with -1 and 0 I would assume to get a tuple:
(-1,0,1,2,3)

This notion also fits to default arguments we already have in D.

August 08, 2023

On Tuesday, 8 August 2023 at 19:18:42 UTC, sighoya wrote:

>

On Tuesday, 8 August 2023 at 16:47:25 UTC, Vijay Nayar wrote:

>
# Assigns a=1, b=2, c=3
a, b, c = 1, 2, 3

Don't like it because if a and b predefined with -1 and 0 I would assume to get a tuple:
(-1,0,1,2,3)

This notion also fits to default arguments we already have in D.

In Python, there is also a syntax for default arguments that is identical to D: https://www.geeksforgeeks.org/default-arguments-in-python/

Using the comma operator to define tuples would require changing the operator precedence. Rather than being at the lowest priority, with assignment being above it, it would be a higher priority than assignment.

Thus you would end up with:

(a, b, c) = (1, 2, 3)

Rather than:

(a), (b), (c = 1), 2, 3

This change in priority would be specific to top-level statements, it would not subvert the parsing of a function's argument list.

August 08, 2023

On Tuesday, 8 August 2023 at 21:00:06 UTC, Vijay Nayar wrote:

>

This change in priority would be specific to top-level statements, it would not subvert the parsing of a function's argument list.

Then we have some kind of non uniformity between function declarations and top level statements.
And what about this statement?:

int a,b=1,c;
August 09, 2023

On Tuesday, 8 August 2023 at 16:47:25 UTC, Vijay Nayar wrote:

>

TL;DR: The comma operator has little use in D, but perhaps it could be used like in Python, to define tuple expressions.

You can use something like tuple assignment, you can get pretty far simulating that with current D’s tools:

struct t
{
    import std.typecons : tuple;
    static opIndex(Ts...)(Ts args) => tuple(args);

    alias opIndexAssign = opIndexOpAssign!"";
    template opIndexOpAssign(string op)
    {
        static void opIndexOpAssign(R, Ts...)(R rhs, ref Ts lhs)
        {
            static foreach (i; 0 .. Ts.length)
            	mixin("lhs[i] ", op,"= rhs[i];");
        }
    }
}

void main()
{
    int x;
    double y;
    t[x, y] = t[2, 3.14];
    assert(x == 2);
    assert(y == 3.14);
    t[x, y] += t[3, 2.71];
    assert(x == 5);
    assert(y == 3.14 + 2.71);
}

It doesn’t work with non-copyable values, but it can be made to work with some effort (I’ve done that). Other than that, this little thing gets you quite far. A rough outline of how to support non-copyable types: Make your own tuple-like type to store the values that went into and “store” a compile-time bool array of whether the values were rvalues or lvalues. When the values are read, return originally-lvalue values by reference and move the originally-rvalue values (return by value).

Obviously it can’t do declarations and a few other things that language-level tuple support would (in all likelihood) bring.

August 10, 2023
On 8/8/23 18:47, Vijay Nayar wrote:
> 
> **Question**: Would D benefit from using commas to define tuples rather than triggering an error when the result of a comma-expression is used? Right now, because the errors, the result of a comma expression is a bit of "unused real estate" in the language.
> 
> The easy syntax for dealing with tuples of values is especially useful in writing logic for machine-learning algorithms, and has helped Python gain and hold a strong foothold in this area, but where performance is needed, Python libraries like PyTorch turn to C++. Perhaps this could be D instead.

The answer is yes, though I don't think changing the precedence fits the D grammar.

My DIP draft: https://github.com/tgehr/DIPs/blob/master/DIPs/DIP1xxx-tg.md

This will have to be adapted a bit as Walter prefers an alternative solution to using `alias this` for expansion in function arguments.

Partial implementation of the DIP (has some known limitations even in implemented features):
https://github.com/tgehr/dmd/tree/tuple-syntax

My dconf talk will be partially about this:
https://dconf.org/2023/index.html#timong
August 10, 2023
On Thursday, 10 August 2023 at 02:28:08 UTC, Timon Gehr wrote:
> On 8/8/23 18:47, Vijay Nayar wrote:
>> 
>> **Question**: Would D benefit from using commas to define tuples rather than triggering an error when the result of a comma-expression is used? Right now, because the errors, the result of a comma expression is a bit of "unused real estate" in the language.
>> 
>> The easy syntax for dealing with tuples of values is especially useful in writing logic for machine-learning algorithms, and has helped Python gain and hold a strong foothold in this area, but where performance is needed, Python libraries like PyTorch turn to C++. Perhaps this could be D instead.
>
> The answer is yes, though I don't think changing the precedence fits the D grammar.
>
> My DIP draft: https://github.com/tgehr/DIPs/blob/master/DIPs/DIP1xxx-tg.md
>

That link is for static-foreach, right one is https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md
August 10, 2023
On 8/10/23 12:41, Basile B. wrote:
> On Thursday, 10 August 2023 at 02:28:08 UTC, Timon Gehr wrote:
>> On 8/8/23 18:47, Vijay Nayar wrote:
>>>
>>> **Question**: Would D benefit from using commas to define tuples rather than triggering an error when the result of a comma-expression is used? Right now, because the errors, the result of a comma expression is a bit of "unused real estate" in the language.
>>>
>>> The easy syntax for dealing with tuples of values is especially useful in writing logic for machine-learning algorithms, and has helped Python gain and hold a strong foothold in this area, but where performance is needed, Python libraries like PyTorch turn to C++. Perhaps this could be D instead.
>>
>> The answer is yes, though I don't think changing the precedence fits the D grammar.
>>
>> My DIP draft: https://github.com/tgehr/DIPs/blob/master/DIPs/DIP1xxx-tg.md
>>
> 
> That link is for static-foreach, right one is https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md

Oops! Not sure how I accidentally switched back branches (it was 4:28 am though). Thanks for the correction!
August 10, 2023
On Thursday, 10 August 2023 at 10:41:12 UTC, Basile B. wrote:
> On Thursday, 10 August 2023 at 02:28:08 UTC, Timon Gehr wrote:
>> On 8/8/23 18:47, Vijay Nayar wrote:
>>> 
>>> **Question**: Would D benefit from using commas to define tuples rather than triggering an error when the result of a comma-expression is used? Right now, because the errors, the result of a comma expression is a bit of "unused real estate" in the language.
>>> 
>>> The easy syntax for dealing with tuples of values is especially useful in writing logic for machine-learning algorithms, and has helped Python gain and hold a strong foothold in this area, but where performance is needed, Python libraries like PyTorch turn to C++. Perhaps this could be D instead.
>>
>> The answer is yes, though I don't think changing the precedence fits the D grammar.
>>
>> My DIP draft: https://github.com/tgehr/DIPs/blob/master/DIPs/DIP1xxx-tg.md
>>
>
> That link is for static-foreach, right one is https://github.com/tgehr/DIPs/blob/tuple-syntax/DIPs/DIP1xxx-tg.md

6 years ago, wow..

Builtin tuple has been on my wishlist for a long time, time flies too fast..

« First   ‹ Prev
1 2 3