Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 12, 2004 inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Inspired by the change in D-0.107: "InExpressions now, instead of returning a bit, return a pointer to the associative array element if the key is present, null if it is not. This obviates the need for many double lookups." I wonder how many look-ups this code needs: int[int] map; .. map[key] += 2; Do we need two separate look-ups for read/write? Can we have inout return type for the index operator (and in general for any functions)? Is 'inout' same as reference type in C++? Why right now it's only allowed in passing function parameters? |
December 13, 2004 Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to no | <no@where.com> wrote in message news:cpi1ob$2fjt$1@digitaldaemon.com... > Inspired by the change in D-0.107: > > "InExpressions now, instead of returning a bit, return a pointer to the associative array element if the key is present, null if it is not. This obviates the need for many double lookups." > > I wonder how many look-ups this code needs: > > int[int] map; > .. > map[key] += 2; Just one. Compile it, run obj2asm, and see! > Do we need two separate look-ups for read/write? Can we have inout return type > for the index operator (and in general for any functions)? That might be a good idea. I'll have to think about it some more. > Is 'inout' same as reference type in C++? More or less, yes. > Why right now it's only allowed in passing function parameters? |
December 13, 2004 Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | "Walter" <newshound@digitalmars.com> wrote in message news:cpj3iv$e8n$2@digitaldaemon.com... > > <no@where.com> wrote in message news:cpi1ob$2fjt$1@digitaldaemon.com... > > Inspired by the change in D-0.107: > > > > "InExpressions now, instead of returning a bit, return a pointer to the associative array element if the key is present, null if it is not. This obviates the need for many double lookups." > > > > I wonder how many look-ups this code needs: > > > > int[int] map; > > .. > > map[key] += 2; > > Just one. Compile it, run obj2asm, and see! > > > Do we need two separate look-ups for read/write? Can we have inout return > type > > for the index operator (and in general for any functions)? > > That might be a good idea. I'll have to think about it some more. This is a great idea! Please, please think about it. :) > > > Is 'inout' same as reference type in C++? > > More or less, yes. > > > Why right now it's only allowed in passing function parameters? > > |
December 13, 2004 Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <cpj3iv$e8n$2@digitaldaemon.com>, Walter says... ><no@where.com> wrote in message news:cpi1ob$2fjt$1@digitaldaemon.com... >> Do we need two separate look-ups for read/write? Can we have inout return >type >> for the index operator (and in general for any functions)? > >That might be a good idea. I'll have to think about it some more. > What might the syntax for this be? First thought is something like: # inout int foo(int x) { } -- Chris Sauls |
December 14, 2004 Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | In article <cpj3iv$e8n$2@digitaldaemon.com>, Walter says... > > ><no@where.com> wrote in message news:cpi1ob$2fjt$1@digitaldaemon.com... >> Inspired by the change in D-0.107: >> >> Do we need two separate look-ups for read/write? Can we have inout return >type >> for the index operator (and in general for any functions)? > >That might be a good idea. I'll have to think about it some more. > Can't wait to have it! So is opIndexAssign gonna be deprecated maybe?? -tetsuya |
December 14, 2004 alias type? [was] Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Sauls | I'm thinking along the lines of introducing a full alias (like a C++ reference) type into D. In C++, a variable of reference type can only be init-ed at declaration: // C++ code void foo() { int i, j; int& r; // error, `r' declared as reference but not initialized int& r = i; // OK. } I'm think about some re-bindable reference type, let's all it alias type: // imaginary D code void foo() { int i, j; alias int r; // decl a variable r of alias type r alias i; // alias is an operator, r is now alias-ed to i; r = 3; // assert( i == 3 ); r alias j; // r is now alias-ed to j; r = 3; // assert( j == 3 ); } This will help a lot to improve the code, as in the case of map example to reduce the unnessary look-ups. Comments? yqz In article <cpl3kl$2qha$1@digitaldaemon.com>, Chris Sauls says... > >In article <cpj3iv$e8n$2@digitaldaemon.com>, Walter says... >><no@where.com> wrote in message news:cpi1ob$2fjt$1@digitaldaemon.com... >>> Do we need two separate look-ups for read/write? Can we have inout return >>type >>> for the index operator (and in general for any functions)? >> >>That might be a good idea. I'll have to think about it some more. >> > >What might the syntax for this be? First thought is something like: > ># inout int foo(int x) { } > >-- Chris Sauls |
December 14, 2004 Re: alias type? [was] Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to no | no@where.com wrote:
> I'm thinking along the lines of introducing a full alias
> (like a C++ reference) type into D.
Sounds a lot like the C++ references, just even more confusing ?
I'm trying to see how this is easier than pointers... But I fail ?
But maybe the world could need some more sugar, even if syntactic.
Const references are kinda nice, but the others make for nasty bugs.
IMHO.
--anders
|
December 15, 2004 Re: alias type? [was] Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | >> I'm thinking along the lines of introducing a full alias >> (like a C++ reference) type into D. > >Sounds a lot like the C++ references, just even more confusing ? To me his idea sounds simple. You have tow operators '=' and 'alias'. '=' means value-assignment, 'alias' means reference-assignment. But I'm not sure, if we should use alias fo this. Maybe somethings else would be more clear: # void foo() # { # int i, j; # ref int r; # # r := i; // r is now refering to i # r = 3; // assert( i == 3 ); # # r := j; // r is now refering to j; # r = 3; // assert( j == 3 ); # } I just replaced on alias with ref and the alias-operator with := and to me it looks pretty understandable. >I'm trying to see how this is easier than pointers... But I fail ? # void foo() # { # int i, j; # int *r; # # r = i; // r is now refering to i # *r = 3; // assert( i == 3 ); # # r = j; // r is now refering to j; # *r = 3; // assert( j == 3 ); # } Of course, it's the same as pointers, but you don't have to dereference it. -- Matthias Becker |
December 15, 2004 Re: alias type? [was] Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matthias Becker | Matthias Becker wrote: >>Sounds a lot like the C++ references, just even more confusing ? > > To me his idea sounds simple. You have tow operators '=' and 'alias'. '=' means > value-assignment, 'alias' means reference-assignment. > But I'm not sure, if we should use alias fo this. Actually alias sort of works for this already, just that you can't reuse the same name twice: > alias i r; > alias j r; "declaration main.r is already defined" > Maybe somethings else would be > more clear: > > # void foo() > # { > # int i, j; > # ref int r; > # # r := i; // r is now refering to i > # r = 3; // assert( i == 3 ); > # # r := j; // r is now refering to j; > # r = 3; // assert( j == 3 ); > # } > > I just replaced on alias with ref and the alias-operator with := and to me it > looks pretty understandable. Unless you had to look at Pascal or Ada at some point in your life... They use := for assignment (=) and a single = for equality (==). >>I'm trying to see how this is easier than pointers... But I fail ? > > # void foo() > # { > # int i, j; > # int *r; > # # r = i; // r is now refering to i > # *r = 3; // assert( i == 3 ); > # # r = j; // r is now refering to j; > # *r = 3; // assert( j == 3 ); > # } You meant to write "r = &i", since the above code does not compile. > Of course, it's the same as pointers, but you don't have to dereference it. I know the C++ references. No dereferencing, no nullpointers. But still with a high potential for confusion, if used heavily... I think the inout parameters and lack of copy constructors makes D not need them, but maybe some kind of references would be good ? --anders |
December 15, 2004 Re: alias type? [was] Re: inout (return) type modifier? | ||||
---|---|---|---|---|
| ||||
Posted in reply to no | no@where.com wrote: > I'm think about some re-bindable reference type, let's all it alias type: > > // imaginary D code > void foo() > { > int i, j; > alias int r; // decl a variable r of alias type > > r alias i; // alias is an operator, r is now alias-ed to i; > r = 3; // assert( i == 3 ); > > r alias j; // r is now alias-ed to j; > r = 3; // assert( j == 3 ); > } > > This will help a lot to improve the code, as in the case of map example to > reduce the unnessary look-ups. If it *was* implemented, shouldn't it be done the same way as it's done with arguments, i.e. with the "in", "inout" and "out" designators ? > int i, j; > > inout int ri = i; > ri = 3; > assert(i == 3); > > inout int rj = j; > rj = 3; > assert(j == 3); With 'out' first initializing the value, as it works with function parameters right now ? http://www.digitalmars.com/d/function.html As with functions, 'in' would have no effect whatsoever, making "r" into just a copy of i. ("in int r = i;" being same as "int r = i;") I'm not sure what the use of such references would be, outside of parameters, though ? **** If the new declaration *was* just an alias, then maybe D needs "unalias" like the "sh" shell has ? > void main() > { > int i, j; > > alias i r; > r = 3; > assert(i == 3); > > unalias r; // <-- new compiler directive > > alias j r; > r = 3; > assert(j == 3); > } Where unalias simply makes a previous alias declaration "disappear" from the name space ? Without it, it throws an error when reusing : > declaration main.r is already defined --anders |
Copyright © 1999-2021 by the D Language Foundation