classes1.scm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. (module-static counter get-new-count <IdClass1> <IdClass2> my-id-class-2 call-lambda)
  2. ;; Based on a test-case from Jamison Hope <jrh@theptrgroup.com>
  3. (define-syntax (import-class form)
  4. (syntax-case form ()
  5. ((import-class fqcn)
  6. (let* ((cls :: java.lang.Class (eval (syntax fqcn)))
  7. (name (string->symbol (invoke cls 'getSimpleName))))
  8. #`(define-alias #,(datum->syntax-object form name) fqcn)))))
  9. (import-class java.util.Date)
  10. (define-constant xx :: <int> 20)
  11. (define-simple-class <SimpleA> ()
  12. (two :: <int> init-form: 2 access: 'private)
  13. (a :: <int> init: (- b two))
  14. (b :: <int> init-form: 6 allocation: class:)
  15. (n22 :: <int> init-value: 22 allocation: 'static access: 'protected)
  16. (hyphenated-field? init-value: "yes")
  17. (mHappy init: #t)
  18. ((isHappy) :: <boolean>
  19. mHappy)
  20. ((setHappy val :: <boolean>) :: <void>
  21. (set! mHappy val))
  22. ((lambda-method1)
  23. (lambda (x) (make-vector 1 x))) ; This lambda doesn't "capture" anything.
  24. ((lambda-method2 n)
  25. (lambda (x) (make-vector (+ a n) x))) ; This lambda does.
  26. ((lambda-method3)
  27. (call-lambda (lambda () (slot-ref (this) 'two))))
  28. ((lambda-method4)
  29. (call-lambda (lambda () two)))
  30. ((lambda-method5 x)
  31. (lambda () (set! two x)))
  32. ((lambda-method6)
  33. (lambda (x) (set! two x)))
  34. ((lambda-method7)
  35. (lambda (x) (slot-set! (this) 'two x)))
  36. ((lambda-method-rest1 name)
  37. (lambda (arg1 . rest) (list name arg1 rest)))
  38. ((x1900) :: <int> access: 'package allocation: 'static
  39. 1900)
  40. ((g) :: <int> access: 'protected
  41. (+ xx a))
  42. ((f (y :: <int>)) :: <int>
  43. (if (equal? hyphenated-field? "yes") (+ (g) b y) 999))
  44. ((withVarArg a #!rest b)
  45. (format "a:~a b:~b" a b))
  46. ((asText (i ::int))::string (java.lang.Integer:toString i))
  47. ((asText (t ::java.lang.CharSequence))::string (t:toString))
  48. ((test-asText)
  49. (with-compile-options warn-invoke-unknown-method: #t warn-as-error: #t
  50. ((this):asText "hello")))
  51. ;; Bug reported by Dean Ferreyra <dferreyra@igc.org> 2005-06-09
  52. ((trouble)
  53. (let ((fn (lambda (o)
  54. (get-identity-property-name o))))
  55. fn))
  56. ((get-identity-property-name property-name)
  57. (string-append property-name "Identity")))
  58. (define (call-lambda fn)
  59. (fn))
  60. (define-class <ClsB> ()
  61. (b :: <int> 14)) ;; deprecated syntax
  62. (define-class <ClsC> (<ClsB>)
  63. (c :: <int>)
  64. (init: (set! c (static-field <SimpleA> 'n22)))
  65. ((f (y :: <int>)) :: <int> (+ xx c y)))
  66. (define-syntax define-class-using-syntax-rules
  67. (syntax-rules ()
  68. ((define-class-using-syntax-rules name super parts ...)
  69. (define-simple-class name (super) parts ...))))
  70. (define *MY-YEAR-OFFSET* 1900)
  71. (define (default-offset)
  72. *MY-YEAR-OFFSET*)
  73. (define-simple-class <DateTest> (Date)
  74. (offset init-form: (default-offset))
  75. ((get-year) :: <int>
  76. ;; Saying plain Date below doesn't work - we get (this):getDate
  77. (+ (invoke-special java.util.Date (this) 'get-year) offset)))
  78. (define-simple-class <SimpleDateTest> (<java.util.Date>)
  79. ((get-year) :: <int>
  80. (+ (invoke-special <java.util.Date> (this) 'get-year)
  81. (invoke-static <SimpleA> 'x1900))))
  82. ;; Test separate compilation of type-alias for simple class.
  83. ;; Make it a forward declaration for a better test.
  84. (define-alias my-id-class-2 <IdClass2>)
  85. (define-simple-class <IdClass1> ()
  86. (var0 allocation: 'class init: (get-new-count))
  87. (var1 init-form: (get-new-count)))
  88. (define-private counter :: <int> 0)
  89. (define (get-new-count)
  90. (set! counter (+ counter 1))
  91. counter)
  92. (define-simple-class <IdClass2> (<IdClass1>)
  93. (allocation: 'class init: (get-new-count))
  94. (var2 init-form: (get-new-count)))
  95. (module-static incr-field-function)
  96. (define incr-field-function #f)
  97. (define-simple-class <TestCapturedFieldRef> ()
  98. (var 100)
  99. ((*init* var0)
  100. (set! var (+ var var0))
  101. (set! incr-field-function
  102. (lambda (delta)
  103. (set! var (+ var delta))))))