munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.43k stars 1.01k forks source link

Unnecessary return null + capital Void #1116

Closed HorridModz closed 1 year ago

HorridModz commented 1 year ago

In 11.3.1 (page 178 in physical book), there is this code:

@Override
public Void visitBlockStmt(Stmt.Block stmt) {
    beginScope();
    resolve(stmt.statements);
    endScope();
    return null;
}

I believe there's two things wrong with this code.

  1. The data type (void) is capitalized - it should be "void", not "Void"
  2. There is an unnecessary 'return null;' at the end.
prggTheProgrammer commented 1 year ago

I don't think this is wrong. It is explained in chapter 8. Because this method implements the interface method E visitBlockStmt and E is of type Void it needs to return null.

class Interpreter implements Expr.Visitor<Object>,
                             Stmt.Visitor<Void> {

The reason this can't be Stmt.Visitor<void> is explained in the note: Java doesn’t let you use lowercase “void” as a generic type argument for obscure reasons having to do with type erasure and the stack. Instead, there is a separate “Void” type specifically for this use. Sort of a “boxed void”, like “Integer” is for “int”. So because of the <Void> the return type needs to be Void and the method must have return null;

HorridModz commented 1 year ago

Sorry for the misunderstanding! Thanks for correcting me.

petejerky commented 1 year ago

Respect!!!!!On 15 Apr 2023, at 3:43 am, HorridModz @.***> wrote: Sorry for the misunderstanding! Thanks for correcting me.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you are subscribed to this thread.Message ID: @.***>

HorridModz commented 1 year ago

@petejerky ?

petejerky commented 1 year ago

YesOn 17 Apr 2023, at 1:18 pm, HorridModz @.***> wrote: @petejerky ?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>