unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Damien Mattei <damien.mattei@gmail.com>
To: guile-user <guile-user@gnu.org>
Subject: Re: hidden feature of vector-copy or bug?
Date: Mon, 7 Aug 2023 15:54:41 +0200	[thread overview]
Message-ID: <CADEOadfcPK+dFy6sDc=eJM7HWoeb23s1-zTFcNFFWYsGN641cA@mail.gmail.com> (raw)
In-Reply-To: <CADEOadcJqRyWHa2F+-DgOvF3jO+-8u8ez7pxCAn2pLgH15LX5Q@mail.gmail.com>

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	other threads:[~2023-08-07 13:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-06  9:13 hidden feature of vector-copy or bug? Damien Mattei
2023-08-07 13:54 ` Damien Mattei [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADEOadfcPK+dFy6sDc=eJM7HWoeb23s1-zTFcNFFWYsGN641cA@mail.gmail.com' \
    --to=damien.mattei@gmail.com \
    --cc=guile-user@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).