www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Template code-blocks

reply Sha Chancellor <schancel pacific.net> writes:
I noticed a lot of code now is using templates on a per-function basis.  
Fine and dandy.  But I must ask why we cannot do:


# template box(T : Object) {
#     T box(T t) {
#         return t;
#     }
# }

as:

# template box(T : Object)
# T box(T t) {
#         return t;
# }

Everywhere else in C, and C++ scoping was done based on the next entity 
in line. (Aside from functions)

IE:  a line is an entity, but a code block can make multiple lines one 
entity also.


Such as:

# if( foo ) 
#    continue;

or

# if( foo )
# {
#    numberOfFoos++;
#    continue;
# }


A function is an entity, shouldn't templates allow one liners?
Jul 30 2004
next sibling parent Berin Loritsch <bloritsch d-haven.org> writes:
<rant mode="on">

A little off topic, but I have to ask why the generic class name can't
be more than one character long?  It is legal syntax to use a longer
name, and it can give better insight into what is going on.  For
example:

template interface IMap(K, V) {
      V get(K k);
      void put(K k, V v);
}

would become:

template interface IMap(key_t, value_t) {
      value_t get(key_t key);
      void put(key_t key, value_t value);
}

It sure would make understanding the code (esp. examples) much easier.

It's just something that has bothered me since learning C++ STL.

It really helps out making the error messages easier to understand when
you are writing the template.

After all, seeing an illegal type message with the type being reported
as "T" is not nearly as informative as one with the type being
"element_t" or some such thing.

</rant>

Sha Chancellor wrote:
 I noticed a lot of code now is using templates on a per-function basis.  
 Fine and dandy.  But I must ask why we cannot do:
 
 
 # template box(T : Object) {
 #     T box(T t) {
 #         return t;
 #     }
 # }
 
 as:
 
 # template box(T : Object)
 # T box(T t) {
 #         return t;
 # }
 
 Everywhere else in C, and C++ scoping was done based on the next entity 
 in line. (Aside from functions)
 
 IE:  a line is an entity, but a code block can make multiple lines one 
 entity also.
 
 
 Such as:
 
 # if( foo ) 
 #    continue;
 
 or
 
 # if( foo )
 # {
 #    numberOfFoos++;
 #    continue;
 # }
 
 
 A function is an entity, shouldn't templates allow one liners?

Jul 30 2004
prev sibling parent reply Lars Ivar Igesund <larsivar igesund.net> writes:
Sha Chancellor wrote:

 I noticed a lot of code now is using templates on a per-function basis.  
 Fine and dandy.  But I must ask why we cannot do:
 
 
 # template box(T : Object) {
 #     T box(T t) {
 #         return t;
 #     }
 # }
 
 as:
 
 # template box(T : Object)
 # T box(T t) {
 #         return t;
 # }
 
 Everywhere else in C, and C++ scoping was done based on the next entity 
 in line. (Aside from functions)
 
 IE:  a line is an entity, but a code block can make multiple lines one 
 entity also.
 
 
 Such as:
 
 # if( foo ) 
 #    continue;
 
 or
 
 # if( foo )
 # {
 #    numberOfFoos++;
 #    continue;
 # }
 
 
 A function is an entity, shouldn't templates allow one liners?

I guess it has to do with parsing ease. Anyway, the # if (foo) # continue; case isn't a one-liner, but an one-statement thingy. You can write # if (foo) continue; if you want. Lars Ivar Igesund
Jul 30 2004
parent Sha Chancellor <schancel pacific.net> writes:
In article <cee84k$nn$1 digitaldaemon.com>,
 Lars Ivar Igesund <larsivar igesund.net> wrote:

 I guess it has to do with parsing ease. Anyway, the
 
 # if (foo)
 #   continue;
 
 case isn't a one-liner, but an one-statement thingy. You can write
 
 # if (foo) continue;
 
 if you want.
 
 Lars Ivar Igesund

You know what i mean.... You could also do: if(foo) { bar; bob; joe; fred; continue; } Bah! - Sha
Jul 30 2004