[racket] Typed Racket struct with more permissive constructor

9 views
Skip to first unread message

Neil Toronto

unread,
May 31, 2012, 11:42:07 AM5/31/12
to us...@racket-lang.org
Currently, the floating-point bitmap type is defined so that its size
fields are Integer, and a guard assures that they're really
Nonnegative-Fixnum:

(struct: flomap ([values : FlVector]
[components : Integer]
[width : Integer]
[height : Integer])
#:transparent
#:guard
(λ (vs c w h name)
(with-asserts ([c nonnegative-fixnum?]
[w nonnegative-fixnum?]
[h nonnegative-fixnum?])
[...] ; elided length check on vs
(values vs c w h))))


What I'd *really* like, though, is to have the fields be
Nonnegative-Fixnum in the first place, and simply have a more permissive
constructor. Is there a way to do that? As it is, `flomap-components'
always returns an Integer, but no flomap instance can actually have an
integer-valued `components' field.

Neil ⊥

____________________
Racket Users list:
http://lists.racket-lang.org/users

Sam Tobin-Hochstadt

unread,
May 31, 2012, 11:50:04 AM5/31/12
to Neil Toronto, us...@racket-lang.org

Could you just structure the program this way?

(struct: flomap ([values : FlVector]

[components : Nonnegative-Fixnum]


[width : Integer]
[height : Integer])

#:transparent)

(: make-flomap : FlVector Integer Integer Integer -> flomap)
(define (make-flomap v c w h)
(with-asserts ... (flomap v c w h)))
--
sam th
sa...@ccs.neu.edu

Neil Toronto

unread,
May 31, 2012, 12:21:07 PM5/31/12
to Sam Tobin-Hochstadt, us...@racket-lang.org

I could, but `make-flomap' is already defined and used extensively (it's
an analogue of make-flvector). Also, not using the structure name to
create instances from their field values now makes me feel dirty. :D

Neil ⊥

Ray Racine

unread,
May 31, 2012, 12:47:22 PM5/31/12
to Neil Toronto, us...@racket-lang.org, Sam Tobin-Hochstadt
On occasion the datatype as constructor does get in the way.

(struct: MyDataType (...))

One needs to export the MyDataType identifier for type declarations.  But entailed is a constructor function and match deconstruction as well.  i.e. I can't provide the type without providing the ctor as well when maybe all I wanted was the type. 

For 95% of all Typed structs: isn't a problem.  For the remaining 5% one wants some sort of detailed control of the ctor function, which leads to public/private fields, default init values ....

Support for some sort of case-lambda ctors as well as default field values would go far and avoids potential multiple make-Mytype and new-Mytype.

(struct: Person ([name: String] [age: Integer #default 21]) )

then 

(Person "Joe")
(Person "Joe" 35) 

Sam Tobin-Hochstadt

unread,
May 31, 2012, 12:57:37 PM5/31/12
to Neil Toronto, us...@racket-lang.org
On Thu, May 31, 2012 at 12:21 PM, Neil Toronto <neil.t...@gmail.com> wrote:

You should be able to fix this with a `rename-out', presumably.

> Also, not using the structure name to create
> instances from their field values now makes me feel dirty. :D

I'm not sure I can help with that. ;)

--
sam th
sa...@ccs.neu.edu

Reply all
Reply to author
Forward
0 new messages