Jump to page: 1 25  
Page
Thread overview
declare-and-init operator := (decls in expressions)
Aug 28, 2005
Ben Hinkle
Aug 28, 2005
Hasan Aljudy
Aug 28, 2005
Dejan Lekic
Aug 28, 2005
Ben Hinkle
Aug 30, 2005
Oskar
Aug 31, 2005
Ben Hinkle
Aug 31, 2005
Pablo Aguilar
Sep 01, 2005
Ben Hinkle
Aug 31, 2005
Bruno Medeiros
Sep 01, 2005
Oskar Linde
Aug 28, 2005
Ben Hinkle
Aug 29, 2005
Manfred Nowak
Aug 29, 2005
Ben Hinkle
Aug 29, 2005
Manfred Nowak
Aug 29, 2005
Ben Hinkle
Aug 29, 2005
Manfred Nowak
Aug 29, 2005
Ben Hinkle
Aug 30, 2005
Manfred Nowak
Aug 30, 2005
Derek Parnell
Aug 30, 2005
Manfred Nowak
Aug 29, 2005
Ben Hinkle
Aug 30, 2005
Manfred Nowak
Aug 31, 2005
Matthias Becker
Sep 01, 2005
Manfred Nowak
Sep 03, 2005
Matthias Becker
Sep 03, 2005
Manfred Nowak
Sep 03, 2005
Ben Hinkle
Sep 04, 2005
Manfred Nowak
Sep 06, 2005
Ben Hinkle
Sep 06, 2005
Manfred Nowak
Aug 29, 2005
Charles
Aug 29, 2005
Ben Hinkle
Aug 29, 2005
AJG
Aug 30, 2005
Bruno Medeiros
Aug 31, 2005
Ben Hinkle
Aug 31, 2005
James Dunne
Aug 31, 2005
Ben Hinkle
Aug 31, 2005
Bruno Medeiros
Sep 01, 2005
Ben Hinkle
Sep 01, 2005
Manfred Nowak
Sep 01, 2005
Oskar
Sep 01, 2005
Manfred Nowak
Sep 01, 2005
Oskar Linde
Sep 01, 2005
Oskar Linde
Sep 01, 2005
Ben Hinkle
Sep 01, 2005
Manfred Nowak
Sep 07, 2005
Carlos Santander
Sep 08, 2005
Ben Hinkle
Sep 08, 2005
Ben Hinkle
August 28, 2005
I don't know if this has been proposed before but I was thinking how nice it
is to work in MATLAB (where you don't have to declare a variable's type) and
the following operator came to mind
  var := init;
is the same as
  typeof(init) var = init;

That means, for example, if ReallyLongClass is a class that

  ReallyLongClass f = new ReallyLongClass;

can be replaced with

  f := new ReallyLongClass;

It even will come in handy with lots of little variables like
  for (i:=0; i<10; i++) {...}

If := appears inside an expression (say, expr) in a statement (say, stmt)
like
  if ((p := strchar(q,'a')) != null) {
     ... use p ...
  }
then the rewritten code is
  { typeof(init) var; stmt' }
where stmt' is stmt with := replaced with = in expr.

Another example using if-else statement (from
http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/27542)
 if ((line := ReadLine()) != null) {
      printf("We got a line, Chief!");
      line = EscapeLine(line);
      DeployLine(line);
 } else {
      printf("We are out of lines, Chief!");
      line = "Emergency Line";
      DeployLine(line);
 }
is equivalent to
 {
   char[] line;
   if ((line = ReadLine()) != null) {
      printf("We got a line, Chief!");
      line = EscapeLine(line);
      DeployLine(line);
   } else {
      printf("We are out of lines, Chief!");
      line = "Emergency Line";
      DeployLine(line);
   }
 }

An example that wouldn't work (from http://www.digitalmars.com/d/archives/digitalmars/D/20481.html)

  do {
    x := getchar();
  } while (x != 'x');

since x:=getchar(); is the deepest statement enclosing the := so the declaration wouldn't be visible to the while condition.

This proposal avoids the parsing ambiguity that Stewart found with
  if ((a*b = c) != 0) {...}
since the a*b would continue to be parsed as it is today - as
multiplication.


A "real-life" example is a modified std.string.find from phobos. Notice that
it only needs declarations where some unsigned size_t array lengths are
assigned to ints.
int find(char[] s, char[] sub)
{
  int sublength = sub.length;

  if (sublength == 0)
    return 0;

  c := sub[0];
  if (sublength == 1)
  {
      p := memchr(s, c, s.length);
      if (p)
         return p - &s[0];
  }
  else
  {
      int imax = s.length - sublength + 1;

      // Remainder of sub[]
      q := &sub[1];
      sublength--;

      for (i := 0; i < imax; i++)
      {
        p := memchr(&s[i], c, imax - i);
        if (!p)
          break;
        i = p - &s[0];
        if (memcmp(p + 1, q, sublength) == 0)
          return i;
      }
  }
  return -1;
}



August 28, 2005
Seems like a handy syntactic sugar, but I'm of the opinion that too much sugar is a kind of bloat; yes, it's convenient, but it adds unnecessary complication to the syntax.
Plus, I hated that := operator from pacal.

However, maybe we could use something more consistent with the current design, like:

#typeof var = init;

Where typeof is not followed by an opening parenthesis, so a default (init) is assumed.
This is similar to the behaviour of 'extern' for example, where if it's not followed by a parenthesis, a default (D) is assumed. Same for align.

However,
#typeof var = init;
maybe misleading, as it /can/ read: "var is a variable whose tyoe is the same type as var", so maybe another keyword should be introduced:
#auto_type var = init;

or just
#type var = init;

Ben Hinkle wrote:
> I don't know if this has been proposed before but I was thinking how nice it is to work in MATLAB (where you don't have to declare a variable's type) and the following operator came to mind
>   var := init;
> is the same as
>   typeof(init) var = init;
> 
> That means, for example, if ReallyLongClass is a class that
> 
>   ReallyLongClass f = new ReallyLongClass;
> 
> can be replaced with
> 
>   f := new ReallyLongClass;
> 
> It even will come in handy with lots of little variables like
>   for (i:=0; i<10; i++) {...}
> 
> If := appears inside an expression (say, expr) in a statement (say, stmt) like
>   if ((p := strchar(q,'a')) != null) {
>      ... use p ...
>   }
> then the rewritten code is
>   { typeof(init) var; stmt' }
> where stmt' is stmt with := replaced with = in expr.
> 
> Another example using if-else statement (from http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/27542)
>  if ((line := ReadLine()) != null) {
>       printf("We got a line, Chief!");
>       line = EscapeLine(line);
>       DeployLine(line);
>  } else {
>       printf("We are out of lines, Chief!");
>       line = "Emergency Line";
>       DeployLine(line);
>  }
> is equivalent to
>  {
>    char[] line;
>    if ((line = ReadLine()) != null) {
>       printf("We got a line, Chief!");
>       line = EscapeLine(line);
>       DeployLine(line);
>    } else {
>       printf("We are out of lines, Chief!");
>       line = "Emergency Line";
>       DeployLine(line);
>    }
>  }
> 
> An example that wouldn't work (from http://www.digitalmars.com/d/archives/digitalmars/D/20481.html)
> 
>   do {
>     x := getchar();
>   } while (x != 'x');
> 
> since x:=getchar(); is the deepest statement enclosing the := so the declaration wouldn't be visible to the while condition.
> 
> This proposal avoids the parsing ambiguity that Stewart found with
>   if ((a*b = c) != 0) {...}
> since the a*b would continue to be parsed as it is today - as multiplication.
> 
> 
> A "real-life" example is a modified std.string.find from phobos. Notice that it only needs declarations where some unsigned size_t array lengths are assigned to ints.
> int find(char[] s, char[] sub)
> {
>   int sublength = sub.length;
> 
>   if (sublength == 0)
>     return 0;
> 
>   c := sub[0];
>   if (sublength == 1)
>   {
>       p := memchr(s, c, s.length);
>       if (p)
>          return p - &s[0];
>   }
>   else
>   {
>       int imax = s.length - sublength + 1;
> 
>       // Remainder of sub[]
>       q := &sub[1];
>       sublength--;
> 
>       for (i := 0; i < imax; i++)
>       {
>         p := memchr(&s[i], c, imax - i);
>         if (!p)
>           break;
>         i = p - &s[0];
>         if (memcmp(p + 1, q, sublength) == 0)
>           return i;
>       }
>   }
>   return -1;
> }
> 
> 
> 
August 28, 2005
IMHO
var := init;
is much cleaner than
#typeof var = init;
whether you like Pascal or not, You must admit that. :)

-- 
...........
Dejan Lekic
  http://dejan.lekic.org

August 28, 2005
"Hasan Aljudy" <hasan.aljudy@gmail.com> wrote in message news:det1o2$1403$1@digitaldaemon.com...
> Seems like a handy syntactic sugar, but I'm of the opinion that too much
> sugar is a kind of bloat; yes, it's convenient, but it adds unnecessary
> complication to the syntax.
> Plus, I hated that := operator from pacal.
>
> However, maybe we could use something more consistent with the current design, like:
>
> #typeof var = init;
>
> Where typeof is not followed by an opening parenthesis, so a default
> (init) is assumed.
> This is similar to the behaviour of 'extern' for example, where if it's
> not followed by a parenthesis, a default (D) is assumed. Same for align.
>
> However,
> #typeof var = init;
> maybe misleading, as it /can/ read: "var is a variable whose tyoe is the
> same type as var", so maybe another keyword should be introduced:
> #auto_type var = init;
>
> or just
> #type var = init;

There's a proposed C++ extension that uses 'auto' (or maybe 'decl' I can't
remember). I prefer the operator because
1) it's less typing (one of the goals of the idea is to cut down verbosity)
2) it is used to declare variables in expressions, too.


August 28, 2005
A variation on the idea is to treat ExpressionStatements as adding the declaration to the current scope instead of just those ExpressionStatements of the form "a:=b;". That would make a difference for statements like "a:=b:=10;" and such. I actually like this version better than the more restrictive one, I think.


August 29, 2005
Ben Hinkle wrote in news:dessc7$v3n$1@digitaldaemon.com

[...]
>   var := init;
> is the same as
>   typeof(init) var = init;
[...]

Before you start with such, please explain why declarations were introduced to some languages and then why D can get rid of them.

-manfred
August 29, 2005
"Manfred Nowak" <svv1999@hotmail.com> wrote in message news:deuk7j$2dhn$1@digitaldaemon.com...
> Ben Hinkle wrote in news:dessc7$v3n$1@digitaldaemon.com
>
> [...]
>>   var := init;
>> is the same as
>>   typeof(init) var = init;
> [...]
>
> Before you start with such, please explain why declarations were introduced to some languages and then why D can get rid of them.
>
> -manfred

It's funny you should ask that since I googled about a bit before posting.
Here's a nice little summary at artima.com about why C added types to B
http://www.artima.com/cppsource/spiritofc.html
It had nothing to do with safety at first - it was about performance
apparently.
Note - I wouldn't say the proposal is to "get rid" of declarations. The
proposal is to infer the type of the declaration from the initialization
expression. The user still has to write := instead of = which indicates
"declare and assign".


August 29, 2005
Ben Hinkle wrote in news:dev34h$2ppd$1@digitaldaemon.com

[...]
> why C added types to B
[...]
> it was about performance apparently.

1. One can very well have types, but no declarations at all. Therefore this article throws no light on my request.

2. Performance in that way, says the author, that B wasnt able to handle floats and the downsizing of the byte which were introduced by the upcoming hardware. No reason is given for the introduction of user defined types.

> Note - I wouldn't say the proposal is to "get rid" of declarations. The proposal is to infer the type of the declaration from the initialization expression. The user still has to write := instead of = which indicates "declare and assign".

In fact you are proposing some sort of micro template, because you need not know anything of the type of the RHS.

Or from another point of view, you are giving names to the
otherweise unnamed intermediate results of the actual parameters of
function calls.
  f( index:= g( h( 1)));

Whenever this is usefull: why do you need the redundance of the ":="  and the fact, that the name on the LHS is undefined?

Why doesn't it suffice to simply write
  index= g( h( 1));
and if `index' is not defined yet, then the "=" tranforms into your
":=" implicitely?

Do you fear, that someone makes a typo and therefore
  indx= g( h( 1));
goes undetected?

But if someone makes this typo and gets an error message wouldnt she/he mechanically insert the ":" to make the message go away?

And if you want to prevent typos at all, what other schemes do you propose to secure that the programmer knows what she/he types?

-manfred
August 29, 2005
"Manfred Nowak" <svv1999@hotmail.com> wrote in message news:devgi7$3mg$1@digitaldaemon.com...
> Ben Hinkle wrote in news:dev34h$2ppd$1@digitaldaemon.com
>
> [...]
>> why C added types to B
> [...]
>> it was about performance apparently.
>
> 1. One can very well have types, but no declarations at all. Therefore this article throws no light on my request.
>
> 2. Performance in that way, says the author, that B wasnt able to handle floats and the downsizing of the byte which were introduced by the upcoming hardware. No reason is given for the introduction of user defined types.

If you want more motivation see previous D posts requesting tings like this and google for anything involving words and phrases like  "type inference in C/C++", "dynamic types", "static type" etc etc. The holy grail of computer languages is to get the ease-of-use of a scripting language and that scales to large high-performance projects. I think the impact on ease-of-use is obvious. To me the only downside of the idea is any impact on large-scale development and clearly I believe it doesn't hurt large-scale development.

>> Note - I wouldn't say the proposal is to "get rid" of declarations. The proposal is to infer the type of the declaration from the initialization expression. The user still has to write := instead of = which indicates "declare and assign".
>
> In fact you are proposing some sort of micro template, because you need not know anything of the type of the RHS.
>
> Or from another point of view, you are giving names to the
> otherweise unnamed intermediate results of the actual parameters of
> function calls.
>  f( index:= g( h( 1)));
>
> Whenever this is usefull: why do you need the redundance of the ":="  and the fact, that the name on the LHS is undefined?
>
> Why doesn't it suffice to simply write
>  index= g( h( 1));
> and if `index' is not defined yet, then the "=" tranforms into your
> ":=" implicitely?
>
> Do you fear, that someone makes a typo and therefore
>  indx= g( h( 1));
> goes undetected?

yes

> But if someone makes this typo and gets an error message wouldnt she/he mechanically insert the ":" to make the message go away?

no

> And if you want to prevent typos at all, what other schemes do you propose to secure that the programmer knows what she/he types?

huh?

>
> -manfred


August 29, 2005
I love it.  However ,

>for (i:=0; i<10; i++) {...}

how does the compiler know to make i an int , as opposed to a uint / real / other compatible type?  A cast there would make it worse then explictly defining the type IMO.

Charlie

"Ben Hinkle" <ben.hinkle@gmail.com> wrote in message news:dessc7$v3n$1@digitaldaemon.com...
> I don't know if this has been proposed before but I was thinking how nice
it
> is to work in MATLAB (where you don't have to declare a variable's type)
and
> the following operator came to mind
>   var := init;
> is the same as
>   typeof(init) var = init;
>
> That means, for example, if ReallyLongClass is a class that
>
>   ReallyLongClass f = new ReallyLongClass;
>
> can be replaced with
>
>   f := new ReallyLongClass;
>
> It even will come in handy with lots of little variables like
>   for (i:=0; i<10; i++) {...}
>
> If := appears inside an expression (say, expr) in a statement (say, stmt)
> like
>   if ((p := strchar(q,'a')) != null) {
>      ... use p ...
>   }
> then the rewritten code is
>   { typeof(init) var; stmt' }
> where stmt' is stmt with := replaced with = in expr.
>
> Another example using if-else statement (from
> http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D/27542)
>  if ((line := ReadLine()) != null) {
>       printf("We got a line, Chief!");
>       line = EscapeLine(line);
>       DeployLine(line);
>  } else {
>       printf("We are out of lines, Chief!");
>       line = "Emergency Line";
>       DeployLine(line);
>  }
> is equivalent to
>  {
>    char[] line;
>    if ((line = ReadLine()) != null) {
>       printf("We got a line, Chief!");
>       line = EscapeLine(line);
>       DeployLine(line);
>    } else {
>       printf("We are out of lines, Chief!");
>       line = "Emergency Line";
>       DeployLine(line);
>    }
>  }
>
> An example that wouldn't work (from
> http://www.digitalmars.com/d/archives/digitalmars/D/20481.html)
>
>   do {
>     x := getchar();
>   } while (x != 'x');
>
> since x:=getchar(); is the deepest statement enclosing the := so the declaration wouldn't be visible to the while condition.
>
> This proposal avoids the parsing ambiguity that Stewart found with
>   if ((a*b = c) != 0) {...}
> since the a*b would continue to be parsed as it is today - as
> multiplication.
>
>
> A "real-life" example is a modified std.string.find from phobos. Notice
that
> it only needs declarations where some unsigned size_t array lengths are
> assigned to ints.
> int find(char[] s, char[] sub)
> {
>   int sublength = sub.length;
>
>   if (sublength == 0)
>     return 0;
>
>   c := sub[0];
>   if (sublength == 1)
>   {
>       p := memchr(s, c, s.length);
>       if (p)
>          return p - &s[0];
>   }
>   else
>   {
>       int imax = s.length - sublength + 1;
>
>       // Remainder of sub[]
>       q := &sub[1];
>       sublength--;
>
>       for (i := 0; i < imax; i++)
>       {
>         p := memchr(&s[i], c, imax - i);
>         if (!p)
>           break;
>         i = p - &s[0];
>         if (memcmp(p + 1, q, sublength) == 0)
>           return i;
>       }
>   }
>   return -1;
> }
>
>
>


« First   ‹ Prev
1 2 3 4 5