<asterite>
To those wondering, the change to single letters has nothing to do with performance
<asterite>
Single uppercase letter is used for free vars
<asterite>
>> def foo(x : T); T; end; foo 1
<DeBot>
asterite: Sorry, that took too long.
<asterite>
Well, that should print Int32
shama has quit [Quit: (╯°□°)╯︵ɐɯɐɥs]
<asterite>
but in generic types, like Array(T), you can use T as a free var, or to match something
<asterite>
(match something once the array is instantiated, free var if it's not instantiated)
<asterite>
and since free vars are single-letter, type vars must also be single-letters
asterite has quit [Client Quit]
ismaelga has quit [Remote host closed the connection]
asterite has joined #crystal-lang
<asterite>
another option could have been using new syntax for free vars, like def foo(x : T') or something, but that adds more noise and syntax. We think the single letter rule is a simple rule and a good compromise
asterite has quit [Client Quit]
Cidan is now known as zz_Cidan
<vikaton>
wait w0t
<vikaton>
im confused
asterite has joined #crystal-lang
<asterite>
>> Int32
<DeBot>
asterite: Int32
<asterite>
>> def foo(x : T); T; end; foo 1
<DeBot>
asterite: Int32
<asterite>
>> Array.new(10, 0)
<DeBot>
asterite: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
<asterite>
>> Array(Char).new(10, 0)
<DeBot>
asterite: Error in line 3: no overload matches 'Array(Char)#initialize' with types Int32, Int32
<asterite>
See? That method, `new`, has a type restriction T. If you don't specify the generic type of the array, it is used as a free var to deduce the type
<asterite>
However, when you specify it, it becomes a restriction against the specific type (in this case Char)
<asterite>
So the T in the constructor is acting both as a free var and as a type restriction, depending on the use case
<asterite>
Previous to that commit, anything that was not found (say, x : Something) was considered a free var, but that's bugprone. So we changed the rule to: free vars are single uppercase letters. With that rule, we must force generic type vars to be single uppercase letters.
<asterite>
It's kind of hard, but that's the reason :)
DerisiveLogic has quit [Ping timeout: 276 seconds]
HakanD_ has joined #crystal-lang
waterlink has joined #crystal-lang
BlaXpirit has joined #crystal-lang
shama has joined #crystal-lang
shama has quit [Ping timeout: 265 seconds]
Ven_ has joined #crystal-lang
HakanD_ has quit [Quit: Be back later ...]
leafybasil has quit [Remote host closed the connection]
waterlink has quit [Ping timeout: 252 seconds]
waterlink1 has joined #crystal-lang
shama has joined #crystal-lang
leafybasil has joined #crystal-lang
shama has quit [Ping timeout: 250 seconds]
leafybasil has quit [Remote host closed the connection]
leafybasil has joined #crystal-lang
waterlink1 has quit [Ping timeout: 256 seconds]
leafybas_ has joined #crystal-lang
leafybasil has quit [Ping timeout: 240 seconds]
wanderer_ has joined #crystal-lang
blaix has joined #crystal-lang
shama has joined #crystal-lang
Ven_ has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
Ven_ has joined #crystal-lang
Ven_ has quit [Client Quit]
shama has quit [Ping timeout: 265 seconds]
vegai has left #crystal-lang [#crystal-lang]
wanderer_ has quit [Quit: Page closed]
vikaton has joined #crystal-lang
leafybas_ has quit [Remote host closed the connection]
leafybasil has joined #crystal-lang
Ven_ has joined #crystal-lang
shama has joined #crystal-lang
shama has quit [Ping timeout: 256 seconds]
blaix has quit [Quit: Leaving.]
HakanD_ has joined #crystal-lang
shama has joined #crystal-lang
shama has quit [Ping timeout: 265 seconds]
kulelu88 has joined #crystal-lang
JBat has joined #crystal-lang
<jeromegn>
I’m having trouble creating C bindings for a lib (libmongoc). i tried the two generator that exist without much success. It either didn’t work at all or produced odd code.
<jeromegn>
I might need a tutorial or something, it can be more general… the thing is, I’m not sure what to Google to get that info
<jeromegn>
I come from Ruby and other interpreted languages though. I’ve done some Go too! but I’m probably missing some basic knowledge about how things like that work in general
<jeromegn>
that readme is a bit minimal :)
<jhass>
I'm not sure what your question is though, do you have some specific ones?
<jeromegn>
are those what the Void and Void* are for?
<jhass>
but typedef is an alias so to say
<jeromegn>
interesting
<jeromegn>
ah yea I see that now in the docs
<jhass>
alias MongoClientT = ActualMongoClientT but you would just define the actual thing directly
<jhass>
the alias is not useful in the binding
<jhass>
or type, yeah
<jeromegn>
so, this should be different? fun client_new = "mongoc_client_new"(uri_string : UInt8*) : MongocClientT*
<jeromegn>
it should return something else than that MongocClientT?
<jeromegn>
(thanks for bearing with me, I have very little experience with compiled languages)
<jhass>
if you properly defined MongoClientT, that's fine
<jhass>
if you never need to access the actual struct, but just obtain references to it and pass them elsewhere, it's easier to alias it to Void*
<jeromegn>
interesting
<jhass>
alias (or type) MongoClientT = Void*; fun ... : MongoClientT
<jhass>
so that's if you never need to MongoClientT.new or client.some_field
<jhass>
but solely pass pointers around
<jeromegn>
but in any case, an empty struct would also work
<jhass>
no
<jhass>
well, only if you have only ever have pointers to it and don't allocate it yourself
<jhass>
but it's still inferior to an alias then
<jhass>
mongo_wants_a_client_t(MongoClientT.new) would fail since it wouldn't allocate enough memory
<jhass>
with the empty struct
<jeromegn>
ah
<jeromegn>
that’s good to know
<jeromegn>
seems safe to use a Void pointer, doesn’t look like it needs to allocate a new client
<jhass>
yeah, having mongoc_client_new strongly hints that
shama has joined #crystal-lang
<jeromegn>
:)
havenwood has joined #crystal-lang
<jeromegn>
this is already making much more sense
<jhass>
if you have a basic C usage example, I found it helps to try to translate it quite literally and bind only the stuff you need for it to work, to get started, IME
shama has quit [Ping timeout: 264 seconds]
<jeromegn>
yea, I’m going to try that
<jeromegn>
it seems like it always returns pointers
<jeromegn>
if I want to get the actual value from the pointer in memory, how would I go about doing that?
<jeromegn>
probably has nothing to do with c bindings at this point
<jeromegn>
pointers seem mostly hidden in Crystal
<jhass>
well, depends on what you mean with actual value
<jhass>
Foo* is btw just syntax sugar for Pointer(Foo)
<jhass>
so there's an actual Pointer class
<jeromegn>
fun uri_get_string = "mongoc_uri_get_string"(uri : URI*) : UInt8*
<jeromegn>
yes
<jeromegn>
I want to get that string it’s returning a pointer of :)
<jhass>
for UInt8* there's a String.new that takes it
<jeromegn>
ohh
<jeromegn>
well, I’ll be damned.
<jeromegn>
also, is that normal? getting pointers to pointers? Pointer(Pointer(Void))@0x7FAF83D0F690
<kulelu88>
is there a Dockerfile for crystal?
<jhass>
jeromegn: mh, did you maybe do alias Foo = Void*; fun x() : Foo* ?
<jeromegn>
yes
<jhass>
jeromegn: while the original signature is like foo_t* ?
<vikaton>
so in fun cos(value : Float64) : Float64, this calls the cos method in C ?
kulelu88 has joined #crystal-lang
<jeromegn>
I believe so
<vikaton>
>> sizeof(String)
<DeBot>
vikaton: 4
<vikaton>
>> sizeof(Void*)
<DeBot>
vikaton: 4
Ven_ has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<jhass>
jeromegn: yes, they write wrappers, like class MongoClient; def initialize(url); @client = LibMongoC.client_new(url); end; def to_unsafe; @client; end; def uri; String.new(LibMongoC.client_get_uri(self)); end; end;
<jeromegn>
nice
<jeromegn>
I think that’s what I’ll do
<jeromegn>
i work at Compose.io, we really love Ruby, but it’s been too slow for some of our stuff. due to its blocking nature...
<jeromegn>
so I’m hoping with Crystal we can get the best of both worlds.
ponga has quit [Quit: Leaving...]
ponga has joined #crystal-lang
<vikaton>
I can't execute C bindings D:
<vikaton>
jeromegn: that would be nice!
<vikaton>
>> lib C; fun cos(value : Float64) : Float64;end;C.cos(1.5)
<DeBot>
vikaton: collect2: error: ld returned 1 exit status
<vikaton>
hm ^ ?
<vikaton>
I get the same error
<jhass>
>> @[Link("m")];lib C; fun cos(value : Float64) : Float64;end;C.cos(1.5)
<DeBot>
jhass: 0.0707372
leafybas_ has joined #crystal-lang
leafybasil has quit [Ping timeout: 264 seconds]
HakanD__ has joined #crystal-lang
shama has joined #crystal-lang
HakanD_ has quit [Ping timeout: 256 seconds]
shama has quit [Ping timeout: 256 seconds]
<vikaton>
oh
Ven_ has joined #crystal-lang
<vikaton>
what is"m" ?
<vikaton>
C binding libs seem like interfaces
leafybas_ has quit [Read error: Connection reset by peer]
leafybasil has joined #crystal-lang
vikaton has quit []
<BlaXpirit>
libm is glibs
<BlaXpirit>
glibc
<jeromegn>
jhass: got some more question(s). now I’m trying to create the bindings for libbson (BSON) and for some reason, MongoDB decided to put all those checks that raise “Only <bson.h> can be included directly.”
<jeromegn>
I’m wondering if there’s a way around that. I removed the checks from a copy of the .h files, however that breaks for other reasons. I’m assuming the checks might have been important.
<jeromegn>
I guess I might have to do that whole thing manually..
HakanD__ has quit [Read error: Connection reset by peer]
shama has joined #crystal-lang
HakanD__ has joined #crystal-lang
shama has quit [Quit: (╯°□°)╯︵ɐɯɐɥs]
leafybasil has quit [Remote host closed the connection]
leafybasil has joined #crystal-lang
strcmp1 has quit [Ping timeout: 272 seconds]
HakanD___ has joined #crystal-lang
HakanD__ has quit [Ping timeout: 272 seconds]
strcmp1 has joined #crystal-lang
willlll has joined #crystal-lang
willlll has quit [Client Quit]
willlll has joined #crystal-lang
Ven_ has quit [Quit: My MacBook has gone to sleep. ZZZzzz…]
<BlaXpirit>
jeromegn, what about porting instead of wrapping
<jeromegn>
you mean, writing a new driver and all?
<BlaXpirit>
nvm, it's too bloated :|
<jeromegn>
hehe
<jeromegn>
it’s huge
<jeromegn>
the C driver has all the features
<jeromegn>
rewriting them seems wasteful
bcardiff has joined #crystal-lang
blaix has joined #crystal-lang
<jeromegn>
so would: uint8_t hello[120] == StaticArray(UInt8, 120) ?
<jeromegn>
>> ‘0’
<DeBot>
jeromegn: Error in line 3: undefined local variable or method '‘0’'
leafybas_ has joined #crystal-lang
leafybasil has quit [Ping timeout: 252 seconds]
leafybas_ has quit [Ping timeout: 240 seconds]
<jeromegn>
>> String.new(1_u8)
<DeBot>
jeromegn: Error in line 3: no overload matches 'String::new' with types UInt8
<jeromegn>
>> String.new(pointerof(1_u8))
<DeBot>
jeromegn: Error in line 3: can't take address of 1_u8
ismaelga has quit [Remote host closed the connection]
<jhass>
jeromegn: depends on what data you want to write there
<jeromegn>
well
<jeromegn>
the C thing populates a “char str[25]"
<jeromegn>
so I pass it “str :: StaticArray(UInt8, 25)”
<jeromegn>
and it populates it correctly, but then I don’t know how to get the string out of it
<jeromegn>
hmm
<jhass>
so you want a string, use a string
<jeromegn>
a MongoDB ObjectID usually looks like “53sada245dss35sdfs3”
<jeromegn>
“Converts oid into a hex encoded string."
<jeromegn>
my program just exits with “Program terminated abnormally with error code: 11"
<jeromegn>
which reminds me, I don’t know how to debug that
<jeromegn>
no stack trace or anything
<jhass>
try running it through valgrind
<jeromegn>
I did modify the code a bit though
<jeromegn>
if I use the exact thing you wrote, I get invalid voerload
<jeromegn>
overload
<jeromegn>
no overload matches 'String::new' with types StaticArray(UInt8, 25)
<jeromegn>
which makes sense
<jeromegn>
anyway, I can do it the long way for now
<jeromegn>
not a big deal.
HakanD___ has quit [Quit: Be back later ...]
ismaelga has joined #crystal-lang
<jhass>
jeromegn: if you have a UInt8 static array there's no need for the map
<jhass>
just .to_unsafe it
<jeromegn>
indeed
<jeromegn>
nice
<jeromegn>
dang, there’s no Time.new(seconds)
<jeromegn>
had to jump around a lot
<jhass>
>> Time.new(245)
<DeBot>
jhass: 0001-01-01 00:00:00
<jhass>
?
<jhass>
>> Time.new(10000000)
<DeBot>
jhass: 0001-01-01 00:00:01
<jeromegn>
it accepts ticks
<jeromegn>
but not seconds
<jeromegn>
time_t = LibBSON.oid_get_time(self)
<jeromegn>
time_spec = LibC::TimeSpec.new
<jeromegn>
time_spec.tv_sec = time_t
<jeromegn>
time_spec.tv_nsec = time_t
<jeromegn>
Time.new(time_spec)
<jeromegn>
it also accepts a LibC::TimeSpec
<jhass>
yeah, would be a good addition
<jhass>
def self.at_epoch(seconds : Int)
<jeromegn>
indeed
<jhass>
wanna pull request it? ;)
<jeromegn>
yea
<jeromegn>
I’ll fork
Cidan is now known as zz_Cidan
<jhass>
we got an open class model too btw, so I often just accumulate a bunch of core extensions while developing something to move on and upstream what I really use when done
<jeromegn>
good idea
zz_Cidan is now known as Cidan
<jeromegn>
so I don’t think the lines I pasted are right
<jeromegn>
apparently, the libbson lib has macros in it that would simplify my life
<jeromegn>
I wonder if I can bind to those
<jeromegn>
I got a weird error
<jeromegn>
Undefined symbols for architecture x86_64:
<jeromegn>
"_BSON_APPEND_UTF8", referenced from:
<jeromegn>
fun append_utf8 = "BSON_APPEND_UTF8"(doc : Document*, key : UInt8*, value : UInt8*) : Bool
<jhass>
no, C macros are string replacements during compile time (actually in the preprocessor phase, so before compile)
<jeromegn>
ah
<jeromegn>
that’s alright I guess
<jeromegn>
it’s not a huge deal
<jhass>
you could only reimplement them in crystal macros or functions
waterlink has joined #crystal-lang
DerisiveLogic has joined #crystal-lang
HakanD___ has joined #crystal-lang
<jeromegn>
can we have default args for `fun` in c bindings?
<jhass>
I don't think so
<BlaXpirit>
jeromegn, if it works, it works. if not, adding it is kinda controversial
<jeromegn>
hehe
<jeromegn>
it actually doesn’t make sense in my case, I was mostly curious.
<jeromegn>
>> 0x00
<DeBot>
jeromegn: 0
<jeromegn>
>> 0x01
<DeBot>
jeromegn: 1
<jeromegn>
>> 0xFF
<DeBot>
jeromegn: 255
<jeromegn>
ah
aemadrid has quit [Quit: Connection closed for inactivity]
<jeromegn>
>> (1 << 0)
<DeBot>
jeromegn: 1
<jeromegn>
>> (1 << 3)
<DeBot>
jeromegn: 8
HakanD___ has quit [Quit: Be back later ...]
JBat has joined #crystal-lang
ismaelga has quit [Remote host closed the connection]
JBat has quit [Quit: Computer has gone to sleep.]
JBat has joined #crystal-lang
willlll has quit [Quit: willlll]
JBat has quit [Client Quit]
JBat has joined #crystal-lang
bcardiff has quit [Quit: Leaving.]
JBat has quit [Remote host closed the connection]
ismaelga has joined #crystal-lang
JBat has joined #crystal-lang
DerisiveLogic has quit [Ping timeout: 256 seconds]
<fowl>
BlaXpirit: am I reading crsfml right, you declare types like type CircleShape = Void* and then open them with class to define methods? Or are those classes wrappers
<BlaXpirit>
fowl, _lib files are one thing
<BlaXpirit>
and the rest is a different thing
<BlaXpirit>
however.
<fowl>
I saw the Wrapper module
<BlaXpirit>
i don't even know how to explain
<fowl>
:p I'm trying to make sdl2 wrapper have that aww nice oop feel
<BlaXpirit>
why do you insist on sdl2?
<BlaXpirit>
i seriously don't get why people don't just use sfml
<jhass>
that's easy: "there this stuff that does stuff and there's this other stuff that does other stuff and they together form that staff"
<fowl>
Sfml only recently supports android ios
<BlaXpirit>
gonna support them earlier than crystal :|
<jhass>
mmh, googles adventure to port Dart to android could expose interesting APIs
bcardiff has joined #crystal-lang
<fowl>
It wouldn't be hard for crystal to run on android..I'm not volunteering for it tho
<BlaXpirit>
so you haven't provided arguments :|
<fowl>
I have a c++ compiler with sdl2 on my phone
<BlaXpirit>
could you please just use sfml? that would make me happy :D
<fowl>
BlaXpirit: arguments for sdl2? I don't have any other than portability. I won't be using the drawing APIs
<BlaXpirit>
what else then?
<BlaXpirit>
it's not like there is a proper GL wrapper either
<jhass>
don't we have like 3 sdl2 bindings already? you people should join efforts ;P
<fowl>
There is
<BlaXpirit>
have you tried to use it?
<fowl>
The SOIL stuff crashes for me
<fowl>
Other than that it works
leafybasil has joined #crystal-lang
bcardiff has quit [Quit: Leaving.]
bcardiff has joined #crystal-lang
bcardiff1 has joined #crystal-lang
bcardiff has quit [Read error: Connection reset by peer]
<fowl>
jhass: I'm not trying to split them up more, I only found 2 recent looking repos tho and one points to the other one
<jhass>
mh
<BlaXpirit>
fowl, so do you want me to try again to explain?
<BlaXpirit>
lib files just define a low level wrapper, they're very consistent and easy to understand
<fowl>
BlaXpirit: can you explain @owned? How are destructors wired up
<BlaXpirit>
then there are _lib files and suffixless files
<BlaXpirit>
the only difference between them is that one is generated one is manually written
<BlaXpirit>
but they must be considered together as a whole
<BlaXpirit>
wait i mean
<BlaXpirit>
_obj and suffixless
<BlaXpirit>
sory
<BlaXpirit>
so. remember like in nim we wrap some of CSFML's stuff as object, and some as ptr object
<BlaXpirit>
so small objects are just structs here
<BlaXpirit>
i do NOT wrap them. but I reopen the things that are defined in lib
<BlaXpirit>
and just alias it
<BlaXpirit>
then there are analogs of ptr objects - those that have Create and Destroy
<BlaXpirit>
I wrap those ones in classes
<fowl>
Where is @owned used to decide if the ptr should be destroyed
<fowl>
Ah ok
<BlaXpirit>
so. when I Create a CSFML object, i write @owned = true
<BlaXpirit>
the finalizer just checks if it is @owned
<BlaXpirit>
dup also creates new objects, so @owned is set to true there too
<fowl>
When do you not own a ptr from sfml
<BlaXpirit>
I pretty much just check if the function name has "create" or "copy" in it - then it's owned
<BlaXpirit>
otherwise it's just for temporary use
<BlaXpirit>
and instead of writing @owned = true transfer_ptr does it
<fowl>
Yea
<BlaXpirit>
especially because dup/copy doesn't have access to @owned
<fowl>
Thanks for the explanation
<crystal-gh>
[crystal] yyyc514 opened pull request #631: Fixes to StringFormatter (master...string_formatter) http://git.io/vJS9S
<Dreamer3>
ok just need to maek sure all the other tests still pass
BlaXpirit has quit [Quit: Quit Konversation]
blaix has quit [Quit: Leaving.]
kulelu88 has quit [Ping timeout: 255 seconds]
<jeromegn>
anybody know why I’d have to specify the type of return value in a Proc *only* when I do a string interpolation?
<jeromegn>
-> (x : Hash(String, String)){ “hello” } works, but -> (x : Hash(String, String)){ “hello #{x[“name”]}” } complains that I need to us “as” to specify the return type.
<jeromegn>
that example looks weird probably… this is in the context of a method that accepts a Proc with specific return values
<DeBot>
jeromegn: Syntax error in eval:4: expecting token '}', not 'end'
<jeromegn>
>> def hello(proc : Proc(String, String | NoReturn)); proc.call; end; hello do |x|; return “ah #{x}”; end
<DeBot>
jeromegn: Syntax error in eval:7: expecting identifier 'end', not 'EOF'
<jeromegn>
ok well, I’ll work with that locally hehe
strcmp2 has joined #crystal-lang
strcmp1 has quit [Ping timeout: 252 seconds]
<crystal-gh>
[crystal] asterite pushed 4 new commits to master: http://git.io/vJ9mc
<crystal-gh>
crystal/master e4e72a0 Josh Goebel: formatting type should be generic...
<crystal-gh>
crystal/master 9a62674 Josh Goebel: [strings] support "%X" and "%x" properly...
<crystal-gh>
crystal/master 0a452ef Josh Goebel: [string] %i and %d should have same behavior...
<crystal-gh>
[crystal] will closed pull request #627: Try, but fail, to reorder structs (master...failed-attempt-to-reorder-structs) http://git.io/vJif2
<jeromegn>
jhass: apprently it’s necessary :( I’m trying to get a value from the BSON document. I need to define that struct properly to know which type of value it is.
<jeromegn>
at least, I think.
<crystal-gh>
[crystal] asterite pushed 5 new commits to master: http://git.io/vJ98d
<crystal-gh>
crystal/master a45d1d6 Ary Borenszweig: Added some docs