[Info-vax] BASIC (and Horizon)

Michael S already5chosen at yahoo.com
Wed Jan 31 08:52:30 EST 2024


On Tue, 30 Jan 2024 23:28:53 -0500
Dave Froble <davef at tsoft-inc.com> wrote:

> 
> Prey tell, what structured construct will perform cleanup and exit?
> I always expected a return from routines and such.
> 

One option is named construct and named break.
Example below is from Go (golang) tutorial. Ada has something very
similar.

Loop:
    for n := 0; n < len(src); n += size {
        switch {
        case src[n] < sizeOne:
            if validateOnly {
                break
            }
            size = 1
            update(src[n])

        case src[n] < sizeTwo:
            if n+1 >= len(src) {
                err = errShortInput
                break Loop
            }
            if validateOnly {
                break
            }
            size = 2
            update(src[n] + src[n+1]<<shift)
        }
    }


Another option is defer clause.
From the same tutorial:

// Contents returns the file's contents as a string.
func Contents(filename string) (string, error) {
    f, err := os.Open(filename)
    if err != nil {
        return "", err
    }
    defer f.Close()  // f.Close will run when we're finished.

    var result []byte
    buf := make([]byte, 100)
    for {
        n, err := f.Read(buf[0:])
        result = append(result, buf[0:n]...) // append is discussed
    later. if err != nil {
            if err == io.EOF {
                break
            }
            return "", err  // f will be closed if we return here.
        }
    }
    return string(result), nil // f will be closed if we return here.
}

The third option, the one I like least (an understatement) is use of
exceptions. Despite my personal preferences, it's quite popular.




More information about the Info-vax mailing list