public inbox for kawa@sourceware.org
 help / color / mirror / Atom feed
From: Per Bothner <per@bothner.com>
To: kawa@sourceware.org
Subject: Re: Kawa type-conversions
Date: Sat, 28 Mar 2015 02:24:00 -0000	[thread overview]
Message-ID: <551610A8.4070504@bothner.com> (raw)
In-Reply-To: <3E31B880-0FD8-4C1B-8EA5-15ED388861D7@theptrgroup.com>



On 03/20/2015 05:05 PM, Jamison Hope wrote:
>
>> These aren't really consistent:
>>
>> (if (? x::int 3.4) x #f) ==> 3
>> (instance? 3.4 int) ==> 3
>
> I think you meant "==> #f" for the second one (otherwise they seem
> pretty consistentÂ…).

Right.  (instance? 3.4 int) returns #f - which is fine,
but (if (? x::int 3.4) x #f) currently returns 3 but should return #f.

>> * As an incompatible change, converting float to int should (IMO) no
>> longer be allowed for implicit conversions:
>>
>>   (->int 3.4) ==> 3
>>   (as int 3.4) ==> ERROR ;; incompatible change
>>   (define x::int 3.4) ==> ERROR ;; incompatible change
>
> Agreed, those last two just look weird.  But what about (as int 3.0)?
> In other words, is it an error to try to coerce *any* floating point
> number to an int, or just when it isn't already integer-valued? (in
> the R6RS sense).
>
> We already have `exact', so I don't have strong feelings either way,
> as long as (as int (exact 3.0)) works.

Likewise, I don't feel strongly about it, but it's easier to implement
if (as int 3.0) is an ERROR.  One important reason: If instead of a
constant you have an inexact-valued expression you want to complain
at compile-time.  So I think a reasonable specification for (as int XXX)
is that it works if and only if XXX has an exact integral type.  (It
might be reasonable to do range-checking, at run-time if needed.)

> Here's one other feature I would like to see, which I *think* fits in with
> your description of (convert TYPE VALUE) and (->TYPE VALUE).
>
> I would like a way to extend the system with custom conversion functions
> for particular from/to type pairs.  This would be a big help when
> interacting with different Java libraries.

I agree that could be useful.  Of course, if we're distinguishing between
explicit and implicit conversions we need to specify which custom conversions
are explicit and which are implicit.  You discuss an extra search mechanism
(convention) for explicit conversion, but I don't understand how
that so much more convenient than just re-defining the ->TYPE function.

Customization of *implicit* conversion seems more interesting.
Since we want static type-checking, that seems to imply that the
custom conversion functions must be visible at compile-time.

For example something like:

(define-conversion (name::T1)::T2 (convert-T1-to-T2 name))

This would get compiled to a static method:

public static T2 $convert_to$T2(T1 name) {convert_T1_to_T2(name); }

When needing to do an implicit type conversion, the compiler picks
"the most specific applicable" (in the sense of method overload resolution)
conversion method and compiles in a call to the corresponding static method.

I don't know how practical and safe this is.  It could certainly
easily be abused.

> So perhaps (->TYPE VALUE) and (convert TYPE VALUE) could work
> something like this:
>
> 1. If VALUE is an instance of TYPE, just return it.
>
> 2. If VALUE has a "toTYPE" method, invoke that.
>
> 3. If TYPE has a one-argument constructor which works on VALUE,
>     then return new TYPE(VALUE).

Actually, we want to prefer factory methods over constructors.
So perhaps:

> 3a. If TYPE has a static one-argument method which works on VALUE,
>     named 'valueOf' then return TYPE.valueOf(VALUE).  Likewise for 'make'.

>
> 4. Otherwise, starting with VALUE.getClass() and working up to
>     java.lang.Object, look for a function "FQCN->TYPE".  If one
>     is found, then invoke it.
>
> Implemented interfaces should probably be in that search list, too,
> and I'm not sure what to do about type aliases.  I used
> "xxx->complex" for the function names in that example, but maybe it
> should be "xxx->gnu.math.Complex".

-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/

  reply	other threads:[~2015-03-28  2:24 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-20 20:06 Per Bothner
2015-03-21  0:05 ` Jamison Hope
2015-03-28  2:24   ` Per Bothner [this message]
2015-03-30 16:32     ` Jamison Hope

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

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

  git send-email \
    --in-reply-to=551610A8.4070504@bothner.com \
    --to=per@bothner.com \
    --cc=kawa@sourceware.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).