Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-02 Thread Ben Greenman
Oh right --- instead of making a new exception type, it's possible to
use `require/typed` to give `raise` a different type signature:

```
#lang typed/racket/base

(require/typed racket/base
  (raise (All (A) (-> Any A

(with-handlers ((vector? values))
  (raise (vector 1 2 3)))
;; '#(1 2 3)
```

[[ any-wrap/c is the contract for the `Any` type ]]

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-02 Thread Robby Findler
It is a contract that TR uses internally that might be helpful here,
maybe. Or maybe not! :)

Robby

On Sat, Dec 2, 2017 at 3:32 AM, HiPhish  wrote:
> What is any-wrap/c? It does not show up in the documentation.
>
> On Friday, December 1, 2017 at 9:17:18 PM UTC+1, Robby Findler wrote:
>>
>> Why not use any-wrap/c?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-02 Thread HiPhish
What is any-wrap/c? It does not show up in the documentation.

On Friday, December 1, 2017 at 9:17:18 PM UTC+1, Robby Findler wrote:
>
> Why not use any-wrap/c?
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread Robby Findler
Why not use any-wrap/c?

Robby

On Fri, Dec 1, 2017 at 8:39 PM Sam Tobin-Hochstadt 
wrote:

> Two notes:
>
> 1. It is unsafe to expose mutable data to untyped code via exceptions
> without wrapping them. If you `(raise f)`, then untyped code could
> catch the exception and call `f`.
>
> 2. You should inherit from `exn:fail` -- other `exn` types are for
> non-standard exceptions such as `exn:break`.
>
> Sam
>
> On Fri, Dec 1, 2017 at 2:24 PM, Ben Greenman
>  wrote:
> > That name sounds good to me.
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an email to racket-users+unsubscr...@googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread Sam Tobin-Hochstadt
Two notes:

1. It is unsafe to expose mutable data to untyped code via exceptions
without wrapping them. If you `(raise f)`, then untyped code could
catch the exception and call `f`.

2. You should inherit from `exn:fail` -- other `exn` types are for
non-standard exceptions such as `exn:break`.

Sam

On Fri, Dec 1, 2017 at 2:24 PM, Ben Greenman
 wrote:
> That name sounds good to me.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread Ben Greenman
That name sounds good to me.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread HiPhish
Thank you, I have made my own exception type now. Is `exn:fail:rpc` 
appropriate or should I use a different name? Using that name looks like 
I'm inserting it into Racket's own exception type hierarchy.

On Friday, December 1, 2017 at 8:11:27 PM UTC+1, Ben Greenman wrote:
>
> ...
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread Ben Greenman
The error is because type signature of `raise` doesn't allow
"non-flat" values like functions and mutable vectors.

It might not be safe to allow `(raise (vector 1 2 3))` in Typed Racket
... I'm not sure.

For now I think you should make a new exception type. Example:

```
#lang typed/racket/base

(define-type Packable (U Byte (Vectorof Packable)))

(struct exn:fail:rpc exn ((val : Packable)))

(with-handlers ((exn:fail:rpc? exn:fail:rpc-val))
  (raise (exn:fail:rpc "oops" (current-continuation-marks) (vector 1 2 3
```

On Fri, Dec 1, 2017 at 9:05 AM, HiPhish  wrote:
> Hello Racketeers,
>
> I have been trying to port a module over to Typed Racket, and I have almost
> succeeded except for one issue: raising an arbitrary object as an error.
> Compare the following code:
>
> ;; Works fine
> (raise 3)
>
> ;; Does not work
> (raise (vector 1 2 3))
>
> The error I get is
> ; nvim-client/client.rkt:244:55: Type Checker: No function domains
> matched in
> ;   function application:
> ; Domains: (U (Rec flat (U (Pairof flat flat) Boolean Char Complex
> Keyword Null
> ;   String Symbol)) exn) Any
> ;  (U (Rec flat (U (Pairof flat flat) Boolean Char Complex
> Keyword Null
> ;String Symbol)) exn)
> ; Arguments: (U (Immutable-HashTable Packable Packable)
> (Mutable-HashTable
> ;   Packable Packable) (Pairof Packable (Listof Packable))
> (Weak-HashTable
> ;   Packable Packable) Boolean Bytes Ext Message-Args Null Real String)
> ;   in: (raise error)
> ; [,bt for context]
>
> So I looked up the signature of `raise` and I get this:
>
> > raise
> - : (->* ((U (Rec
>   flat
>   (U (Pairof flat flat)
>  Boolean
>  Char
>  Complex
>  Keyword
>  Null
>  String
>  Symbol))
>  exn))
>  (Any)
>  (Nothing : (Bot | Bot)))
> #
>
> So what does this mean and what should I do? I am porting a module from my
> Neovim client which uses the MessagePack-RPC protocol, and part of the
> protocol
> is that any object that can be packed can be raised as an error.
> https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md#error
>
> Raising any object as an error would have allowed someone making an RPC
> request
> to just do something like this:
>
> (with-handlers ([packable? (λ (exn) (display exn))])
>   (rpc-request "foo" '#(bar)))
>
> What should I do instead? Create a new error type like `exn:fail:rpc`? Or am
> I
> missing something here?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[racket-users] Raising arbitrary errors in Typed Racket

2017-12-01 Thread HiPhish
Hello Racketeers,

I have been trying to port a module over to Typed Racket, and I have almost
succeeded except for one issue: raising an arbitrary object as an error.
Compare the following code:

;; Works fine
(raise 3)

;; Does not work
(raise (vector 1 2 3))

The error I get is
; nvim-client/client.rkt:244:55: Type Checker: No function domains 
matched in
;   function application:
; Domains: (U (Rec flat (U (Pairof flat flat) Boolean Char Complex 
Keyword Null
;   String Symbol)) exn) Any
;  (U (Rec flat (U (Pairof flat flat) Boolean Char Complex 
Keyword Null
;String Symbol)) exn)
; Arguments: (U (Immutable-HashTable Packable Packable) 
(Mutable-HashTable
;   Packable Packable) (Pairof Packable (Listof Packable)) 
(Weak-HashTable
;   Packable Packable) Boolean Bytes Ext Message-Args Null Real String)
;   in: (raise error)
; [,bt for context]

So I looked up the signature of `raise` and I get this:

> raise
- : (->* ((U (Rec
  flat
  (U (Pairof flat flat)
 Boolean
 Char
 Complex
 Keyword
 Null
 String
 Symbol))
 exn))
 (Any)
 (Nothing : (Bot | Bot)))
#

So what does this mean and what should I do? I am porting a module from my
Neovim client which uses the MessagePack-RPC protocol, and part of the 
protocol
is that any object that can be packed can be raised as an error.
https://github.com/msgpack-rpc/msgpack-rpc/blob/master/spec.md#error

Raising any object as an error would have allowed someone making an RPC 
request
to just do something like this:

(with-handlers ([packable? (λ (exn) (display exn))])
  (rpc-request "foo" '#(bar)))

What should I do instead? Create a new error type like `exn:fail:rpc`? Or 
am I
missing something here?

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.