Macht es Java "Typ -Löschung" unmöglich, ungeprüfte Forderungen zu beseitigen, z. Verbraucher ? [geschlossen]Java

Java-Forum
Anonymous
 Macht es Java "Typ -Löschung" unmöglich, ungeprüfte Forderungen zu beseitigen, z. Verbraucher ? [geschlossen]

Post by Anonymous »

Ich entwerfe einen Folgekurs für AP Informatik A (was in Java unterrichtet wird). Mein Ziel ist es, Highschool -Senioren beizubringen, wie eine einfache Programmiersprache von Grund auf implementiert werden kann. Ich habe Java gelernt, bevor es Generika oder Lambda-Ausdrücke hatte. Daher habe ich meinen Dispatcher erweitert, um über die 13 Funktionstypen zu erfahren, die ausführen, , akzeptieren oder anwenden können. Aber versuchen Sie es, wie ich vielleicht vielleicht herausfinden konnte, wie ich Javas Typ -System glücklich machen kann. /> Ist es überhaupt möglich, eine "nicht kontrollierte Besetzung" zu vermeiden, wenn sie sich mit Typen wie Consumer < /code> befassen, deren Einzelheiten vor der Laufzeit gelöscht werden?

Code: Select all

// Minimum viable example of trying to avoid "unchecked cast"

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.Runnable;
import java.util.function.Consumer;
import java.util.function.Function;

public class MinimumViableExample {

private static final int VM_STACK_SIZE = 64;

// Argument stack
private static Object[] vm_stack = new Object[VM_STACK_SIZE];
private static int vm_stack_ptr = 0;

// Function type signatures (not all used in this example)
private enum Type {SIG_____, SIG_b___, SIG_i___, SIG_o___,
SIG_ii__, SIG_so__, SIG_sii_, SIG_Iii_,
SIG____s, SIG_s__i, SIG_i__i, SIG_i__O,
SIG_s__o, SIG_ii_i, SIG_si_o} //signatures

// Main: push 2 ints on stack, multiply them, leave result on stack
public static void main (String[] args) {
Object[] bcode = new Object[]{"sequence",
new Object[]{"vm-push-lit", 10},
new Object[]{"vm-push-lit", 2},
new Object[]{"vm-funcall", "*"}};

eval_bcode(bcode);
System.out.printf("10*2 = %d\n", (Integer) vm_stack[0]);
}

// This is the "helper function" proposed as solution by LLM
private static void accept_boolean_consumer (Object function, Object first_arg)
{
if (function instanceof Consumer) {
Consumer c_function = (Consumer) function;
//if (c_function instanceof Consumer) { // Bogus instanceof type
((Consumer) c_function).accept((Boolean) first_arg);
//}
}}

// This is the core of the interpreter
private static void eval_bcode (Object bcode) {
if (bcode == null || ((Object[]) bcode).length == 0) {
return;
} else if (bcode instanceof Object) {
Object operator = ((Object[]) bcode)[0];
switch (operator.toString()) {
case "sequence":    //Execute sequence of commands
eval_bcode_sequence(get_bcode_list(bcode));
break;
case "vm-push-lit": //Push literal onto the stack
vm_push_lit(((Object[]) bcode)[1]);
break;
case "vm-funcall":  //Call function, push result
vm_funcall(((Object[]) bcode)[1].toString());
break;
}}}

// Returns copy of bcode argument, stripped of first element
private static Object[] get_bcode_list (Object bcode) {
Object[] bcodeList = new Object[((Object[]) bcode).length -1];
System.arraycopy(((Object)bcode), 1, bcodeList, 0, ((Object[]) bcode).length -1);
return bcodeList;
}

// Evaluate a list of code commands
private static void eval_bcode_sequence (Object[] bcode_list) {
for (Object bcode : bcode_list) {
eval_bcode(bcode);
}}

//Push literal trampoline (vm_push shared by functions not in this example)
private static void vm_push_lit (Object x) {
vm_push(x);
}

//Does actual work of stack push
private static void vm_push (Object x) {
vm_stack[vm_stack_ptr++] = x;
}

//Does actual work of stack pop
private static Object vm_pop () {
return vm_stack[--vm_stack_ptr];
}

//Core of function dispatch, where all the unchecked casts are
private static void vm_funcall (String fun_name) {
Object[] fun_data        = (Object[]) fun_n_args(fun_name);
int n_args               = (int)      fun_data[0];
Type typesig             = (Type)     fun_data[1];
boolean side_effect_only = (boolean)  fun_data[2];
Object function          = (Object)   fun_data[3];
Object[] arglist         = new Object[n_args];
for (int x = 0; x < n_args;  x++) {
arglist[x] = vm_pop();
}
Object value = null;
if (side_effect_only) {
switch (typesig) {
case SIG_____:
((Runnable) function).run();
break;
case SIG_b___:
accept_boolean_consumer(function, arglist[0]);
break;
//Several more cases omitted...
}
} else {
// These functions return a value (push onto stack below)
switch (typesig) {
case SIG_i__i:
value = (Integer) ((Function) function).apply((Integer) arglist[0]);
break;
case SIG_ii_i:
value = (Integer) ((BiFunction) function).apply((Integer) arglist[0], (Integer) arglist[1]);
break;
//Several more cases omitted...
}}
if (!side_effect_only) {
vm_push(value);
}}

//Table maps function names to {n_args, type_sig, no_return_val, implementation}
private static Object[] fun_n_args (String fun_name) {
switch (fun_name) {
case "1-":  //pop, subtract 1, push result
return new Object[] {1, Type.SIG_i__i, false, (Function) (x) -> x - 1 };
case "*":   //pop2, multiply, push result
return new Object[] {2, Type.SIG_ii_i, false, (BiFunction) (x, y) -> x * y };
// Many cases omitted...
default:
System.out.println(fun_name + " is not supported");
return null;
}}

@FunctionalInterface
private interface BiFunction {
R apply(T t, U u);
}
}

Quick Reply

? [geschlossen]" />
Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post