Results 1 to 5 of 5

Thread: ASH function templates

  1. #1
    Developer
    Join Date
    Aug 2009
    Posts
    2,700

    Default ASH function templates

    C++ has a notion of templates, e.g.
    PHP Code:
    template <typename T>
    T max(T aT b) {
      return 
    b;
    }
    ...
    max<int>(413); // returns 13 
    The Java generics API is somewhat more concise (ignoring things like inheritance which just don't exist in ASH):
    PHP Code:
    <TT max(T aT b) {
        return 
    b;

    Currently, I find myself duplicating code to change types of maps, e.g.
    PHP Code:
    string as_list(boolean[stringstuff) {
      
    buffer s;
      foreach 
    thing in stuff {
        if (
    s.length() > 0) {
          
    s.append(", ");
        }
        
    s.append(thing);
      }
      return 
    s.to_string();
    }

    string as_list(boolean[itemstuff) {
      
    buffer s;
      foreach 
    thing in stuff {
        if (
    s.length() > 0) {
          
    s.append(", ");
        }
        
    s.append(thing);
      }
      return 
    s.to_string();
    }
    ...
    as_list(item_array);
    as_list(string_array); 
    I'd be happier if I could just write
    PHP Code:
    <Tstring as_list(boolean[Tstuff) {
        ...
    }
    ...
    as_list<item>(item_array);
    as_list<string>(string_array); 
    The functional programmer in me also notes that this could be combined with call to implement various well-known functional programming paradigms, e.g.
    PHP Code:
    <AA[intfilter(string function_nameA[intinput) {
      
    A[intoutput;
      foreach 
    i in input {
        if (
    call boolean function_name(input[i])) {
          
    output[count(output)] = input[i];
        }
      }
      return 
    output;

    Initial comment regarding potential implementation: my understanding is that the compiler creates a new instance of the function for each distinct template specialization.
    Last edited by heeheehee; 09-10-2017 at 05:12 PM.

  2. #2
    Senior Member
    Join Date
    Apr 2009
    Posts
    1,750

    Default

    Java generics do not make sense without inheritance (and interfaces). That's the only thing they do: let you write inheritance-aware code without constant up/downcasting. That's not something I can see being useful in ASH.

    C++ templates are a laterally different concept. They are not code yet, they are *templates* for code, and need to be instantiated (or preprocessed, but by yet different part of the toolchain than the C/C++ preprocessor is). As you say, this generates new functions (and types, if you template-ize a struct). That's something I can see in ASH.
    (C++ templates also allow for insane things at compile-time, with parameters that have computed values, but I don't think we want *that* in ASH ... yet)

  3. #3
    Developer
    Join Date
    Aug 2009
    Posts
    2,700

    Default

    Java generics do not make sense without inheritance (and interfaces). That's the only thing they do: let you write inheritance-aware code without constant up/downcasting. That's not something I can see being useful in ASH.
    Originally Posted by xKiv View Post
    Not true! The entire java.util.Collections library benefits largely from the use of generics, which in turn helps type-safety when using, say, a HashSet<String> (so you can't throw arbitrary other datatypes into the set).

  4. #4
    Senior Member
    Join Date
    Apr 2009
    Posts
    1,750

    Default

    Not true! The entire java.util.Collections library benefits largely from the use of generics, which in turn helps type-safety when using, say, a HashSet<String> (so you can't throw arbitrary other datatypes into the set).
    Originally Posted by heeheehee View Post
    And they do even that by handling everything as Object and then up/downcasting automatically behind the scenes. You benefit mostly by not being able to forget to write those up/downcasts explicitly.
    Templates don't need to have common ancestor, they only need duck-typing.

    (one of the most visible differences is that you can template on a primitive type (like int), which is something you cannot do with generics)

    But what I really meant is that, with generics, if you want to handle the generically parametrized objects in any way, you have to constraint the generic type to extend from some class or interface (in the case of collections, from Object - that's all you need for ==, equals(), hashCode(), and for passing references around), or explicitly cast. Which means that you often cannot generically handle third-party objects even though they have the right methods (but not the right interface). (and thus, wrapper objects proliferate).

  5. #5
    Developer
    Join Date
    Aug 2009
    Posts
    2,700

    Default

    And they do even that by handling everything as Object and then up/downcasting automatically behind the scenes. You benefit mostly by not being able to forget to write those up/downcasts explicitly.
    Templates don't need to have common ancestor, they only need duck-typing.
    Originally Posted by xKiv View Post
    I don't disagree on that front; that's just a design choice (limitation?) of Java.

    (one of the most visible differences is that you can template on a primitive type (like int), which is something you cannot do with generics)
    ...
    (and thus, wrapper objects proliferate).
    Yup, Integer, Float, etc. I guess my original Java example doesn't quite work, since you'd really want to be invoking a.compareTo(b), T would extend Comparable, etc.

    Either way, I only cited Java's generic system because the syntax is ever so slightly more concise than C++'s templates, and the concept is similar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •