digitalmars.D.learn - Send empty assoc array to function
- JN (10/10) Jul 09 2020 void foo(int[int] bar)
- JN (2/12) Jul 09 2020 Hmm, foo(null) seems to work, but is it correct way to do it?
- Steven Schveighoffer (3/22) Jul 09 2020 Yes, that is correct.
- Max Samukha (3/5) Jul 09 2020 Why isn't [] accepted as an empty AA literal?
- Steven Schveighoffer (4/10) Jul 09 2020 Because it's an empty dynamic array literal.
- Max Samukha (9/15) Jul 10 2020 Just as typeof(null) is a subtype of all nullable types, you
- Max Samukha (2/3) Jul 10 2020 an unfortunate
- Steven Schveighoffer (7/24) Jul 10 2020 to!string is from the library, pragma(msg) is the compiler. The latter
- JN (8/30) Jul 09 2020 Interesting. Often in D discussion, an argument pops up that the
- Steven Schveighoffer (9/24) Jul 09 2020 This is a stretch.
- Mike Parker (3/9) Jul 09 2020 Meh. You could say the same about foo(int[]), or foo(SomeClass).
- Mike Parker (7/19) Jul 09 2020 Besides, when it comes to breakage, the discussions I've seen are
- JN (11/14) Jul 10 2020 Oh, that actually makes sense. I always thought assoc arrays are
- Max Samukha (2/3) Jul 09 2020 Should work in principle, but you can foo(null) to work around.
- Anonymouse (2/12) Jul 09 2020 I always did foo((int[int]).init);
- Jesse Phillips (5/22) Jul 09 2020 Isn't that just 'null'.
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
Jul 09 2020
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 linesHmm, foo(null) seems to work, but is it correct way to do it?
Jul 09 2020
On 7/9/20 4:04 PM, JN wrote:On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:Yes, that is correct. -Stevevoid 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 linesHmm, foo(null) seems to work, but is it correct way to do it?
Jul 09 2020
On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:Yes, that is correct. -SteveWhy isn't [] accepted as an empty AA literal?
Jul 09 2020
On 7/9/20 4:31 PM, Max Samukha wrote:On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:Because it's an empty dynamic array literal. If D were to accept an empty AA literal, I'd expect it to be [:]. -SteveYes, that is correct.Why isn't [] accepted as an empty AA literal?
Jul 09 2020
On Thursday, 9 July 2020 at 21:04:57 UTC, Steven Schveighoffer wrote:Just as typeof(null) is a subtype of all nullable types, you could make typeof([]) a subtype of both AAs and dynamic arrays. [:] could still be made a specifically AA literal. BTW, writeln((int[int]).init) prints "[]" (to!string((V[K]).init) == "[]"), but pragma(msg, (int[int]).init) - the more general 'null' ((V[K]).init.stringof == "null"), which is a unfortunate inconsistency.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
Jul 10 2020
On Friday, 10 July 2020 at 08:15:24 UTC, Max Samukha wrote:a unfortunatean unfortunate
Jul 10 2020
On 7/10/20 4:15 AM, Max Samukha wrote:On Thursday, 9 July 2020 at 21:04:57 UTC, Steven Schveighoffer wrote:Sure it's possible. But I don't see it happening.Just as typeof(null) is a subtype of all nullable types, you could make typeof([]) a subtype of both AAs and dynamic arrays. [:] could still be made a specifically AA literal.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 [:].BTW, writeln((int[int]).init) prints "[]" (to!string((V[K]).init) == "[]"), but pragma(msg, (int[int]).init) - the more general 'null' ((V[K]).init.stringof == "null"), which is a unfortunate inconsistency.to!string is from the library, pragma(msg) is the compiler. The latter is authoratative where the compiler is concerned. to!string probably should be changed. [] should be printed for initialized but empty AAs, null should be printed for .init. -Steve
Jul 10 2020
On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote:On 7/9/20 4:04 PM, JN wrote: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.On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:Yes, that is correct. -Stevevoid 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 linesHmm, foo(null) seems to work, but is it correct way to do it?
Jul 09 2020
On 7/9/20 5:13 PM, JN wrote:On Thursday, 9 July 2020 at 20:24:11 UTC, Steven Schveighoffer wrote: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. -SteveOn 7/9/20 4:04 PM, JN wrote: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.Hmm, foo(null) seems to work, but is it correct way to do it?Yes, that is correct.
Jul 09 2020
On Thursday, 9 July 2020 at 21:13:49 UTC, JN wrote: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.Meh. You could say the same about foo(int[]), or foo(SomeClass). AAs are reference types. Reference type instances can be null.
Jul 09 2020
On Friday, 10 July 2020 at 03:59:37 UTC, Mike Parker wrote:On Thursday, 9 July 2020 at 21:13:49 UTC, JN wrote:Besides, when it comes to breakage, the discussions I've seen are about breaking changes in the language/compiler. There are opportunities in most (if not all) programming languages to introduce silent breakage when a library maintainer changes a public-facing API. And when it happens, that's not the fault of the language, but the library maintainer.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.Meh. You could say the same about foo(int[]), or foo(SomeClass). AAs are reference types. Reference type instances can be null.
Jul 09 2020
On Friday, 10 July 2020 at 03:59:37 UTC, Mike Parker wrote:Meh. You could say the same about foo(int[]), or foo(SomeClass). AAs are reference types. Reference type instances can be null.Oh, that actually makes sense. I always thought assoc arrays are value types. Anyway, even if they are reference type, I still would consider [] and null different types of values. [] conveys to me that the object exists, but is empty. null conveys to me that the object exists and cannot be used. int[int] a = null; a[5] = 6; This kind of code just looks weird... yes, I know the " = null " part is excessive, but still.
Jul 10 2020
On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote:foo( [] ) doesn'tShould work in principle, but you can foo(null) to work around.
Jul 09 2020
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 linesI always did foo((int[int]).init);
Jul 09 2020
On Thursday, 9 July 2020 at 20:08:47 UTC, Anonymouse wrote:On Thursday, 9 July 2020 at 19:53:42 UTC, JN wrote: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.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 linesI always did foo((int[int]).init);
Jul 09 2020