jhass changed the topic of #crystal-lang to: The Crystal programming language | https://crystal-lang.org | Crystal 0.35.1 | Fund Crystal's development: https://crystal-lang.org/sponsors | GH: https://github.com/crystal-lang/crystal | Docs: https://crystal-lang.org/docs | Gitter: https://gitter.im/crystal-lang/crystal
duane has quit [Ping timeout: 240 seconds]
f1refly has quit [Ping timeout: 265 seconds]
Liothen has joined #crystal-lang
Liothen has quit [Read error: Connection reset by peer]
Liothen has joined #crystal-lang
Liothen has quit [Ping timeout: 244 seconds]
Liothen has joined #crystal-lang
Liothen has quit [Read error: Connection reset by peer]
Liothen has joined #crystal-lang
Liothen has quit [Read error: Connection reset by peer]
Liothen has joined #crystal-lang
avane has quit [Quit: ZNC - https://znc.in]
avane has joined #crystal-lang
<FromGitter> <mattrberry> I'm just gonna throw this question out here but I'm going offline now so sorry if I don't respond to replies haha. Just figured I'd get the question out before I go off. I have my bitfield macro here: https://github.com/mattrberry/bitfield ⏎ It lets you define bitfield-like objects like: ⏎ ⏎ ```abstract class Base8 < BitField(UInt8) ⏎ def foo ⏎ puts "bar" ⏎ end ⏎ end``` ⏎ ...
<FromGitter> <mattrberry> And again sorry if I don't respond tonight, but gotta run soon :)
<yxhuvud> j8r: no. io_uring requires way too new kernels to be possible to merge. This will be a standalone shard.
yukai has quit [Ping timeout: 256 seconds]
<FromGitter> <phykos> a good part of LLVM-based languages can be compiled to webassembly. ⏎ can Crystal do that?
zorp has joined #crystal-lang
<yxhuvud> no.
<jhass> Doing ABI compat for that would probably be relatively straight forward, but stdlib compat would be a very very big task
<yxhuvud> There is that, but also getting GC to work could be a nightmare
<jhass> well yeah. It kinda is part of stdlib I guess though. In theory you could build an stdlib that requires explicit memory management
<jhass> I guess our line between stdlib and runtime is blurry
<jhass> I kinda included runtime into stdlib for my statement above, so that's GC + MT/evented IO
<jhass> /coroutine support
<yxhuvud> right. I think it is mostly all the runtime stuff that is problematic, rather than stdlib itself.
<yxhuvud> except for oddities like regexp lib and other dependencies that particular classes need.
<yxhuvud> but you can do without those. You really can't do without gc or some sort of io.
<FromGitter> <j8r> yxhuvud it can be a macro, like done to support very new LLVM versions
<yxhuvud> we don't have any good way to detect kernel versions in macros, AFAIK. And the point of the exercise at this point is to get it working and get something that shows if further interest is warranted or not. Not to produce a pull request.
<FromGitter> <j8r> ha ok
<FromGitter> <j8r> I thought you were working just to patch libevent, no Crystal involved
<FromGitter> <j8r> in fact, working on https://github.com/libevent/libevent/issues/1019 per se
<yxhuvud> nah it is pure crystal. Based on https://github.com/yxhuvud/ior
<yxhuvud> (docs are not really up to date, a lot more ops are actually supported.)
avane has quit [Ping timeout: 256 seconds]
avane has joined #crystal-lang
_whitelogger has joined #crystal-lang
duane has joined #crystal-lang
<FromGitter> <halfdan> Howdy! Building a Crystal wrapper for a C-library. The library has a function that allows passing a void* as context for a callback. No matter what I try I always end up with ⏎ ⏎ ```code paste, see link``` ⏎ ⏎ I tried Object instead of pointer too. The whole point of this argument is to pass an arbitrary value to a callback. What's the crystal way of achieving this?
<FromGitter> <Blacksmoke16> try `Pointer(Void).null`
<FromGitter> <Blacksmoke16> oh sorry, sec
<FromGitter> <Blacksmoke16> as the error says i think you need to use a more specific type like `MyContext*` or something
<FromGitter> <Blacksmoke16> (which is essentially Pointer(MyContext)), at least thats my understanding of it
<frojnd> Anyone uses any google console apis? I'm trying to use translate api: https://cloud.google.com/translate/docs/reference/api-overview?hl=en_US But there is no crystal client yet. So I can use REST service but I must implement google oauth. Does crystal already have such gem?
<FromGitter> <Blacksmoke16> theres an `OAuth2` module in stdlib yea
<FromGitter> <naqvis> @halfdan use `Void*` instead
<FromGitter> <naqvis> ```alias CallbackFunc = (UInt32, UInt64, UInt64, UInt32, Void* -> Int32)```
<frojnd> I've also found this: https://github.com/PlaceOS/google maybe exactly what I need
duane has quit [Ping timeout: 258 seconds]
<FromGitter> <halfdan> @naqvis Seems to work (same @Blacksmoke16 ). How do I then cast an arbitrary pointer to a Void*? I.e. ⏎ ⏎ ```var = 12 ⏎ pointerof(var) # Pointer(Int32) but should be Void*``` [https://gitter.im/crystal-lang/crystal?at=5fa94b2db4283c208a547ed5]
<FromGitter> <Blacksmoke16> afaik doesnt using `Void*` just mean that value is ignored?
<FromGitter> <Blacksmoke16> (granted I don't really know C but my understanding was you use Void* when you aren't using that value or something)
<FromGitter> <halfdan> @Blacksmoke16 negative - it means you're referencing memory still, but the exact structure of it is unknown
<FromGitter> <halfdan> you can later cast it to a struct/union/whatever and access memory this way
<FromGitter> <halfdan> also to answer my own question: `(Void*).as(Int32*)`
<FromGitter> <Blacksmoke16> 👍 ah gotcha
<FromGitter> <naqvis> @halfdan `Box.box(var)`
<FromGitter> <halfdan> Now how can I make the Void* an optional param? Can't use `pointerof(nil)` nor `= nil` as defaults
<FromGitter> <halfdan> Oooh interesting
<FromGitter> <halfdan> @naqvis Well... Error: can't use Box(T) as proc argument yet, use a more specific type
<FromGitter> <naqvis> can you share more detailed code?
<FromGitter> <halfdan> yeah sure
<FromGitter> <halfdan> one sec
<FromGitter> <naqvis> don't use `Box` as type please
<FromGitter> <naqvis> use `Void*`
<FromGitter> <halfdan> Ah.
<FromGitter> <naqvis> as isn't that your FFI is expecting a void pointer?
<FromGitter> <halfdan> that part already works - I'm passing a boxed callback + context to the FFI scan func
<FromGitter> <naqvis> also passing a closure to FFI as callback is tricky thing
<FromGitter> <halfdan> yup but that part works
<FromGitter> <halfdan> @naqvis Can I make Void* an optional argument?
<FromGitter> <halfdan> Or is that syntactically impossible
<FromGitter> <Blacksmoke16> could use a null pointer as a default?
<FromGitter> <halfdan> how do I get one? `pointerof(nil)` isn't allowed
<FromGitter> <Blacksmoke16> try just passing in `nil`
<FromGitter> <halfdan> ```code paste, see link``` [https://gitter.im/crystal-lang/crystal?at=5fa951828a236947ba9518ea]
<FromGitter> <Blacksmoke16> oh wait, the whole callback is optional
<FromGitter> <halfdan> no, the context is
<FromGitter> <Blacksmoke16> thats what i thought
<FromGitter> <Blacksmoke16> eh yea idk, ill defer to @naqvis :p
duane has joined #crystal-lang
<FromGitter> <naqvis> sorry was away
<FromGitter> <naqvis> > @naqvis Can I make Void* an optional argument? ⏎ ⏎ yes you can. Use `Void*?`
<FromGitter> <naqvis> and then pass `nil` as an argument
<FromGitter> <halfdan> perfect - all works now
<FromGitter> <halfdan> I somehow thought the ? needed to be on the var name
<FromGitter> <halfdan> on the type makes a whole lot more sense
<FromGitter> <Blacksmoke16> its a shortcut to `Void* | Nil`
<FromGitter> <naqvis> 👍
<FromGitter> <Blacksmoke16> and `Void*` is a shortcut to `Pointer(Void)`
gangstacat has quit [Ping timeout: 272 seconds]
zorp has quit [Ping timeout: 260 seconds]
<FromGitter> <HertzDevil> be careful as using `Void*?` you cannot distinguish between null pointers and nil
<FromGitter> <HertzDevil> `true ? Pointer(Void).null : nil # => nil`
<FromGitter> <HertzDevil> there is some debate around this issue
f1refly has joined #crystal-lang
<FromGitter> <HertzDevil> non-throwing `#to_i?` and friends are in `String` but not the integer types themselves :rage1:
<FromGitter> <j8r> @HertzDevil yep: https://github.com/crystal-lang/crystal/pull/9389
<FromGitter> <j8r> we have to use `#new rescue` for now :/
<FromGitter> <j8r> `Int32.new("a") rescue nil`
<FromGitter> <HertzDevil> that's not exactly it
<FromGitter> <HertzDevil> i'm thinking more about `#to_i?` between integer types
<FromGitter> <j8r> you can, Int32#to_i8 by example?
<FromGitter> <j8r> ha ok
<FromGitter> <j8r> yes, there is no `Int32#to_i8?`, Int8#to_i32?`, `and so on
<FromGitter> <j8r> Buuut
<FromGitter> <j8r> with my PR, you could do in a functional way: `Int32.new? 10_u8`
<FromGitter> <j8r> ...nvm, it will be possible but will fail too 😅
<FromGitter> <j8r> all this to_x are a mess...
<FromGitter> <HertzDevil> yeah that pr just calls those missing methods in the integer types
f1reflyylmao has joined #crystal-lang
f1refly has quit [Ping timeout: 240 seconds]
<FromGitter> <HertzDevil> it's probably doable, since comparison works across integer types and each defines its own `MIN` and `MAX`
<FromGitter> <j8r> All this new and to_x...
<FromGitter> <j8r> that's redundant IMHO
<FromGitter> <j8r> I'd gladly have something like `to(Int32)`, something like that
<FromGitter> <HertzDevil> in that case `Int32#to?(type : Int8.class)` and so on would still be missing
<FromGitter> <j8r> hum yes
<FromGitter> <j8r> we are touching to primitives (https://github.com/crystal-lang/crystal/blob/master/src/primitives.cr)
<FromGitter> <j8r> I"m not familiar with them, not sure the code can be reduced
<FromGitter> <j8r> it would be there to add the `to_x?`
<FromGitter> <HertzDevil> the case with integers is simple: https://play.crystal-lang.org/#/r/9xpy
<FromGitter> <HertzDevil> no primitives needed though they may speed things up
<FromGitter> <HertzDevil> also not sure what overflow means for float conversion
<FromGitter> <HertzDevil> (btw yes crystal supports chained comparisons)
<FromGitter> <j8r> there is this way, yeah... Maybe like you said there is a more efficient way
<FromGitter> <j8r> overflow check is even a primitive, otherwise it would have been done in a similar fashion as you do
<FromGitter> <j8r> anyway, worth opening an issue!
<jhass> I guess the efficient way would be to have https://github.com/crystal-lang/crystal/blob/master/src/compiler/crystal/codegen/primitives.cr#L194 but returning nil there
<FromGitter> <HertzDevil> it's on lines 611 and 619, the anchored part seems to be for + and -
<FromGitter> <j8r> +1
<FromGitter> <j8r> however there are other places where this method is used :/
<jhass> but then, next up we do +?, -?, *? etc?
<FromGitter> <j8r> why?
<jhass> maybe this is not common enough to warrant a primitive over just having to do n.to_x unless n > MAX
<FromGitter> <j8r> :shrug: not sure if it will add much complexity, or not.
<FromGitter> <HertzDevil> my main concern is `Int#to_x?` brings `Int`'s interface to parity with `String`
<FromGitter> <HertzDevil> whether we could leverage the primitives is a secondary concern
<FromGitter> <HertzDevil> there's the even slower `to_x rescue nil`
<FromGitter> <HertzDevil> so i don't think this naturally leads to nilable binops
<FromGitter> <HertzDevil> ~~we should be glad nobody has ever suggested saturated math support in crystal~~
<FromGitter> <j8r> I only found 2 occurrences in the compiler/stdlib
<FromGitter> <j8r> @HertzDevil agree
<FromGitter> <j8r> ...
<FromGitter> <j8r> so, calling `BigInt#to_u8` mean `BigInt#to_u64#to_u32#to_u8` 0.o
<FromGitter> <j8r> I don't understand
<FromGitter> <HertzDevil> one alternative is to make `#to_x?` the primitive methods, and then do `to_x? || raise OverflowError.new` in `#to_x` instead
<FromGitter> <HertzDevil> might be bad for codegen
<FromGitter> <j8r> good idea! Not sure
<FromGitter> <j8r> it would be great if you open an issue with all of this
<FromGitter> <HertzDevil> will do if nobody does before i wake up tomorrow
gangstacat has joined #crystal-lang
DTZUZU has joined #crystal-lang
cloaked1 has joined #crystal-lang
cloaked1 has quit [Quit: http://quassel-irc.org - Chat comfortably. Anywhere.]
sagax has joined #crystal-lang
yukai has joined #crystal-lang
_whitelogger has joined #crystal-lang