Error for defining class method as expression


I am trying to define a class method like so:

#lang racket
(define foo% (class object%
  (define/public plus-one (curry + 1))))

But this code produces the following error: eval:2:0: class: bad form for method definition

According to the Racket documentation, it should be possible to define a method using the syntax (define/public id expr), so I don't understand why this isn't working.

There is an easy workaround by using the (define/public (id . formals) body ...+) syntax, of course, but I would like to understand why the current code isn't accepted.

Ryan Culpepper

In the documentation for class*, see the nonterminals named method-definition and method-procedure. They describe the constraints on a legal method definition. A paragraph later in the class docs says that each private, public, etc declaration must correspond to a method-definition. So it isn't a bug, it's the intended behavior.

The reason behind the behavior, by the way, is that Racket's class macro implements methods by rewriting the method's lambda (or case-lambda) expression(s) to add an implicit argument for this. The same argument is also implicitly used to access the object's public and private fields. Restricting method definitions to certain shapes makes it possible to find the right lambda expressions to adjust.

Here's one way of rewriting your example:

#lang racket
(define do-plus-one (curry + 1))
(define foo% (class object%
  (define/public (plus-one n) (do-plus-one n)))

This has the following nice properties: do-plus-one is computed only once, and it doesn't take a field slot in each foo% object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at


Login to comment


Defining a class with a method

defining method of class in if statement

Conditionally defining method for container class

Defining a javascript class Method as a constant

Error defining function outside the class

Scala error when defining class

Defining Protege class with expression of numerical data

defining operator doesn't work anymore (Error in UseMethod("%op%"): no applicable method for '%op%' applied to an object of class "character")

Defining custom gradient as a class method in Tensorflow

Defining all abstract method in one concrete class

Java: Defining a generic method inside an anonymous class

Defining an abstract method in a SQLAlchemy base class

Swift protocol defining class method returning self

Determine the defining class from within a method

How to get the defining class of a bound method?

python method for defining indexing in a class (use of "[ ]")

Python: re-defining a class method at runtime

defining class vars in modules in crystal causes error

Receiving an Error While Defining a Class Member Function

Python a class method and defining a default value (if param is empty) and static method

Regex test failed in Javascript when defining expression as a class property

Undefined method for Class error

Compile error defining a std::hash for a template class nested class

defining a var outside of a class used hard in a class giving name error

error: expected primary-expression before ‘>’: templated function that try to uses a template method of the class for which is templated

Is there any solution to handle Transactional by defining in Abstract Class of Concrete Method?

Defining python class method using arguments from __init__

Scala what is the difference between defining a method in the class instead on the companion object

Get defining class of unbound method object in Python 3