-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpaper.tex
More file actions
1146 lines (935 loc) · 29.9 KB
/
paper.tex
File metadata and controls
1146 lines (935 loc) · 29.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% SPDX-FileCopyrightText: Copyright (c) 2020-2026 Yegor Bugayenko
% SPDX-License-Identifier: MIT
\documentclass[sigplan,nonacm]{acmart}
\settopmatter{printfolios=false,printccs=false,printacmref=false}
\usepackage[nocn,noframes]{ffcode}
\usepackage{natbib}
\usepackage{tikz}
\usepackage[capitalize]{cleveref}
\usepackage{eolang}
\usepackage{to-be-determined}
\setlength\footskip{13pt}
\newcommand\broken{\tbd{This section is not valid, must be rewritten!}}
\title{Reducing Programs to Objects}
\subtitle{
Ver:
\texorpdfstring{
\href{https://github.com/REPOSITORY/releases/tag/0.0.0}
{\ff{0.0.0}}
}{0.0.0}
}
\author{Yegor Bugayenko}
\affiliation{\institution{Huawei}\country{Russia}\city{Moscow}}
\email{yegor256@gmail.com}
\begin{abstract}
C++, Java, C\#, Python, Ruby, and JavaScript are the most powerful object-oriented programming languages if language power is defined as the number of \emph{features} available to a programmer.
\eolang{}, on the other hand, is an object-oriented programming language with a reduced set of features: it has nothing but objects and mechanisms of their composition and decoration.
This paper asks the following research question: ``Which known features are possible to implement using only objects?''
\end{abstract}
\begin{document}
\raggedbottom
\maketitle
\section{Features}
We selected the most complex features and demonstrated how each of them may be represented in \eolang{}~\citep{bugayenko2021eolang} objects\footnote{%
\LaTeX{} sources of this paper are maintained in
\href{https://github.com/REPOSITORY}{REPOSITORY} GitHub repository,
the rendered version is \href{https://github.com/REPOSITORY/releases/tag/0.0.0}{\ff{0.0.0}}.}:
\begin{itemize}
\item Non-conditional jumps
(\cref{sec:goto}),
\item Data and code pointers
(\cref{sec:pointers}),
\item Procedures
(\cref{sec:procedures}),
\item Classes
(\cref{sec:classes}),
\item Exceptions
(\cref{sec:exceptions}),
\item Anonymous functions
(\cref{sec:blocks}),
\item Generators
(\cref{sec:generators}),
\item Types and casting
(\cref{sec:types}),
\item Reflection
(\cref{sec:reflection}),
\item Static methods
(\cref{sec:static}),
\item Inheritance
(\cref{sec:inheritance}),
\item Method overloading
(\cref{sec:overloading}),
\item Java generics
(\cref{sec:generics}),
\item C++ templates
(\cref{sec:templates}),
\item Mixins
(\cref{sec:mixins}),
\item Java annotations
(\cref{sec:annotations}).
\end{itemize}
Other features are more trivial, which is why they are not presented in this paper, such as operators, loops, variables, code blocks, constants, branching, and so on.
\subsection{Goto}
\label{sec:goto}
Goto is a one-way imperative transfer of control to another line of code.
There are only two possible cases of goto jumps: forward and backward.
\subsubsection{Backward Jump}
This is a ``backward'' jump example in C language:
\begin{ffcode}
#include "stdio.h"
void f() {
int i = 1;
again:
i++;
if (i < 10) goto again;
printf("Finished!");
}
\end{ffcode}
It can be mapped to the following \eolang{} code:
\begin{ffcode}
[] > f
malloc.of > @
0
[i] >>
seq * > @
i.write 1
go.to
[g] >>
seq * > @
i.write (i.plus 1)
if.
i.lt 10
g.backward
true
QQ.io.stdout "Finished!"
\end{ffcode}
Here, the one-argument abstract atom \ff{go.to} is being copied with a one-argument abstract anonymous object, which is a sequence of objects incrementing \ff{i} and then comparing it with the number ten.
If the condition is true, \ff{backward} is called, which leads to a backward jump and re-iteration of \ff{go.to}.
\subsubsection{Forward Jump}
This is an example of a ``forward'' jump in C language:
\begin{ffcode}
int f(int x) {
int r = 0;
if (x == 0) goto exit;
r = 42 / x;
exit:
return r;
}
\end{ffcode}
It can be mapped to the following \eolang{} code:
\begin{ffcode}
[x] > f
malloc.of > @
0
[r] >>
seq * > @
r.write 0
go.to
[g] >>
seq * > @
if.
x.eq 0
g.forward true
true
r.write (42.div x)
r
\end{ffcode}
Here, the same abstract atom \ff{go.to} is copied with an abstract one-argument object that is a sequence of objects.
When the condition is true, a forward jump is performed by \ff{forward} atom.
Similarly, the atom \ff{go.to} may be used to simulate other conditional jump statements, like \ff{break}, \ff{continue}, or \ff{return} in the middle of a function body (see \cref{sec:procedures}).
\subsubsection{Complex Case}
This is a more complex case of using \ff{goto} in C:
\begin{ffcode}
#include "stdio.h"
void f(int a, int b) {
goto start;
back:
printf("A");
start:
if (a > b) goto back;
printf("B");
}
\end{ffcode}
To translate this code to \eolang{}, it must be refactored as \cref{fig:goto} demonstrates.
The function \ff{f()} is copied twice, and each copy has its own execution flow implemented.
\begin{figure}
\includegraphics[width=0.9\columnwidth]{goto-pic}
\caption{The function \ff{f} with a few \ff{goto} statements inside is translated to two functions, reducing the complexity of the code at the cost of introducing duplication.}
\Description{Workflow of GOTO object}
\label{fig:goto}
\end{figure}
\begin{ffcode}
#include "stdio.h"
void f(int a, int b) { f1(a, b); }
void f1(int a, int b) {
if (a > b) f2(a, b);
printf("B");
}
void f2(int a, int b) {
back:
printf("A");
if (a > b) goto back;
printf("B");
}
\end{ffcode}
Then, the translation to \eolang{} is trivial using the \ff{go.to} object.
In more complex cases, a program may first be restructured to replace \ff{goto} statements with loops and branching, as suggested by \citet{williams1985restructuring,pan1996formal,erosa1994taming,ceccato2008goto}.
\subsubsection{Multiple Returns}
Some structured programming languages allow a function to exit at an arbitrary place, not only at the end, using \ff{return} statements, for example:
\begin{ffcode}
void abs(int x) {
if (x > 0) {
return x;
}
return -1 * x;
}
\end{ffcode}
This can be mapped to the following \eolang{} code using \ff{go.to} object:
\begin{ffcode}
[x] > abs
go.to > @
[g] >>
seq * > @
if.
x.gt 0
g.forward x
true
g.forward
-1.times x
\end{ffcode}
The dataization of \ff{forward} will exit the \ff{go.to} object wrapping the entire code in the ``function'' \ff{abs}.
\subsection{Pointers}
\label{sec:pointers}
\broken
A pointer is an object in many programming languages that stores a memory address.
Pointers may point to data memory or program memory.
\subsubsection{Pointers to Data}
This is an example of C code, where the pointer \ff{p} is first incremented by seven times \ff{sizeof(book)} (which is equal to 108) and then de-referenced to become a struct \ff{book} mapped to memory.
Then, the \ff{title} part of the struct is filled with a string and the \ff{price} part is returned as a result of the function \ff{f}:
\begin{ffcode}
#include "string.h"
struct book {
char title[100];
long long price;
};
int f(struct book* p) {
struct book b = *(p + 7);
strcpy(b.title, "Object Thinking");
return b.price;
}
\end{ffcode}
This code can be mapped to the following \eolang{} code:
\begin{ffcode}
[ptr] > book
ptr > @
ptr.block > title
100
[b] (b.as-string > @)
ptr.block > price
8
[b] (b.as-int > @)
pointer. > p1
heap 1024
0
108
[p] > f
seq * > @
book (p.add 7) > b
b.title.write
("Object Thinking").as-bytes
b.price
\end{ffcode}
Here, \ff{p1} is an object that can be used as an argument of \ff{f}.
It is a copy of an abstract object \ff{heap.pointer}, which expects two parameters:
1)~an absolute address in memory
and
2)~the size of the memory block it points to, in bytes.
Its attribute \ff{block} expects two attributes: 1)~the number of bytes in the heap and 2)~an abstract object that can encapsulate \ff{bytes} that were just read from memory.
The object \ff{heap} is an abstraction of a random access memory.
\subsubsection{Pointers to Code}
A pointer may not only refer to data in the heap but also to executable code in memory (there is no difference between ``program'' and ``data'' memory in both x86 and ARM architectures).
This is an example of a C program that calls a function referenced by a function pointer, providing two integer arguments to it:
\begin{ffcode}
int foo(int x, int y) {
return x + y;
}
int f() {
int (*p)(int, int);
p = &foo;
return (*p) (7, 42);
}
\end{ffcode}
This code can be mapped to the following \eolang{} code:
\begin{ffcode}
[x y] > foo
x.plus y > @
[] > f
cage 0 > p
seq * > @
p.write
[x y]
foo x y > @
p.@ 7 42
\end{ffcode}
It is important to note that the following code cannot be represented in \eolang{}:
\begin{ffcode}
int f() {
int (*p)(int);
p = (int(*)(int)) 0x761AFE65;
return (*p) (42);
}
\end{ffcode}
Here, the pointer refers to an arbitrary address in program memory, where some executable code is supposed to be located.
\subsubsection{Variables in Stack}
This C function, which returns seven (tested with GCC), assumes that variables are placed in the stack sequentially and uses pointer de-referencing to access them:
\begin{ffcode}
int f() {
long long a = 42;
long long b = 7;
return *(&a - 1);
}
\end{ffcode}
This code can be mapped to the following \eolang{} code:
\begin{ffcode}
[p] > long64
p.block > @
8
[b] (b.as-int > @)
[] > f
seq * > @
malloc. > stack
heap 32 > h
16
long64 stack > b
b.write (7.as-bytes)
long64 stack > a
a.write (42.as-bytes)
long64 (a.p.sub 1) > ret!
h.free stack
ret
\end{ffcode}
Here, the atom \ff{malloc} allocates a segment of bytes in the \ff{heap}. Later, the atom \ff{free} releases it.
The attribute \ff{ret} is made constant to avoid its recalculation after it is ``returned'' (this mechanism is explained in \cref{sec:destructors} where destructors are discussed).
\subsection{Procedures}
\label{sec:procedures}
A subroutine is a sequence of program instructions that performs a specific task, packaged as a unit.
In different programming languages, a subroutine may be called a routine, subprogram, function, method, or procedure.
In this PHP example, a function \ff{max} is defined:
\begin{ffcode}
function max($a, $b) {
if ($a > $b) return $a;
return $b;
}
\end{ffcode}
It can be mapped to the following \eolang{} code:
\begin{ffcode}
[a b] > max
go.to > @
[g] >>
seq * > @
if.
a.gt b
g.forward a
true
b
\end{ffcode}
This example also demonstrates how \ff{go.to} object can be used to simulate the behavior of the \ff{return} statement from within the body of a method.
\subsection{Impure Functions}
\broken
A function is pure when, among other qualities, it does not have side effects: no mutation of local static variables, non-local variables, mutable reference arguments, or input/output streams.
This PHP function is impure:
\begin{ffcode}
$a = 42;
function inc($a) {
$a = $a + 1;
return $a;
}
inc(inc($a)); // $a == 44
\end{ffcode}
It may be mapped to a constant \eolang{} object:
\begin{ffcode}
memory 42 > a
[x] > inc!
seq * > @
x.write
x.plus 1
inc
inc a
\end{ffcode}
\subsection{Classes}
\label{sec:classes}
\broken
A class is an extensible program code template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
The following program contains a Ruby class with a single constructor, two methods, and one mutable member variable:
\begin{ffcode}
class Book
def initialize(i)
@id = id
puts "New book!"
end
def path
"/tmp/${@id}.txt"
end
def move(i)
@id = i
end
end
\end{ffcode}
It can be mapped to the following \eolang{} code, where a class becomes an object factory:
\begin{ffcode}
[i] > book
cage i > id
[] > ruby-init
QQ.txt.sprintf > @
"New book!"
[] > path
QQ.txt.sprintf > @
"/tmp/%s.txt"
id
[i] > move
id.write i > @
\end{ffcode}
Here, the constructor is represented by the object \ff{ruby-init}, which finishes the initialization of the object.
Making an ``instance'' of a book would look like this:
\begin{ffcode}
book 42 > b
b.ruby-init
b.move 7
b.path
\end{ffcode}
\subsection{Destructors}
\label{sec:destructors}
\broken
In C++ and some other languages, a destructor is a method that is called for a class object when that object passes out of scope or is explicitly deleted.
The following code will print both \ff{Alive} and \ff{Dead} texts:
\begin{ffcode}
#include <iostream>
class Foo {
public:
Foo() { std::cout << "Alive"; }
~Foo() { std::cout << "Dead"; };
};
int main() {
Foo f = Foo();
}
\end{ffcode}
It may be translated to \eolang{} as such:
\begin{ffcode}
[] > foo
[] > constructor
QQ.io.stdout "Alive" > @
[] > destructor
QQ.io.stdout "Dead" > @
[] > main
foo > f
seq * > @
f.constructor
f.destructor
\end{ffcode}
There is no garbage collection in \eolang{}, so a destructor must be explicitly ``called'' when an object passes out of scope or is deleted.
\subsection{Exceptions}
\label{sec:exceptions}
Exception handling is the process of responding to the occurrence of exceptions—anomalous or exceptional conditions requiring special processing—during the execution of a program.
This C++ program utilizes exception handling to avoid segmentation fault due to null pointer de-referencing:
\begin{ffcode}
#include <iostream>
class Book { public: int price(); };
int price(Book* b) {
if (b == NULL) throw "NPE!";
return (*b).price() * 1.1;
}
void print(Book* b) {
try {
std::cout << "The price: " << price(b);
} catch (char const* e) {
std::cout << "Error: " << e;
}
}
\end{ffcode}
This mechanism may be implemented in \eolang{}:
\begin{ffcode}
[] > book
[] > price ?
[b] > price
if. > @
b.eq 0
error "NPE!"
b.price.times 1.1
[b] > print
try > @
[] >>
QQ.io.stdout > @
QQ.txt.sprintf *1
"The price: %d"
price b
[e] >>
QQ.io.stdout > @
QQ.txt.sprintf *1
"Error: %s"
e
true
\end{ffcode}
Here, the object \ff{try} expects three arguments: 1)~an abstract object to be dataized, 2)~an abstract object to be copied with one argument, in case dataization returns an encapsulated object, and 3)~an object to be dataized anyway (similar to Java \ff{finally} block).
In the object \ff{price}, we get the object \ff{error}, which, if dataized, causes the termination of dataization and a non-conditional jump to the ``catch'' object of the \ff{try}.
The mechanism is similar to ``checked'' exceptions in Java, where a method's signature must declare all types of exceptions the method may throw.
\subsubsection{Many Exception Types}
\broken
A Java method may throw a number of either checked or unchecked exceptions, for example:
\begin{ffcode}
void f(int x) throws IOException {
if (x == 0) {
throw new IOException();
}
throw new RuntimeException();
}
\end{ffcode}
This would be represented in \eolang{} as such:
\begin{ffcode}
[x] > f
if. > @
x.eq 0
error "IOException"
error "RuntimeException"
\end{ffcode}
To catch both exceptions the object \ff{f} would be used like this:
\begin{ffcode}
try
[]
try > @
[]
f 5 > @
[e1]
QQ.io.stdout e1 > @
[]
nop > @
[e2]
QQ.io.stdout e2 > @
[]
nop > @
\end{ffcode}
\subsection{Anonymous Functions}
\label{sec:blocks}
Anonymous functions can be passed into methods as arguments. For example, in this Ruby code a ``block'' (a name Ruby uses for anonymous functions) is passed:
\begin{ffcode}
def scan(lines)
lines.each do |t|
if t.starts_with? '#' yield t
end
end
scan(array) { |x| puts x }
\end{ffcode}
This mechanism may be implemented in \eolang{}:
\begin{ffcode}
[lines b] > scan
lines.each > @
[t] >>
if. > @
t.starts-with "#"
b t
true
scan
array
[x] >>
QQ.io.stdout x > @
\end{ffcode}
Here, the anonymous function is passed to the object \ff{scan} as an argument \ff{b}. The ``call'' of this function is the dataization of its copy, performed by the \ff{if} atom.
\subsection{Generators}
\label{sec:generators}
\broken
A generator is a routine that can be used to control the iteration behavior of a loop.
For example, this PHP program will print the first ten Fibonacci numbers:
\begin{ffcode}
function fibonacci(int $limit):generator {
yield $a = $b = $i = 1;
while (++$i < $limit) {
$b = $a + $b;
yield $b - $a;
$a = $b;
}
}
foreach (fibonacci(10) as $n) {
echo "$n\n";
}
\end{ffcode}
This mechanism may be implemented in \eolang{}:
\begin{ffcode}
[limit f] > fibonacci
memory 0 > a
memory 0 > b
memory 0 > i
seq * > @
a.write 1
b.write 1
f 0
while.
seq (i.write (i.plus 1)) (i.lt limit)
[idx]
seq * > @
b.write (a.plus b)
a.write (b.minus a)
f (b.minus a)
true
fibonacci > @
10
[n]
QQ.io.stdout > @
QQ.txt.sprintf "%d\n" n
\end{ffcode}
Here, the generator is turned into an abstract object \ff{fibonacci} with an extra parameter \ff{f}, which is specified with an abstract object argument that prints what is given as \ff{n}.
\subsection{Types and Type Casting}
\label{sec:types}
\broken
A type system is a logical system comprising a set of rules that assign a property called a type to various constructs of a computer program, such as variables, expressions, functions, or modules.
The main purpose of a type system is to reduce possibilities for bugs in computer programs by defining interfaces between different parts of a computer program, and then checking that the parts have been connected in a consistent way.
In this example, a Java method \ff{add} expects an argument of type \ff{Book} and compilation would fail if another type is provided:
\begin{ffcode}
class Cart {
private int total;
void add(Book b) {
this.total += b.price();
}
}
\end{ffcode}
The restrictions enforced by the Java type system at compile time through types \ff{Cart} and \ff{Book} may be represented in \eolang{} using decorators, for example:
\begin{ffcode}
[] > original-cart
memory 0 > total
[b] > add
total.write (total.plus (b.price))
[] > cart
original-cart > @
[b] > add
if.
b.subtype-of (QQ.txt.text "Book")
@.add b
[]
error "Type mismatch, Book expected" > @
\end{ffcode}
Here, it is expected that the parameter \ff{b} is defined in a ``class'' that has a \ff{subtype-of} attribute, which may also be provided by a decorator (a simplified example):
\begin{ffcode}
[] > original-book
memory 0 > price
[] > book
original-book > @
[t] > subtype-of
t.eq "Book" > @
[] > price
@.price > @
\end{ffcode}
This decoration may be simplified through a ``fluent'' supplementary object:
\begin{ffcode}
type "Cart" > cart-type
.super-types "Object" "Printable"
.method "add" "Book"
cart-type original-cart > cart
\end{ffcode}
Here, the \ff{type} object implements all necessary restrictions that the Java type system may provide for the type \ff{Cart} and its methods.
Type casting, which is a mechanism for changing an expression from one data type to another, may also be implemented through the same decorators.
\subsection{Reflection}
\label{sec:reflection}
\broken
Reflection is the ability of a process to examine, introspect, and modify its own structure and behavior.
In the following Python example, the method \ff{hello} of an instance of class \ff{Foo} is not called directly on the object but is first retrieved through reflection functions \ff{globals} and \ff{getattr}, and then executed:
\begin{ffcode}
def Foo():
def hello(self, name):
print("Hello, %s!" % name)
obj = globals()["Foo"]()
getattr(obj, "hello")("Jeff")
\end{ffcode}
It may be implemented in \eolang{} by encapsulating additional meta information in classes explained in \cref{sec:classes}.
\subsubsection{Monkey Patching}
\broken
Monkey patching is making changes to a module or class while the program is running.
This JavaScript program adds a new method \ff{print} to the object \ff{b} after the object has already been instantiated:
\begin{ffcode}
function Book(t) { this.title = t; }
var b = new Book("Object Thinking");
b.print = function() {
console.log(this.title);
}
b.print();
\end{ffcode}
This program may be translated to \eolang{}, assuming that \ff{b} is being held by an attribute of a larger object, for example \ff{app}:
\begin{ffcode}
[] > app
cage 0 > b
b' > copy
seq * > @
b.write
[] > book
[t] > new
memory "" > title
title.write t > @
copy.<
b.write
[] > book
[t] > new
seq * > @
[]
copy > @
[] > print
QQ.io.stdout (^.title)
b.print
\end{ffcode}
Here, the modification to the object \ff{book} happens by making a copy of it, creating a decorator, and then storing it where the original object was located.
\subsection{Static Methods}
\label{sec:static}
A static method (or static function) is a method defined as a member of an object but accessible directly from an API object's constructor, rather than from an object instance created via the constructor. For example, this C\# class consists of a single static method:
\begin{ffcode}
class Utils {
public static int max(int a, int b) {
if (a > b) return a;
return b;
}
}
\end{ffcode}
It may be converted to the following \eolang{} code, since a static method is nothing more than a ``global'' function:
\begin{ffcode}
[a b] > utils-max
if. > @
a.gt b
a
b
\end{ffcode}
\subsection{Inheritance}
\label{sec:inheritance}
\broken
Inheritance is the mechanism of basing a class upon another class while retaining similar implementation. In this Java code class \ff{Book} inherits all methods and non-private attributes from class \ff{Item}:
\begin{ffcode}
class Item {
private int p;
int price() { return p; }
}
class Book extends Item {
int tax() { return price() * 0.1; }
}
\end{ffcode}
It may be represented in \eolang{} like this:
\begin{ffcode}
[] > item
memory 0 > p
[] > price
p > @
[] > book
item > i
[] > tax
(QQ.math.number (i.price)).as-float.times 0.1 > @
\end{ffcode}
Here, composition is used instead of inheritance.
\subsubsection{Prototype-Based Inheritance}
\broken
So-called prototype-based programming uses generalized objects, which can then be cloned and extended. For example, this JavaScript program defines two objects, where \ff{Item} is the parent object and \ff{Book} is the child that inherits the parent through its prototype:
\begin{ffcode}
function Item(p) { this.price = p; }
function Book(p) {
Item.call(this, p);
this.tax = function () {
return this.price * 0.1;
}
}
var t = new Book(42).tax();
console.log(t); // prints "4.2"
\end{ffcode}
This mechanism of prototype-based inheritance may be translated to the following \eolang{} code, using the mechanism of decoration:
\begin{ffcode}
[p] > item
memory 0 > price
[] > new
price.write p > @
[] > book
[p] > new
item p > @
[] > tax
times. > @
as-float.
QQ.math.number (^.price)
0.1
QQ.io.stdout
QQ.txt.sprintf
"%f"
tax.
book.new 42
\end{ffcode}
\subsubsection{Multiple Inheritance}
\broken
Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. In this C++ example,
the class \ff{Jack} has both \ff{bark} and \ff{listen} methods, inherited from \ff{Dog} and \ff{Friend} respectively:
\begin{ffcode}
#include <iostream>
class Dog {
virtual void bark() {
std::cout << "Bark!";
}
};
class Friend {
virtual void listen();
};
class Jack: Dog, Friend {
void listen() override {
Dog::bark();
std::cout << "Listen!";
}
};
\end{ffcode}
It may be represented in \eolang{} like this:
\begin{ffcode}
[] > dog
[] > bark
QQ.io.stdout "Bark!" > @
[] > friend
[] > listen
[] > jack
dog > d
friend > f
[] > listen
seq * > @
d.bark
QQ.io.stdout "listen!"
\end{ffcode}
Here, inherited methods are explicitly listed as attributes in the object \ff{jack}. This is very close to what would happen in the virtual table of a class \ff{Jack} in C++. The \eolang{} object \ff{jack} just makes it explicit.
\subsection{Method Overloading}
\label{sec:overloading}
\broken
Method overloading is the ability to create multiple functions with the same name but different implementations. Calls to an overloaded function will run a specific implementation of that function appropriate to the context of the call, allowing one function call to perform different tasks depending on context. In this Kotlin program two functions are defined with the same name, while only one of them is called with an integer argument:
\begin{ffcode}
fun foo(a: Int) {}
fun foo(a: Double) {}
foo(42)
\end{ffcode}
It may be represented in \eolang{} like this:
\begin{ffcode}
[args...] > foo
(args.at 0) > a0
if.
a0.subtype-of "Int"
first-foo a0
second-foo a0
foo 42
\end{ffcode}
This code expects arguments of \ff{foo} to be equipped with the type system suggested in \cref{sec:types}. The attribute \ff{subtype-of} will help dispatch the call to the right objects.
\subsection{Java Generics}
\label{sec:generics}
\broken
Generics extend Java's type system to allow a type or method to operate on objects of various types while providing compile-time type safety.
For example, this Java class expects another class to be specified as \ff{T} before use:
\begin{ffcode}
class Cart<T extends Item> {
private int total;
void add(T i) {
total += i.price();
}
}