@louis77 has joined the channel
Hi there - just a little hello from a new Racket user! A nice day to you all.
I currently working my way through Racket Weekend from Jesse Alama. Bringing some experiences with Common Lisp, one thing I miss in DrRacket is the ability to send S-expressions from the definition window to the interactive window. Isn’t that something Racketeers miss?
Probably a very noob question :slightly_smiling_face:
Sorry for the terse response - at work!
No that is perfect, thanks!
For example, (module m racket (let l () (l)) (define x x))
Doesn’t raise a runtime error
Similarly if some expression uses a continuation before that definition
Or if the definition is inside a lambda
Of course some special cases could be recognized but an error that happens only in particular special cases is not usually a good idea
Does racket have the ability to create variables with “hidden” compartments? Example of desired functionality: (define a (hidden-compartment 10 #hide-me 20))
(+ a 20)
> 30
(define (my-function foo)
(if (has-hide-me? foo)
(unhide foo)
(+ foo 10))))
(my-function a)
> 20
(my-function 1)
> 11
The key here is that unless the code “looks” for the hidden value the variable is masquerading as a plain ol’ integer
No, there’s not a general way to do something like that
dang, thanks for the quick response
Do any other systems you know of have something like that?
I think in general it would be very difficult to actually make a system behave that way consistently
For sure, but Racket does a lot of things I would consider very difficult so I figured it couldnt hurt to ask :stuck_out_tongue:
The closest I can think of is variable annotations in angr’s http://angr.io/api-doc/claripy.html#module-claripy.annotation\|claripy . But that’s a whole separate beast
(define a (hidden-compartment 10 #:hide-me 20))
(+h a 20) ; => 30
(define (my-function foo)
(if (has-hide-me? foo)
(unhide foo)
(+h foo 10)))
(my-function a) ; => 20
(my-function 1) ; => 11
Not exactly what you want, but maybe a compromise :)
@samth is it (theoretically?) possible in Typed Racket to redefine + to accommodate this w/o slowing things down?
It really depends on the semantics, but I doubt it
I just meant the ability to redefine +
in such a way, that at compile time, if the args were numeric, the normal +
would be used, but if any of the args were of another type, a different function would be used. I think Julia might be able to do this.
I suppose “overload” is a better term than “redefine”.
It’s certainly possible to do something like that but you’d probably end up with very few operations actually optimized, because most things would have to handle both cases (otherwise you’d be re-implementing everything twice)
This is basically how Haskell works, and they can optimize things, but you aren’t guaranteed anything
Note that this is possible:
#lang racket
(begin-for-syntax
(struct hidden-compartment (v hv)
#:property prop:set!-transformer
(λ (self stx)
(syntax-case stx (set!)
[(set! _ v) #`(set! #,(hidden-compartment-v self) v)]
[(f args ...) #'((#%expression f) args ...)]
[_ (hidden-compartment-v self)]))))
(define-syntax-rule (define-hidable x v #:hide-me hv)
(begin
(define v* v)
(define hv* hv)
(define-syntax x (hidden-compartment #'v* #'hv*))))
(define-syntax (unhide stx)
(syntax-case stx ()
[(_ v)
(hidden-compartment-hv
(syntax-local-value #'v))]))
(define-hidable a 10 #:hide-me 20)
a ;=> 10
(unhide a) ;=> 20
(set! a 40)
a ;=> 40
(unhide a) ;=> 20
(define-hidable f (λ (x) x) #:hide-me 1)
(f 10) ;=> 10
(unhide f) ;=> 1
Of course, when you pass a hidable to other functions, the “regular” variant is used, so it doesn’t work exactly like what you want.
I think you could do this with impersonaters to some degree (https://docs.racket-lang.org/reference/chaperones.html?q=chaperone). However, they don’t work on very primitive objects like integers.
yes, many things can be chaperoned, but also many things can’t (anything opaque, roughly)
I like the macro way better anyway. Less overhead and almost as general.
as others have said, yes you can do this with impersonation but only for types that support impersonation. the specific mechanism is impersonator properties which let you stick key-value metadata on things https://docs.racket-lang.org/reference/chaperones.html#(tech._impersonator._property)
What is the proper way to conditionally compile code? For example, I have a file that has two mutually exclusive sets of (require provide define-syntax-rule ...)
. When I made my first attempt, I got a “require: not at module level or top level” error.
I suppose I could put the if
within the require
.
Nope :)
Wrapping them both in begin
works. That allows me to simply comment/uncomment one or the other.
I think the usual approach is to have a macro with some compile time code that decides which one to do
how do i install a racket package without internet? im using my friends VPS that blocks all connections by default and i dont wanna bug him to whitelsit
think png-image is the name of the package i need to be installed offline
I would download the source code (via git, or you can get a tarball of a git repo from github) and then use raco pkg install
locally