Code Monkey home page Code Monkey logo

Comments (5)

mbutterick avatar mbutterick commented on June 2, 2024

The problem seems to be in the signature of the ref generic:

  (ref indexable . _)

This notation means that the implementation of ref must have one fixed argument and one rest argument, so this amended example does work (note . i rather than i):

#lang racket
(require data/collection)
(struct wrapped-collection (value)
    #:methods gen:indexable
     [(define (ref c . i) 42)])

(ref (wrapped-collection 'foo) 'bar) ; 42

However, given that you define ref strictly as a two-ary function, this seems like an error, and that the generic definer should be:

(ref indexable idx)
(set-ref indexable idx val)

from racket-collections.

lexi-lambda avatar lexi-lambda commented on June 2, 2024

Curious; it looks like those contracts are not enforced in the #:defaults block. The motivation for this in the first place was a desire to support hash-ref/dict-ref’s failure-thunk arguments, but it looks like that was a bad idea. It seems like the right thing to do here is to change the generic signature to (ref indexable index).

from racket-collections.

mbutterick avatar mbutterick commented on June 2, 2024

The define-generics docs don’t mention it, but I accidentally discovered how to notate optional arguments in the generic signature:

(ref indexable idx [failure-thunk])

from racket-collections.

lexi-lambda avatar lexi-lambda commented on June 2, 2024

Doing such a thing requires all implementations to support a failure thunk; my original intention was to make such support optional but allowed. Of course, this means you could not depend on an arbitrary indexable supporting a failure thunk, so you could not write indexable-generic functions that use it, but you could use the short name if you knew it was used monomorphically.

In retrospect, that’s probably not a great idea. The question just becomes whether or not all indexables should support a failure thunk or if that ought to be optional. It could theoretically be supported in a separate interface with a different name, like gen:indexable/failure and ref/failure. Do you have a strong opinion either way?

from racket-collections.

mbutterick avatar mbutterick commented on June 2, 2024

my original intention was to make such support optional but allowed

I’m a generics noob. I have no opinion. Just observing that the docs are incomplete. So maybe what you originally intended is possible (by some other notational means).

from racket-collections.

Related Issues (19)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.