We could expose the C virtual machine's type tags and that would be O(1) to a unique number. We could define the number as not being stable across executions of Racket or versions, whichever we like better.
If you're uncomfortable with the number, we could combine it with an O(1) hash lookup from the number to a symbol. I think an extension like this is achievable for someone with a cursory knowledge of C. You should do it Alexis. -- The numbers are given here in this enum: https://github.com/racket/racket/blob/master/racket/src/racket/src/stypes.h The type tag is just called type: https://github.com/racket/racket/blob/master/racket/src/racket/include/scheme.h#L326` Jay On Fri, Mar 25, 2016 at 11:50 PM, Alexis King <[email protected]> wrote: > This is something that has bothered me for a little while. As far as I > know, there is no way in Racket to determine the type of some built-in > structure in constant time. Traditionally, the solution is to just use > predicates, but this is obviously linear in the number of predicates to > be tested. It also needs to be extended for every single datatype that > gets added to the language (or was simply omitted). > > I’d love to be able to have a type-of operator that produces a unique > key for each type of data structure Racket has. Why? Well, a little > while ago, I tried to implement multimethods in Racket in a safe way[1], > and I think my solution was a pretty good one. However, it is currently > pretty limited by the fact that it only works with structs, since it > uses the structure type as a key for dispatch. It would be really nice > to make dispatch O(1) for all conceivable datatypes, including the > builtins. > > This doesn’t really seem all that hard to me, but I could be totally > wrong. Things are often a lot harder to do than they initially seem. Is > there any reason this isn’t in the language right now? I could imagine > an argument that it exposes too much about the implementation, but since > the predicates already exist, I’m not sure that’d actually be true. It’d > be really helpful for making a useful uniform interface for my > multiple-dispatch system. > > Thanks, > Alexis > > [1]: > http://lexi-lambda.github.io/blog/2016/02/18/simple-safe-multimethods-in-racket/ > > -- > You received this message because you are subscribed to the Google Groups > "Racket Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/racket-dev/D016C78B-7C44-42C1-90D8-3E8CB8F660EE%40gmail.com. > For more options, visit https://groups.google.com/d/optout. -- Jay McCarthy Associate Professor PLT @ CS @ UMass Lowell http://jeapostrophe.github.io "Wherefore, be not weary in well-doing, for ye are laying the foundation of a great work. And out of small things proceedeth that which is great." - D&C 64:33 -- You received this message because you are subscribed to the Google Groups "Racket Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-dev/CAJYbDa%3DLqVG_FO3oSRFcs5%3DY6PPNJqqOuQtSP%3DMyTbOY19Gj2w%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
