c-interface: relaxing type checking on the scheme side
Is there anyway to specify multiple tags for a type so that, for example, I could pass either a float or an int to a c function? C's basic type coercion, in my opinion, is not a bad thing and it helps code become less cluttered. Well, lets take a step back. I just realized I can't even successfully map any primitive type to a new type. For example: (c-declare #<<END int add(int a) { return a+5; } END ) (c-define-type num (type "int" #f)) (define add (c-lambda (num) int "add")) Compiling and calling `(add 5)` throws a type error for the argument (can't convert to C type). I thought the third parameter of the 'type' expression bypassed type checking. What am I doing wrong? Assuming I get that working, I could just bypass type checking altogether, but I don't want that. I was hoping to define a type along the lines of (c-define-type real (type "float" "float int")) where the third parameter of 'type' is a space-delimited list of type tags that should pass for this type. Is this possible, or on the todo list for c-interface improvements? Thanks! -- James Long
Afficher les réponses par date
James Long wrote:
Is there anyway to specify multiple tags for a type so that, for example, I could pass either a float or an int to a c function? C's basic type coercion, in my opinion, is not a bad thing
See the two attached solutions. floatorint.scm is a type-safe variant, it lets you pass around the original value (a union type), while coerce.scm really coerces the value directly (feel free to add code to check whether a float is within the int range yourself ;)). Christian.
PS. I shouldn't have used the "NUM" identifyer for the scheme-to-c part of c-define-type; to avoid confusion, apply the attached patch. Now once you try to use the type in a c-lambda return position, you should get a more meaningful error message.
Thanks Christian - I haven't fiddled with the c-interface for a while, and that's surely the way to do it. On Feb 5, 2008 5:31 AM, Christian Jaeger <christian@pflanze.mine.nu> wrote:
PS. I shouldn't have used the "NUM" identifyer for the scheme-to-c part of c-define-type; to avoid confusion, apply the attached patch. Now once you try to use the type in a c-lambda return position, you should get a more meaningful error message.
-- James Long Coptix, Inc. longster@gmail.com
participants (2)
-
Christian Jaeger -
James Long