Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

47
LINES

< > BotCompany Repo | #1033956 // GOperator - reification of an operator in a type-safe language. concrete definition with one signature

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4957L/27K).

// Reification of an operator in a type-safe language
// -has a name
// -has 0 or more arguments
//   -each argument has a type and can have a name (otherwise it gets some default name like "argument 1")
// -has an optional return type (or return type null for no return value)
//   -to return multiple values, use a struct type

// T is the meta-type for argument and return types
persistable sclass GOperator<T> extends GAbstractOperator<T> {
  gettable L<Argument> arguments;
  gettable T returnType;
  
  record noeq Argument(int index, S name, T type) {
    T type() { ret type; }
    
    S shortToString() {
      ret type + " " + name;
    }
  }
  
  *(S *name) {}
  
  // add argument
  selfType arg(T type, S name default makeDefaultArgumentName()) {
    arguments = createOrAdd(arguments, new Argument(l(arguments), name, type));
    this;
  }
  
  selfType returns(T type) {
    returnType = type;
    this;
  }
  
  S makeDefaultArgumentName() {
    ret "arg" + (l(arguments)+1);
  }
  
  public S toString aka toStringWithArgumentNames() {
    ret name + ": " + joinWithCommaOr("()", lmapMethod shortToString(arguments))
      + " -> " + or(returnType, "void");
  }
  
  S toStringWithoutArgumentNames() {
    ret name + ": " + joinWithComma(map(arguments, a -> a.type))
      + " -> " + or(returnType, "void");
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033956
Snippet name: GOperator - reification of an operator in a type-safe language. concrete definition with one signature
Eternal ID of this version: #1033956/18
Text MD5: de47a8635d5f669b25382f6a9a4efe73
Transpilation MD5: f4d56ba87e2984a0bf1ceb5812ad97bd
Author: stefan
Category: javax / scripting
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-15 02:31:39
Source code size: 1377 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 195 / 347
Version history: 17 change(s)
Referenced in: [show references]