From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 14403 invoked by alias); 6 Mar 2014 06:13:52 -0000 Mailing-List: contact kawa-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: kawa-owner@sourceware.org Received: (qmail 14393 invoked by uid 89); 6 Mar 2014 06:13:51 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL,BAYES_20,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mail.theptrgroup.com Received: from mail.theptrgroup.com (HELO mail.theptrgroup.com) (71.178.251.9) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 06 Mar 2014 06:13:49 +0000 Received: from [10.11.21.22] (unknown [10.11.21.22]) by mail.theptrgroup.com (Postfix) with ESMTPS id 20500E0937 for ; Thu, 6 Mar 2014 01:13:47 -0500 (EST) From: Jamison Hope Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Subject: enums and case Message-Id: <21EE67EB-02CA-4E60-9A8A-01DD09C6C1B0@theptrgroup.com> Date: Thu, 06 Mar 2014 06:13:00 -0000 To: "kawa@sourceware.org list" Mime-Version: 1.0 (Mac OS X Mail 6.6 \(1510\)) X-IsSubscribed: yes X-SW-Source: 2014-q1/txt/msg00107.txt.bz2 It occurs to me that it would be very handy if we could use Java enums in Scheme case expressions the way that they can be used in Java switch statements: > public enum Day { > SUNDAY, MONDAY, TUESDAY, WEDNESDAY, > THURSDAY, FRIDAY, SATURDAY > } > > Day day = ...; > > switch (day) { > case MONDAY: ... > case FRIDAY: ... > ... > } becomes something like > (define-enum Day (SUNDAY MONDAY TUESDAY ...)) > > (define d ::Day ...) > > (case d ((MONDAY) ...) ((TUESDAY) ...) ...) There are several issues which must be addressed: 1. How to recognize in (case d ((MONDAY) ...) ...) that we intend to compare against the enum constant Day:MONDAY and not the symbol named "MONDAY". Should this depend upon the type declaration for d? In Java, this is a compile-time error: > Object day = Day.MONDAY; switch (day) { /* ... */ } Is it acceptable for the presence or absence of a type declaration to alter the semantics like this in Kawa? If so, can a macro defined in Scheme get to a variable's gnu.expr.Declaration, or would case need to be rewritten as a Java class extending Syntax? 2. Would it be preferable to require qualified constants, as in (case d ((Day:MONDAY) ...) ((Day:TUESDAY) ...) ...)? In that case we wouldn't need to chase the declaration for d, but on the downside it's more verbose. 3. The current implementation of case checks for matches with each datum via (eqv? key (quote datum)). That means that here we would get an expansion of (eqv? d (quote Day:MONDAY)), assuming that we require qualified constants. But (quote Day:MONDAY) evaluates to the symbol with the name "Day:MONDAY", not to the constant named "MONDAY" of the enum class Day. Should quote recognize that there's a colon in there and check for an enum constant before constructing a symbol? After all, '3 evaluates to an integer, not the symbol named "3". But what if we actually do want to have the symbol? 4. Should this be a separate macro, "enum-case"? But standard case already handles booleans and numbers and symbols, so why should enums require a separate facility? We would want to do the checking at compile time to catch spelling errors or other mistakes such as (case d ((JANUARY) ...) ...), so while altering %case-match like this works (kind of), it isn't the Right Thing: > (define-syntax %case-match > (syntax-rules () > ((%case-match key datum) > (if (*:isEnum key:class) > (eq? key (key:class:valueOf (quote datum))) > (eqv? key (quote datum)))) > ((%case-match key datum more ...) > (or (if (*:isEnum key:class) > (eq? key (key:class:valueOf (quote datum))) > (eqv? key (quote datum))) > (%case-match key more ...))))) (Besides, it's horribly inefficient, calling Class#isEnum over and over again.) Thoughts? Once we decide upon the right semantics, this should be a fairly small task to complete. Any potential GSoC students want to give it a try? -- Jamison Hope The PTR Group www.theptrgroup.com