guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hidden feature of vector-copy or bug?


From: Damien Mattei
Subject: Re: hidden feature of vector-copy or bug?
Date: Mon, 7 Aug 2023 15:54:41 +0200

just understood there is 2 versions of vector-copy ;one is srfi 43 and doc says:

Scheme Procedure: vector-copy vec [start [end [fill]]]

Allocate a new vector whose length is end - start and fills it with
elements from vec, taking elements from vec starting at index start
and stopping at index end. start defaults to 0 and end defaults to the
value of (vector-length vec). If end extends beyond the length of vec,
the slots in the new vector that obviously cannot be filled by
elements from vec are filled with fill, whose default value is
unspecified.


On Sun, Aug 6, 2023 at 11:13 AM Damien Mattei <damien.mattei@gmail.com> wrote:
>
> hello,
>
> i'm maintaining and developing some code i wrote which is using
> something is a hidden feature or bug in vector-copy?
>
> here is the code but there no need to understand it, the problem is
> explained after
>
> ;; scheme@(guile-user)> (define gva (growable-vector 1 2 3 4 5))
> ;; scheme@(guile-user)> (define gva2 (growable-vector-resize gva 8))
> ;; growable-vector-resize : new-vector :#(1 2 3 4 5 #<unspecified>
> #<unspecified> #<unspecified>)
> ;; scheme@(guile-user)> (describe gva2)
> ;; #<<growable-vector> 10394d640> is an instance of class <growable-vector>
> ;; Slots are:
> ;;      v = #(1 2 3 4 5 #<unspecified> #<unspecified> #<unspecified>)
> (define-method (growable-vector-resize (gv <growable-vector>)
> (new-size <integer>)) ;; fill unspecified
>   (define actual-size (vector-length gv))
>   (define old-vector (growable-vector-v gv))
>   (define new-vector (vector-copy old-vector 0 new-size))
>   ;;(display "growable-vector-resize : new-vector :") (display
> new-vector) (newline)
>   (growable-vector-set-v! gv new-vector))
>
> it seems that vector-copy when given a end-index greater than the
> length of vector to copy does not return an error but create a new
> vector of the good size indeed:
>
> scheme@(guile-user)> (define v (make-vector 7 1)
> )
> scheme@(guile-user)> v
> $2 = #(1 1 1 1 1 1 1)
> scheme@(guile-user)> (vector-copy v)
> $3 = #(1 1 1 1 1 1 1)
> scheme@(guile-user)> (vector-copy v 0 10)
> $4 = #(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)
>
> here 10 is greater than (length v) but it works and return:
> #(1 1 1 1 1 1 1 #<unspecified> #<unspecified> #<unspecified>)
>
> but the doc does not specify that:
> "Scheme Procedure: vector-copy vec [start [end]]C Function:
> scm_vector_copy (vec)
>
> Returns a freshly allocated vector containing the elements of vec in
> the range [start ... end). start defaults to 0 and end defaults to the
> length of vec."
>
> by default end is length of vec, so if i specify an end greater than
> length of vec should it not be normal to return an error?
>
> Damien



reply via email to

[Prev in Thread] Current Thread [Next in Thread]