<FromGitter>
<bew> Thanks @oprypin, I can't assume anything about the input, it can be simple ascii chars or utf-8 chars or control characters..
<oprypin>
bew, well you have to either assume or guess, except guessing is not really a thing
<FromGitter>
<bew> Can a utf-8 char be invalid? (eg. missing a byte) or will it result in another char?
<FromGitter>
<bew> (another valid)
<oprypin>
at any point in the stream you know whether you've just finished a character or waiting for more bytes
<oprypin>
it's like a prefix code, if that tells you anything
<FromGitter>
<bew> I guess I'll need to guess the format, and if nothing match, try utf-8
<FromGitter>
<bew> Yeah I see
<FromGitter>
<bew> There is no `IO#read_char?` or `IO#read_valid_char!`, assuming utf-8, how to know if the io has a valid (complete) utf-8, or only a partial char?
<oprypin>
bew, i think the method i linked woudl do it
qchmqs has quit [Read error: Connection reset by peer]
<FromGitter>
<bew> Ok I got it, setting the io's encoding to raise on invalid char will do it, thanks for tips @oprypin :+1:
Yxhuvud has quit [Remote host closed the connection]
Yxhuvud has joined #crystal-lang
Aytan72 has joined #crystal-lang
Aytan72 has quit [Ping timeout: 245 seconds]
<crystal-gh>
[crystal] meew0 opened pull request #4349: Add a simple method to run a block every X seconds (master...feature/every) https://git.io/v9CR6
zipR4ND has joined #crystal-lang
bjz_ has joined #crystal-lang
bjz has quit [Ping timeout: 258 seconds]
zipR4ND has quit [Ping timeout: 255 seconds]
Aytan72 has joined #crystal-lang
Aytan72 has quit [Ping timeout: 260 seconds]
zipR4ND has joined #crystal-lang
bjz has joined #crystal-lang
bjz_ has quit [Ping timeout: 240 seconds]
zipR4ND has quit [Ping timeout: 246 seconds]
pta2002 has joined #crystal-lang
Qchmqs has quit [Ping timeout: 240 seconds]
<FromGitter>
<codingphasedotcom> hey guys
<FromGitter>
<elthariel> yo
<pta2002>
hi
<FromGitter>
<MatthewBennington> hi
<pta2002>
(sorry for probably dumb question) How do I have a C string as argument on a binding?
<FromGitter>
<elthariel> fun strlen(Int8*) : Int32
<pta2002>
e.g. the function i want to bind takes in const char*, what do I put on the crystal side
<FromGitter>
<MatthewBennington> I believe it would be `LibC::Char*`
<oprypin>
pta2002, i dont like starting with cruft
<pta2002>
i see
<FromGitter>
<codingphasedotcom> the thing is that its not just crystal code theres also javascript packages
<FromGitter>
<codingphasedotcom> I might endup using yeoman
<oprypin>
oh my what is even going on here
<oprypin>
(no need to answer)
<oprypin>
i mean, shards does put the whole folder available somewhere so there's a chance that it'll just work , put probably not. i dont know
<FromGitter>
<codingphasedotcom> yea
<FromGitter>
<codingphasedotcom> my project is more of a stack than a shard
<FromGitter>
<codingphasedotcom> so its react over crystal kemal and yarn
<FromGitter>
<codingphasedotcom> and what I want to do is some how make it easier for developers to just have all the tools one project
<FromGitter>
<codingphasedotcom> but I'm adding new features and libraries to the project everyday and I want a way that it make it easier for people to download the latest version when they starting a new project
<oprypin>
shards is not designed to handle non-crystal dependencies
<FromGitter>
<codingphasedotcom> ok cool
<FromGitter>
<codingphasedotcom> I will take a look at how the mean stack does it
<FromGitter>
<codingphasedotcom> thanks
unshadow has quit [Ping timeout: 240 seconds]
sz0 has quit [Quit: Connection closed for inactivity]
unshadow has joined #crystal-lang
<pta2002>
i don't have much experience with shards, but maybe you can have an npm package.json and have npm install run on clone?
unshadow has quit [Client Quit]
<oprypin>
its possible to run custom command on shards install
<FromGitter>
<MatthewBennington> I haven't stared the repo yet
<FromGitter>
<bew> :+1:
<FromGitter>
<tekjar> Is no. of public events directly proportional to rate of increase of stars? :P
<FromGitter>
<MatthewBennington> I think I'm number 8000
<oprypin>
yay
<FromGitter>
<Sija> congrats!
<FromGitter>
<Sija> next stop 8080
<FromGitter>
<Sija> proxy star number
<pta2002>
8000 stars, nice! :D
<crystal-gh>
[crystal] oprypin opened pull request #4351: Fail the Travis check as soon as one of the jobs fails (master...patch-3) https://git.io/v9CP1
<Papierkorb>
8086 would also be kinda neat :P
<crystal-gh>
[crystal] elthariel opened pull request #4352: [RFC] Add Sysctl interface support for OSX/Linux/FreeBSD (master...feature/sysctl) https://git.io/v9CPx
<Papierkorb>
with an accompanying Crystaldisk operating system
<pta2002>
alright this is probably very dumb
<pta2002>
but how do you do explicit type interference on variable declaration
<pta2002>
outside of classes or function arguments
<FromGitter>
<MatthewBennington> `foo : string`, I believe
<pta2002>
what I'm trying right now is var : Type = value
<pta2002>
but that doesn't seem to be working
<pta2002>
e.g. for c : UInt32 = 3, 'type must be UInt32, not Int32'
<FromGitter>
<Sija> it’s about the casting
<FromGitter>
<Sija> u need to use `Int32#to_u`
<pta2002>
what do you mean?
<pta2002>
ooh
<FromGitter>
<Sija> `c : UInt32 = 3.to_u`
<FromGitter>
<MatthewBennington> or `c = 3_u32`
<FromGitter>
<Sija> yep
<FromGitter>
<Sija> even better since it’s compile time instead of runtime
<oprypin>
with llvm that line is probably blurred in this case
<FromGitter>
<MatthewBennington> I'm not sure you can declare a type explicitly and define a variable at the same time, but I could be wrong.
<oprypin>
you kinda can
<FromGitter>
<MatthewBennington> kinda?
<oprypin>
but it's (almost?) never useful
<pta2002>
i have a use!
<pta2002>
which is what i'm trying to get working
<pta2002>
say I have a lib
<oprypin>
nope, that's different
<pta2002>
oh
<oprypin>
you were putting an Int32 literal and trying to assign it to a UInt32 type
<oprypin>
so you just want an UInt32 literal
<oprypin>
anyhow.. `a = 5.as(Int32 | Int64)`
<pta2002>
yeah I thought it'd turn the number into an UInt32
<pta2002>
either way, if I have a type on a lib
<pta2002>
say, type Ui32 = UInt32 (this is useless i know), I can't seem to get a constant to be that type?
<oprypin>
pta2002, `type` and `alias` have different uses. mainly, `type` creates a new type that acts just likethat one but is different for the purposes of type checks
<pta2002>
yeah I know
<pta2002>
so how do I create a constant of that type?
<oprypin>
ASDF = 5u32.as(Lib::Ui32)
<oprypin>
should work ... but why?
<pta2002>
oooh
<pta2002>
oprypin: flags
<oprypin>
i'm not entirely sure if my code will work, it might even be impossible
<oprypin>
pta2002, that's @[Flags] enum
<pta2002>
and now that I think of it, I should probably be using an enum for this
<RX14>
I just read about extracting it to multiple files
<RX14>
and anywhere apart from lib_c using macro skip would be the best way to implement that
<FromGitter>
<Sija> yeah, but that’s IMO another issue
<RX14>
hmm?
<FromGitter>
<Sija> you’d still want to extract it into its own file?
<FromGitter>
<Sija> aside of using `skip()` macro i mean
<RX14>
you'd extract it into multiple files
<RX14>
one for each architecture
<RX14>
and use `skip unless flag(:bsd)` or the like
<RX14>
but thats unnecesary due to the way lib_c works
<RX14>
lib_c should really just be using macros imho
<RX14>
not the weird load path stuff
_whitelogger has joined #crystal-lang
<FromGitter>
<KCreate> Hey, if I pass a Union to a method, is there a way to make the compiler generate different versions of the method for each type in the union?
<pta2002>
it must be Pointer(LibSDL::PixelFormat), not LibSDL::PixelFormat
<oprypin>
pointerof(z) is the syntax
<Papierkorb>
KCreate, create a untyped method which just does `pointerof(data).as(UInt8*).to_slice(sizeof(typeof(data)))`
<pta2002>
oh
<pta2002>
so what is the difference between pointerof and *?
<oprypin>
* is not valid syntax basically
<Papierkorb>
KCreate, shameless plug: maybe what you need is implemented in the `cannon` shard already
<oprypin>
it's only valid as a shortcut in type grammar
<pta2002>
ah
<pta2002>
got it
<pta2002>
"can't take address of surface.value.format"
<pta2002>
when i do LibSDL.fill_rect(surface, nil, LibSDL.map_rgb(pointerof(surface.value.format), 0xFF, 0xFF, 0xFF))
<oprypin>
yep because .format is copied by value which is a temporary thing so you cant get the pointer of it
<pta2002>
hm
<pta2002>
so how do I pass it in?
<oprypin>
it's hard to know what the right thing because it's sensitive to context. and i really hope i never have to learn sdl
<FromGitter>
<KCreate> @PapierKorb Thanks for that! Cannon looks really look but might be a might overkill since I only need to serialize primitive types
<Papierkorb>
pta2002: store the result of surface.value.format in a temporary variable, and then take a pointerof(that_thing)
<Papierkorb>
pta2002: Don't use _prefixed variables in normal code. The underscore indicates "not used" to the reader
<pta2002>
oh
<pta2002>
fixed that
<FromGitter>
<KCreate> @Papierkorb What do you mean by not used? Is `_` somewhat special in crystal?
<pta2002>
KCrate: from what I understand it's just a convention
<pta2002>
just like you usually put constants ALL CAPS in C, you add an underscore as a prefix to tell it's not used
<Papierkorb>
KCreate, don't remember if it was really special to the compiler. However, saying `def foo(_bar)` means that foo takes an argument, but you don't use it.
<FromGitter>
<Sija> @KCreate such convention exists in Ruby-land too
<Papierkorb>
Exactly the same, even
<oprypin>
i have no idea why it segfaults
<oprypin>
the code makes sense, just some mistake i can't see
<pta2002>
could it be a problem with sdl?
<Papierkorb>
Could be, the faulting address is pretty large
<pta2002>
isn't the address essentially random though
<oprypin>
what do you mean by problem with sdl... it's like 0.0000000001% probability
<pta2002>
yeah it's VERY likely it's me
<Papierkorb>
oprypin: passing broken stuff will break in sdl
<oprypin>
ah, well sure, it does seem like SDL is doing the memory access
<Papierkorb>
pta2002: random? Yes, but only to an extent. The address space is split into regions, and in these regions will be some somewhat (or actually) random address
<oprypin>
but that's not exactly what we (at least I) meant
<pta2002>
"NOTE: You MUST recompile Crystal without the native SDL bindings, otherwise segfaults will occur. Simply remove the SDL files from a fresh clone of the Crystal source code and compiling should be enough."
<pta2002>
There's native SDL bindings on crystal?
<oprypin>
pta2002, i wouldn't look at 3 year old code for crystal
<pta2002>
didn't see that
<pta2002>
but yeah you're right
<Papierkorb>
pta2002: Recheck your structures first, then the methods, then comment out most of your code and iteratively uncomment code again
<pta2002>
wee debugging
<pta2002>
is there a debugger?
<oprypin>
pta2002, i don't think that's a viable approach
<Papierkorb>
gdb
<pta2002>
oprypin: what isn't a viable approach?
<oprypin>
are you sure the logic of your code would hold up even if you ran the equivalent in C?
<oprypin>
using a debugger, but that's just in my closed-minded experience
<pta2002>
ah
<pta2002>
*shrug*
<oprypin>
like maybe you're just not initializing some member
<pta2002>
found the problem
<pta2002>
missing thing on PixelFormat
<Papierkorb>
yay for rechecking struct's
<pta2002>
that was not it
<pta2002>
the names don't have to match the ones on the library right?
<oprypin>
pta2002, don't have to
<pta2002>
alright
<pta2002>
because SDL uses captials on the first letter
<pta2002>
and i changed them to lowercase
<oprypin>
only `fun` names matter
<oprypin>
(of course you can adapt them like you're doing, that's fine)
<pta2002>
cool
<pta2002>
C's int is Int32 right?
<oprypin>
pta2002, if you run out of ideas i really recommend duplicating the example in C and checking if it works
<Papierkorb>
pta2002: Yes, but there's also LibC::Int
<oprypin>
pta2002, it is on all platforms currently supported by crystal. see^
<oprypin>
you do not have it as a pointer in your struct
<FromGitter>
<KCreate> usually `typeof(data.class)` would give me the runtime type but that's not working either (gives me: `(Float64:Class | Int64:Class | String:Class)`)
<oprypin>
gotta pay extreme attention to this
<pta2002>
oprypin: ooo thanks!
<oprypin>
and like i said if rewriting the example directly gave you an error, you should not blindly look for a workaround to make it compile, but instead think why the difference is there
<pta2002>
that also fixes the previous pointer thing
<pta2002>
yeah, i should pay more attention to these things
<Papierkorb>
KCreate, Fixing that isn't fun. Not sure right now, but you'd have to write a macro which checks if the type is an Union, and if yes, builts a `case` statement checking for each type and returning each types size
<oprypin>
pointerof(a) is &a in C syntax, which is not done in the original example
<Papierkorb>
KCreate, I'm guessing, maybe it's not even possible that way
<pta2002>
wooo white screen! :D
<pta2002>
oprypin: i'll take note of these stuff
<pta2002>
thanks for the help :)
<oprypin>
pta2002, btw if you wanna get right to writing games with a good lib, that would be https://github.com/oprypin/crsfml (shameless plug)
<pta2002>
oprypin: i think i've actually seen that
<pta2002>
or at least I saw some crystal sfml bindings
<pta2002>
how does binding to C++ classes work?
<oprypin>
pta2002, it doesn't :|
<Papierkorb>
well technically... hehe
<pta2002>
huh
<pta2002>
how did you get sfml working then?
<FromGitter>
<Sija> @KCreate It seems to me that you’re trying to get a runtime value (return type from `num`) at compile time
<oprypin>
pta2002, by also making C bindings to SFML
<pta2002>
I mean, doesn't sfml heavily use classes?
<FromGitter>
<KCreate> @Suja What i need is a slice with the bytes of a given value
<FromGitter>
<KCreate> And the type of that value is a union
<pta2002>
oprypin: don't we have the same problem again though?
<Papierkorb>
KCreate, what do you need it for?
<Papierkorb>
KCreate, implementing an already existing, binary protocol?
<oprypin>
pta2002, well no, there is no problem anymore, Crystal has C functions to bind to and nothing else
<oprypin>
i have every class passed as void* so uh yeah
<pta2002>
yeah but how does C bind to the classes?
<Papierkorb>
pta2002: C++ code can emit C style functions
<FromGitter>
<KCreate> I'm writing an assembler for my virtual machine, which needs to be able to encode various types
<pta2002>
well, didn't know you could do that this way
<FromGitter>
<KCreate> I guess a simple case statement for all the types would suffice but maybe there's an easier built-in way
<pta2002>
I see how it works oprypin
<Papierkorb>
pta2002: the magic is `extern "C" { .. }`
<pta2002>
you just made a wrapper around it right?
<oprypin>
yes, "just" :p
<pta2002>
what does extern "C" do?
<pta2002>
i've seen it quite a lot but i have no idea what it is
<Papierkorb>
pta2002: Tells the C++ compiler that it must not mangle the names of the functions inside the block, but emit them as written
<pta2002>
ah
<pta2002>
so, for libraries?
<oprypin>
i actually don't have extern C anywhere :o
<Papierkorb>
When interfacing to C
<pta2002>
to make it bindable
<Papierkorb>
If you're interfacing to C++, you can just use C++ normally
<oprypin>
ah no, I do have it everywhere. #define VOIDCSFML_API extern "C" __declspec(dllexport) void
<pta2002>
that makes more sense
<pta2002>
well I'll see if I can get input working
<pta2002>
although I think I achieved my goal of figuring out how bindings work
<oprypin>
well you can never run out of edge cases when porting these. but you got the general idea anyway
<RX14>
oprypin, thanks so much for that PEP
<oprypin>
:> these are a goldmine
<RX14>
so was the go proposal for monotonic Time
<pta2002>
... okay I don't have the patience to bind all the sdl event structs
<pta2002>
I got it mostly working
Raimondii has joined #crystal-lang
Raimondi has quit [Ping timeout: 268 seconds]
Raimondii is now known as Raimondi
zipR4ND has joined #crystal-lang
<FromGitter>
<fridgerator> if a struct has a property that is an array, is it safe to create a new instance of the struct then push values to the array after initialization?
<pta2002>
@fridgerator I think that'd only really be safe if you have a pointer
<FromGitter>
<fridgerator> alright, i'll have to use a class then
<RX14>
uhh
<RX14>
the array is a pointer
<RX14>
whether using an Array in a struct or class, array itself is always a class
<Papierkorb>
fridgerator, that's safe. the array is basically a smart slice as class
<RX14>
and therefore always a pointer
<FromGitter>
<fridgerator> oh rly?
<pta2002>
ah didn't know that
<Papierkorb>
Yup, check `src/array.cr`
<Papierkorb>
Array is probably one of the least magic built in classes in Crystal
<Papierkorb>
Hash is also pretty non-magical
<RX14>
yeah array is a class which holds pointer, size and a few more bits
<RX14>
the most magic built-in is probably string
<RX14>
which uses wizardry
<FromGitter>
<fridgerator> lol
<RX14>
Strings are a normal class but they access the string content by assuming that it's directly after the class in memory
<Papierkorb>
String is indeed really special. Try to find where its instance variables are defined :P
<RX14>
yeah well do the same for array Papierkorb
<Papierkorb>
RX14: Array can be understood without
<RX14>
but it is defined in program.cr
<Papierkorb>
String threw me off at first, Array not so much
<RX14>
sure
<RX14>
actually yeah
<RX14>
Array doesn't have instance vars defined
<RX14>
also why is css so confusing, one minute flex-grow does nothing now it does something