From d9111ecc5dfce5e5ab396d4110aca30f48bdd310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 9 Apr 2018 10:45:52 +0200 Subject: [PATCH 1/2] Fix c_bindings/fun can have upper case name --- syntax_and_semantics/c_bindings/fun.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/syntax_and_semantics/c_bindings/fun.md b/syntax_and_semantics/c_bindings/fun.md index a6a173e9a..f93b763ae 100644 --- a/syntax_and_semantics/c_bindings/fun.md +++ b/syntax_and_semantics/c_bindings/fun.md @@ -47,7 +47,11 @@ X.variadic(1, 2, 3, 4) Note that there are no implicit conversions (except `to_unsafe`, which is explained later) when invoking a C function: you must pass the exact type that is expected. For integers and floats you can use the various `to_...` methods. -Because method names in Crystal must start with a lowercase letter, `fun` names must also start with a lowercase letter. If you need to bind to a C function that starts with a capital letter you can give the function another name for Crystal: +## Function names + +Function names in a `lib` definition can start with an upper case letter. That's different from methods and function definitions outside a `lib`, which must start with a lower case letter. + +Function names in Crystal can be different from the C name. The following example shows how to bind the C function name `SDL_Init` as `LibSDL.init` in Crystal. ```crystal lib LibSDL @@ -55,7 +59,7 @@ lib LibSDL end ``` -You can also use a string as a name if the name is not a valid identifier or type name: +The C name can be put in a string to be able to write a name that is not a valid identifier: ```crystal lib LLVMIntrinsics @@ -65,6 +69,8 @@ end This can also be used to give shorter, nicer names to C functions, as these tend to be long and are usually prefixed with the library name. +## Types in C Bindings + The valid types to use in C bindings are: * Primitive types (`Int8`, ..., `Int64`, `UInt8`, ..., `UInt64`, `Float32`, `Float64`) * Pointer types (`Pointer(Int32)`, which can also be written as `Int32*`) From d579980655fa9f20530f2e4cc59efa4036642ed1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 9 Apr 2018 16:00:44 +0200 Subject: [PATCH 2/2] string -> quotes --- syntax_and_semantics/c_bindings/fun.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/syntax_and_semantics/c_bindings/fun.md b/syntax_and_semantics/c_bindings/fun.md index f93b763ae..cb0af059e 100644 --- a/syntax_and_semantics/c_bindings/fun.md +++ b/syntax_and_semantics/c_bindings/fun.md @@ -59,7 +59,7 @@ lib LibSDL end ``` -The C name can be put in a string to be able to write a name that is not a valid identifier: +The C name can be put in quotes to be able to write a name that is not a valid identifier: ```crystal lib LLVMIntrinsics