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).

1  
// Reification of an operator in a type-safe language
2  
// -has a name
3  
// -has 0 or more arguments
4  
//   -each argument has a type and can have a name (otherwise it gets some default name like "argument 1")
5  
// -has an optional return type (or return type null for no return value)
6  
//   -to return multiple values, use a struct type
7  
8  
// T is the meta-type for argument and return types
9  
persistable sclass GOperator<T> extends GAbstractOperator<T> {
10  
  gettable L<Argument> arguments;
11  
  gettable T returnType;
12  
  
13  
  record noeq Argument(int index, S name, T type) {
14  
    T type() { ret type; }
15  
    
16  
    S shortToString() {
17  
      ret type + " " + name;
18  
    }
19  
  }
20  
  
21  
  *(S *name) {}
22  
  
23  
  // add argument
24  
  selfType arg(T type, S name default makeDefaultArgumentName()) {
25  
    arguments = createOrAdd(arguments, new Argument(l(arguments), name, type));
26  
    this;
27  
  }
28  
  
29  
  selfType returns(T type) {
30  
    returnType = type;
31  
    this;
32  
  }
33  
  
34  
  S makeDefaultArgumentName() {
35  
    ret "arg" + (l(arguments)+1);
36  
  }
37  
  
38  
  public S toString aka toStringWithArgumentNames() {
39  
    ret name + ": " + joinWithCommaOr("()", lmapMethod shortToString(arguments))
40  
      + " -> " + or(returnType, "void");
41  
  }
42  
  
43  
  S toStringWithoutArgumentNames() {
44  
    ret name + ": " + joinWithComma(map(arguments, a -> a.type))
45  
      + " -> " + or(returnType, "void");
46  
  }
47  
}

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: 119 / 251
Version history: 17 change(s)
Referenced in: [show references]