Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 09, 2020 Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
void foo(int[int] bar) { // ... } Is it possible to send an empty array literal? foo( [ 0 : 2 ] ) works foo( [] ) doesn't int[int] empty; foo(empty); works but it's two lines |
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
> void foo(int[int] bar)
> {
> // ...
> }
>
>
>
> Is it possible to send an empty array literal?
>
> foo( [ 0 : 2 ] ) works
> foo( [] ) doesn't
>
> int[int] empty;
> foo(empty);
> works but it's two lines
Hmm, foo(null) seems to work, but is it correct way to do it?
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
>
> foo( [] ) doesn't
Should work in principle, but you can foo(null) to work around.
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
> void foo(int[int] bar)
> {
> // ...
> }
>
>
>
> Is it possible to send an empty array literal?
>
> foo( [ 0 : 2 ] ) works
> foo( [] ) doesn't
>
> int[int] empty;
> foo(empty);
> works but it's two lines
I always did foo((int[int]).init);
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On 7/9/20 4:04 PM, JN wrote:
> On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
>> void foo(int[int] bar)
>> {
>> // ...
>> }
>>
>>
>>
>> Is it possible to send an empty array literal?
>>
>> foo( [ 0 : 2 ] ) works
>> foo( [] ) doesn't
>>
>> int[int] empty;
>> foo(empty);
>> works but it's two lines
>
> Hmm, foo(null) seems to work, but is it correct way to do it?
>
Yes, that is correct.
-Steve
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:
>
> Yes, that is correct.
>
> -Steve
Why isn't [] accepted as an empty AA literal?
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | On 7/9/20 4:31 PM, Max Samukha wrote:
> On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:
>
>>
>> Yes, that is correct.
>
> Why isn't [] accepted as an empty AA literal?
Because it's an empty dynamic array literal.
If D were to accept an empty AA literal, I'd expect it to be [:].
-Steve
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:
> On 7/9/20 4:04 PM, JN wrote:
>> On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
>>> void foo(int[int] bar)
>>> {
>>> // ...
>>> }
>>>
>>>
>>>
>>> Is it possible to send an empty array literal?
>>>
>>> foo( [ 0 : 2 ] ) works
>>> foo( [] ) doesn't
>>>
>>> int[int] empty;
>>> foo(empty);
>>> works but it's two lines
>>
>> Hmm, foo(null) seems to work, but is it correct way to do it?
>>
>
> Yes, that is correct.
>
> -Steve
Interesting. Often in D discussion, an argument pops up that the language should be protecting against hidden breakages from API changes. This would be an example of that happening.
void foo(int[int] bar), someone calls it with a null, suddenly the signature changes to void foo(int* bar) and you will be sending a null pointer and possibly breaking the app.
|
July 09, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to JN | On 7/9/20 5:13 PM, JN wrote:
> On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:
>> On 7/9/20 4:04 PM, JN wrote:
>>> Hmm, foo(null) seems to work, but is it correct way to do it?
>>>
>>
>> Yes, that is correct.
>>
>
> Interesting. Often in D discussion, an argument pops up that the language should be protecting against hidden breakages from API changes. This would be an example of that happening.
>
> void foo(int[int] bar), someone calls it with a null, suddenly the signature changes to void foo(int* bar) and you will be sending a null pointer and possibly breaking the app.
This is a stretch.
This means you NEVER call it with an actual associative array (Which would fail to compile), and that foo never expects to get a null pointer. Even if it does break, it breaks by segfaulting and not corrupting your program.
All this, plus the author of foo cares nothing for his users, who now suddenly have non-compiling code.
-Steve
|
July 10, 2020 Re: Send empty assoc array to function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | On Thursday, 9 July 2020 at 20:08:47 UTC, Anonymouse wrote:
> On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:
>> void foo(int[int] bar)
>> {
>> // ...
>> }
>>
>>
>>
>> Is it possible to send an empty array literal?
>>
>> foo( [ 0 : 2 ] ) works
>> foo( [] ) doesn't
>>
>> int[int] empty;
>> foo(empty);
>> works but it's two lines
>
> I always did foo((int[int]).init);
Isn't that just 'null'.
I want to make note that you cannot pass null, modify the aa, and expect the parent stack to see those changes. Since you aren't using a variable that is null you are fine.
|
Copyright © 1999-2021 by the D Language Foundation