[racket-users] Typed Classes and this%

2016-02-19 Thread Axel Schnell
I'm just experimenting with typed classes in the typed/racket language.In my 
first experiments I created a class called polynome% with a method derive. The 
result of this method is new instance of this class via (new this% ...). 
Unfortunately the drracket environment complained that it does not know this%. 
Is 'this%' missing by design or is it just not yet implemented?

-- 
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] Typed Classes and this%

2016-02-19 Thread Asumu Takikawa
Hi Axel,

On 2016-02-19 06:24:35 -0800, Axel Schnell wrote:
> I'm just experimenting with typed classes in the typed/racket language.In my
> first experiments I created a class called polynome% with a method derive.
> The result of this method is new instance of this class via (new this% ...).
> Unfortunately the drracket environment complained that it does not know
> this%. Is 'this%' missing by design or is it just not yet implemented?

It's missing by design, because it's unclear what type it can be assigned.

Typed Racket requires that a class type describes all of the methods/fields
that a class actually has, so that when you subclass it you can statically
check that no method/field name conflicts will occur.

But you can't write down such a type for `this%` because the methods/fields it
will have is only known at runtime. In particular, it may actually be a
subclass of the class you originally wrote the `this%` reference in.

Cheers,
Asumu

-- 
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] Typed Classes and this%

2016-02-19 Thread Matthias Felleisen

Axel, 

I tend to organize modules around classes so that I avoid the "Asumu pitfall": 

#lang typed/racket

(define-type Polynome% 
 (Class
  [init-field [a [Vectorof Real]]]
  [value (-> Real Real)]
  [derive (-> Polynome)]))

(define-type Polynome (Instance Polynome%))

#;
(type-out
 [create-polynome (-> [Vectorof Real] Polynome)])
;; where is it? 

(provide
 create-polynome)

(: create-polynome (-> [Vectorof Real] Polynome))
(define (create-polynome a0)
  (new polynome% [a a0]))

;; represent the polynomial p(x) = Σ a(i) * x^i
(: polynome% Polynome%)
(define polynome%
  (class object%
(init-field
 ;; [Vectorof Real]
 a)
(super-new)

;; Real -> Real 
(define/public (value x)
  (* (vector-ref a 0) x))

;; -> Polynome
(define/public (derive)
  (create-polynome a)
  #;
  (new this% [a a]

(: polynome Polynome)
(define polynome (new polynome% [a #(1 2 3)]))

(send polynome value 1)
(send (send polynome derive) value 1)


Warning: functionality intentionally wrong. 

I started from this untyped version: 

#lang racket

(provide
 ;; type Polynome =
 #;
 (Class
  [field a [Vectorof Real]]
  [value (-> Real Real)]
  [derive (-> Polynome)])

 ;; [Vectorof Real] -> Polynome
 create-polynome)

(define (create-polynome a)
  (new polynome [a a]))

;; represent the polynomial p(x) = Σ a(i) * x^i
(define polynome%
  (class object%
(init-field
 ;; [Vectorof Real]
 a)
(super-new)

;; Real -> Real 
(define/public (value x)
  (* (vector-ref a 0) x))

;; -> Polynome
(define/public (derive)
  (create-polynome a)
  #;
  (new this% [a a]


(define polynome (new polynome% [a #(1 2 3)]))

(send polynome value 1)
(send (send polynome derive) value 1)

;; You see how adding types clarified my thoughts. -- Matthias

-- 
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.