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

Re: another take on hackers and painters



On Thu, 22 May 2003, Michael Vanier wrote:

> Of course, this isn't autoconversion, but something like this could be used
> for autoconversion.

Indeed. Here's a complete autoconverting + implemented in PLT Scheme:

(define real+ +)
(define (+ . args)
  (define (?->num x)
    (cond
      ((string? x) (or (string->number x)
                       (error '+ "expected a number string, got non-number
string ~s" x)))
      ((number? x) x)
      (else (error '+ "expected number or string, got ~s" x))))
  (apply real+ (map ?->num args)))

> (+ 3 "12")
15
> (+ "3" "12")
15
> (+ 3 12)
15
> (+ 3 "oops!")
. +: expected a number string, got non-number string "oops!"

So the argument becomes, what implementation of + is best for a language?
It could be:

* "Normal" numbers-only +

Seems like the best choice if you don't want to process text often and
want to be able to statically detect type errors arising from incorrect
use of +.

* an autoconverting, error-raising + as above

Seems like the best choice if you want to write lots of programs that deal
with data files that have simple ad-hoc structure: you don't need a lexer
and parser to deal with a file full of three-column text where column two
is a number, but if it turns out after you finish the program and
somebody's relying on it that in fact column two is only *usually* a
number, you can detect and fix your bug easily.

* an autoconverting, silent-failing +

I really can't think of a group for whom this is the best choice. Perhaps
someone can enlighten me.


-jacob