gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
Hello,

Consider the following code

(gen-interface
  :name IntStack
  :methods [[stackPeek [] int]
[stackPush [int] void]
[stackPop [] int]
[stackDepth [] int]])

(deftype IntStackImpl
  [^{:tag ints :unsynchronized-mutable true} data
   ^{:tag int :unsynchronized-mutable true} depth]
  IntStack
  (stackPeek [this]
(aget data depth))
  (stackPush [this value]
(when (= (inc depth) (alength data))
  (let [data-length (alength data)
new-data (int-array (* data-length 2))]
(System/arraycopy data 0 new-data 0 data-length)
(set! data new-data)))
(set! depth (inc depth))
(aset data depth value))
  (stackPop [this]
(if ( depth 0)
  (let [value (aget data depth)]
(set! depth (dec depth))
value)
  (throw (IllegalStateException. Stack is already empty!
  (stackDepth [this]
depth))

This is very simple stack implementation over plain java array. It does not 
compile with the following message:
CompilerException java.lang.VerifyError: (class: 
clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting 
to find integer on stack

However, when I replace the body of stackPop with, say, plain zero literal 
0, the method seems to pass the compilation, because then I'm getting 
similar error on stackPush method instead.
Placing type hints inside stackPop method does not work (in fact, it is 
even an error to place them, say, on value local binding).

What am I doing wrong here? How to make the class compile?

Cheers,
Vladimir.


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread David Nolen
I don't think you can type hint a field as a primitive array.

On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It does
 not compile with the following message:
 CompilerException java.lang.VerifyError: (class:
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero literal
 0, the method seems to pass the compilation, because then I'm getting
 similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it is
 even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to 
 clojure@googlegroups.comjavascript:_e({}, 'cvml', 
 'clojure@googlegroups.com');
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com javascript:_e({}, 'cvml',
 'clojure%2bunsubscr...@googlegroups.com');
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
Well, it is news for me since it is not documented anywhere. Why is this 
so? BTW, typehinting value in '(let [value (aget data depth)]' binding 
gives an error Can't type hint a local with a primitive initializer so I 
think since this value considered primitive then I actually can hint the 
fields to be primitive arrays.
Nonetheless, it does not look like that problem is in the field array: 
wrapping aget form with (int) does not help.

вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen 
написал:

 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It does 
 not compile with the following message:
 CompilerException java.lang.VerifyError: (class: 
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting 
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero 
 literal 0, the method seems to pass the compilation, because then I'm 
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it is 
 even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
Just tested it, swapping body of stackPop with (aget data depth) has the 
same effect as swapping it with zero, i.e. it fixes the error, 
unfortunately, not in the correct way.

вторник, 18 декабря 2012 г., 22:46:53 UTC+4 пользователь Vladimir Matveev 
написал:

 Well, it is news for me since it is not documented anywhere. Why is this 
 so? BTW, typehinting value in '(let [value (aget data depth)]' binding 
 gives an error Can't type hint a local with a primitive initializer so I 
 think since this value considered primitive then I actually can hint the 
 fields to be primitive arrays.
 Nonetheless, it does not look like that problem is in the field array: 
 wrapping aget form with (int) does not help.

 вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen 
 написал:

 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It does 
 not compile with the following message:
 CompilerException java.lang.VerifyError: (class: 
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting 
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero 
 literal 0, the method seems to pass the compilation, because then I'm 
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it is 
 even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Jim - FooBar();

On 18/12/12 18:46, Vladimir Matveev wrote:
Well, it is news for me since it is not documented anywhere. Why is 
this so? 


it is at least mentioned here in an example...look at line 6

http://clojuredocs.org/clojure_core/clojure.core/definterface

Jim


--
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread David Nolen
Oops, looking over gvec.clj it looks like I'm wrong about type-hinting
fields. However you may need to type-hint the primitive array with a string
instead of the symbol shorthand allowed by Clojure. Try the following:

^{:tag [I :unsynchronized-mutable true}

David


On Tue, Dec 18, 2012 at 1:46 PM, Vladimir Matveev dpx.infin...@gmail.comwrote:

 Well, it is news for me since it is not documented anywhere. Why is this
 so? BTW, typehinting value in '(let [value (aget data depth)]' binding
 gives an error Can't type hint a local with a primitive initializer so I
 think since this value considered primitive then I actually can hint the
 fields to be primitive arrays.
 Nonetheless, it does not look like that problem is in the field array:
 wrapping aget form with (int) does not help.

 вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen
 написал:

 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It does
 not compile with the following message:
 CompilerException java.lang.VerifyError: (class:
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero
 literal 0, the method seems to pass the compilation, because then I'm
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it is
 even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscribe@**googlegroups.com
 For more options, visit this group at
 http://groups.google.com/**group/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en

  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
It is definterface example, I have arrays only in deftype.

вторник, 18 декабря 2012 г., 23:16:09 UTC+4 пользователь Jim foo.bar 
написал:

  On 18/12/12 18:46, Vladimir Matveev wrote:
  
 Well, it is news for me since it is not documented anywhere. Why is this 
 so? 


 it is at least mentioned here in an example...look at line 6

 http://clojuredocs.org/clojure_core/clojure.core/definterface

 Jim


 

-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
Thanks, but it seems that I have found the source of problem, though I do 
not know why compiler message is so uninformative and misleading.

The following code does not work (fails with CompilerException 
java.lang.VerifyError: (class: test/Test2Impl, method: fail signature: ()V) 
Expecting to find integer on stack):

(definterface Test2
  (^void fail []))

(deftype Test2Impl
  [^{:unsynchronized-mutable true :tag int} x]
  Test2
  (fail [this]
(set! x (dec x

The following code _does_ work:

(definterface Test2
  (^void fail []))

(deftype Test2Impl
  [^{:unsynchronized-mutable true :tag int} x]
  Test2
  (fail [this]
(set! x (int (dec x)

Note that I have wrapped (dec x) into (int). This compiles and works 
flawlessy.

In fact it is fine that there is an error here because (dec) is defined 
only on objects and longs, so supplying int to it does not work as 
intended. Even my first example in this message will work if you replace 
:tag int with :tag long.
However, error message is absolutely misleading and seems to be a 
consequence of an error in the compiler...



вторник, 18 декабря 2012 г., 23:18:28 UTC+4 пользователь David Nolen 
написал:

 Oops, looking over gvec.clj it looks like I'm wrong about type-hinting 
 fields. However you may need to type-hint the primitive array with a string 
 instead of the symbol shorthand allowed by Clojure. Try the following:

 ^{:tag [I :unsynchronized-mutable true}

 David


 On Tue, Dec 18, 2012 at 1:46 PM, Vladimir Matveev 
 dpx.in...@gmail.comjavascript:
  wrote:

 Well, it is news for me since it is not documented anywhere. Why is this 
 so? BTW, typehinting value in '(let [value (aget data depth)]' binding 
 gives an error Can't type hint a local with a primitive initializer so I 
 think since this value considered primitive then I actually can hint the 
 fields to be primitive arrays.
 Nonetheless, it does not look like that problem is in the field array: 
 wrapping aget form with (int) does not help.

 вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen 
 написал:

 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It does 
 not compile with the following message:
 CompilerException java.lang.VerifyError: (class: 
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting 
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero 
 literal 0, the method seems to pass the compilation, because then I'm 
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it is 
 even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscribe@**googlegroups.com
 For more options, visit this group at
 http://groups.google.com/**group/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en

  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.comjavascript:
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+u...@googlegroups.com javascript:
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en




-- 
You received this message because you are subscribed to the Google
Groups Clojure group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this 

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread David Nolen
Ah right, sorry to mislead.


On Tue, Dec 18, 2012 at 2:26 PM, Vladimir Matveev dpx.infin...@gmail.comwrote:

 Thanks, but it seems that I have found the source of problem, though I do
 not know why compiler message is so uninformative and misleading.

 The following code does not work (fails with CompilerException
 java.lang.VerifyError: (class: test/Test2Impl, method: fail signature: ()V)
 Expecting to find integer on stack):

 (definterface Test2
   (^void fail []))

 (deftype Test2Impl
   [^{:unsynchronized-mutable true :tag int} x]
   Test2
   (fail [this]
 (set! x (dec x

 The following code _does_ work:

 (definterface Test2
   (^void fail []))

 (deftype Test2Impl
   [^{:unsynchronized-mutable true :tag int} x]
   Test2
   (fail [this]
 (set! x (int (dec x)

 Note that I have wrapped (dec x) into (int). This compiles and works
 flawlessy.

 In fact it is fine that there is an error here because (dec) is defined
 only on objects and longs, so supplying int to it does not work as
 intended. Even my first example in this message will work if you replace
 :tag int with :tag long.
 However, error message is absolutely misleading and seems to be a
 consequence of an error in the compiler...



 вторник, 18 декабря 2012 г., 23:18:28 UTC+4 пользователь David Nolen
 написал:

 Oops, looking over gvec.clj it looks like I'm wrong about type-hinting
 fields. However you may need to type-hint the primitive array with a string
 instead of the symbol shorthand allowed by Clojure. Try the following:

 ^{:tag [I :unsynchronized-mutable true}

 David


 On Tue, Dec 18, 2012 at 1:46 PM, Vladimir Matveev dpx.in...@gmail.comwrote:

 Well, it is news for me since it is not documented anywhere. Why is this
 so? BTW, typehinting value in '(let [value (aget data depth)]' binding
 gives an error Can't type hint a local with a primitive initializer so I
 think since this value considered primitive then I actually can hint the
 fields to be primitive arrays.
 Nonetheless, it does not look like that problem is in the field array:
 wrapping aget form with (int) does not help.

 вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen
 написал:

 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It
 does not compile with the following message:
 CompilerException java.lang.VerifyError: (class:
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) Expecting
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero
 literal 0, the method seems to pass the compilation, because then I'm
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it
 is even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient
 with your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscribe@**googlegrou**ps.com
 For more options, visit this group at
 http://groups.google.com/**group**/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en

  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.com

 Note that posts from new members are moderated - please be patient with
 your first post.
 To unsubscribe from this group, send email to
 clojure+u...@**googlegroups.com

 For more options, visit this group at
 http://groups.google.com/**group/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en


  --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to 

Re: gen-interface and deftype with types, compilation problem

2012-12-18 Thread Vladimir Matveev
No, you shouldn't worry about that) In fact it was useful to me to learn 
that Clojure does not support array type hints everywhere. It may be 
possible that I will have to change some of my code because of this.

вторник, 18 декабря 2012 г., 23:30:39 UTC+4 пользователь David Nolen 
написал:

 Ah right, sorry to mislead.


 On Tue, Dec 18, 2012 at 2:26 PM, Vladimir Matveev 
 dpx.in...@gmail.comjavascript:
  wrote:

 Thanks, but it seems that I have found the source of problem, though I do 
 not know why compiler message is so uninformative and misleading.

 The following code does not work (fails with CompilerException 
 java.lang.VerifyError: (class: test/Test2Impl, method: fail signature: ()V) 
 Expecting to find integer on stack):

 (definterface Test2
   (^void fail []))

 (deftype Test2Impl
   [^{:unsynchronized-mutable true :tag int} x]
   Test2
   (fail [this]
 (set! x (dec x

 The following code _does_ work:

 (definterface Test2
   (^void fail []))

 (deftype Test2Impl
   [^{:unsynchronized-mutable true :tag int} x]
   Test2
   (fail [this]
 (set! x (int (dec x)

 Note that I have wrapped (dec x) into (int). This compiles and works 
 flawlessy.

 In fact it is fine that there is an error here because (dec) is defined 
 only on objects and longs, so supplying int to it does not work as 
 intended. Even my first example in this message will work if you replace 
 :tag int with :tag long.
 However, error message is absolutely misleading and seems to be a 
 consequence of an error in the compiler...



 вторник, 18 декабря 2012 г., 23:18:28 UTC+4 пользователь David Nolen 
 написал:

 Oops, looking over gvec.clj it looks like I'm wrong about type-hinting 
 fields. However you may need to type-hint the primitive array with a string 
 instead of the symbol shorthand allowed by Clojure. Try the following:

 ^{:tag [I :unsynchronized-mutable true}

 David


 On Tue, Dec 18, 2012 at 1:46 PM, Vladimir Matveev 
 dpx.in...@gmail.comwrote:

 Well, it is news for me since it is not documented anywhere. Why is 
 this so? BTW, typehinting value in '(let [value (aget data depth)]' 
 binding 
 gives an error Can't type hint a local with a primitive initializer so I 
 think since this value considered primitive then I actually can hint the 
 fields to be primitive arrays.
 Nonetheless, it does not look like that problem is in the field array: 
 wrapping aget form with (int) does not help.

 вторник, 18 декабря 2012 г., 17:46:14 UTC+4 пользователь David Nolen 
 написал:
  
 I don't think you can type hint a field as a primitive array.

 On Tuesday, December 18, 2012, Vladimir Matveev wrote:

 Hello,

 Consider the following code

 (gen-interface
   :name IntStack
   :methods [[stackPeek [] int]
 [stackPush [int] void]
 [stackPop [] int]
 [stackDepth [] int]])

 (deftype IntStackImpl
   [^{:tag ints :unsynchronized-mutable true} data
^{:tag int :unsynchronized-mutable true} depth]
   IntStack
   (stackPeek [this]
 (aget data depth))
   (stackPush [this value]
 (when (= (inc depth) (alength data))
   (let [data-length (alength data)
 new-data (int-array (* data-length 2))]
 (System/arraycopy data 0 new-data 0 data-length)
 (set! data new-data)))
 (set! depth (inc depth))
 (aset data depth value))
   (stackPop [this]
 (if ( depth 0)
   (let [value (aget data depth)]
 (set! depth (dec depth))
 value)
   (throw (IllegalStateException. Stack is already empty!
   (stackDepth [this]
 depth))

 This is very simple stack implementation over plain java array. It 
 does not compile with the following message:
 CompilerException java.lang.VerifyError: (class: 
 clojure/data/xml/IntStackImpl, method: stackPop signature: ()I) 
 Expecting 
 to find integer on stack

 However, when I replace the body of stackPop with, say, plain zero 
 literal 0, the method seems to pass the compilation, because then I'm 
 getting similar error on stackPush method instead.
 Placing type hints inside stackPop method does not work (in fact, it 
 is even an error to place them, say, on value local binding).

 What am I doing wrong here? How to make the class compile?

 Cheers,
 Vladimir.


  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clojure@googlegroups.com
 Note that posts from new members are moderated - please be patient 
 with your first post.
 To unsubscribe from this group, send email to
 clojure+unsubscribe@**googlegrou**ps.com
 For more options, visit this group at
 http://groups.google.com/**group**/clojure?hl=enhttp://groups.google.com/group/clojure?hl=en

  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.com

 Note that posts from new members are moderated - please be patient with 
 your first post.
 To