<postmodern>
does crystal have something like StandardError? Something to denote that the programmer made a mistake, instead of a call failing for some system issue
<postmodern>
also is there a macro for defining NotImplementedError methods?
<postmodern>
(methods that are placeholders and just raise NotImplementedError)
<postmodern>
wooohooo i finally read my first v4l2 frame with crystal
<postmodern>
I'm noticing that Slice(UInt8) gets converted to Bytes when i `p` the slice. Are type aliases bi-directional? Is it possible to define a Slice(UInt8) or does it automatically get converted to Bytes? Or is `p` pretty-printing converting it to Bytes?
zorp has joined #crystal-lang
alexherbo2 has joined #crystal-lang
<postmodern>
also how does crystal handle IO.select ? Doing a select() or poll() is how most v4l2 programs determine when data is available.
<postmodern>
ah i guess i can use LibC.select. The C programs appear to just use select() as a kind of wait/timeout for data. Might have to wrap that up into a Channel as well, so I can read simultaneusly from multiple V4L2::Devices.
<postmodern>
hmm how does one use FdSet? do I just write into the static array of FdMasks (really just Long)?
<yxhuvud>
Bytes is defined as alias Bytes = Slice(UInt8)
<yxhuvud>
crystal uses libevent, which is poll-based.
<yxhuvud>
As for the Standarderror question, there was a issue/pr about what to do with those, but I can't find it again.
<yxhuvud>
(and yes, aliases are bidirectional. I'd guess there is some special handling in the printing if it actually says 'Bytes'.)
<yxhuvud>
Yeah, there is. See Slice#pretty_print
<yxhuvud>
or #to_s
sorcus has joined #crystal-lang
<postmodern>
yxhuvud, ah ha
<postmodern>
yxhuvud, i still need some way of testing an fd (Int32) then do an ioctl() to dequeue the v4l2 buffer that points to which user-side buffer holds the data
<postmodern>
yxhuvud, you can also just read() on fd, but i think that's slower/provides less information than the buffer method
<yxhuvud>
I'm very much not certain how to do that in a way that plays well with the event loop. if there isn't an IO subclass that polls that fit your needs you may have a hard time. It might be possible to deduce how to do it by looking at socket.cr, but that is definitely in the deep side of the pool
<yxhuvud>
ah ok, you want to only look to see if you should do something. I don't think that is documented or exposed in a user friendly way, but in any case it boils down to using #wait_readable.
<yxhuvud>
I think it an api that is not intended to be used by the public, but it *should* work. It is defined in evented.cr
<yxhuvud>
I have .. spent a bit more time than I should looking at those pieces of code as I'm (slowly!) hacking together an event loop driven by io_uring
<FromGitter>
<j8r> @Blacksmoke16 ...Use a set?
<FromGitter>
<j8r> Could be more efficient, don't know
<FromGitter>
<j8r> Like `Set.new array.size`, then use `add?`
postmodern has quit [Remote host closed the connection]
postmodern has joined #crystal-lang
<oprypin>
Blacksmoke16, yea definitely a set. if the set's size < array size
<FromGitter>
<j8r> The oprypin's option is shorter. Creating a Set from an array use `#concat`, which use `#each` + `#<<`. If you do it yourself an break early if `add?` is `false`, it will be more efficient. May not matter though, depends how many times and the sizes
<oprypin>
>> a = [1, 2, 2, 3]; s = Set(Int32).new; a.any? { |x| s.add?(x) }
<postmodern>
is there a slick crystal way of automatically converting a NamedTuple into a C Struct value upon assignment? (ex: `@struct.parm.capture.timeperframe = new_time_per_frame` where `new_time_per_frame` is a Fract tuple/named-tuple of (numerator/denominator))
<postmodern>
trying to think of a way to implicitly splat the nominator/denominaotr from the named-tuple/tuple into the struct without having to unpack the tuple each time i try to assign one to a struct field
<yxhuvud>
is it a 1:1 match or do the namedtuple have a bigger interface?
<yxhuvud>
or wait, are you modifying an existing struct in place rahter than creating a new one?
<postmodern>
existing struct in pace, it only has two members nominator and denominator. So will the named-tuple/tuple.
<postmodern>
could also probably do a full struct value assignment
<oprypin>
postmodern, at that point what insane justification do you have to still be using a namedtuple
<oprypin>
thats my attempt to answer the right question - "what is the better alternative to namedtuple here"
<postmodern>
oprypin, note this is for doing a struct member assignment of a Linu::V4L2Fract (nominator : UInt32, denominator : UInt32). Obviously initializing the C struct Linux::V4L2Fract is a bit of work, so i was hoping to use a Tuple or NamedTuple and converting it back to the V4L2Fract or maybe splatting it into an assignment
<oprypin>
postmodern, what is the relevance of namedtuple
<oprypin>
like, what advantage do you think you are getting from using a namedtuple instead of a struct
<postmodern>
oprypin, it's easier to initialize than initializing Linux::V4L2Fract (a C struct) directly
<oprypin>
is it?
<postmodern>
yes
<oprypin>
see my example, in it it's just `fract(1, 2)`
<postmodern>
i already saw it and gave you my feedback
<oprypin>
i dont understand though
<oprypin>
in the example i gave, struct Fract *is* Linux::V4L2Fract
<postmodern>
because i'm working with C bindings, where the fract eventually has to be of type Linux::V4L2Fract
<postmodern>
also if i remember correctly, C structs cannot have custom methods or initializers
<oprypin>
postmodern: well i showed you a secret feature that they can
<postmodern>
oprypin, and i already gave you feedback
<oprypin>
there's really no such thing at the ABI level as a "C struct" or "not C struct", and the actual struct is not a part of the shared library's interface, it's just "pass those two things in a particular layout"
<oprypin>
you just change the bindings definition to use this `Fract` struct and it will just work
<oprypin>
and, other than this knowledge of an entirely separate approach,
<oprypin>
yes, technically it is easier to splat a namedtuple. but for a struct it's also one tiny helper method away
<postmodern>
oprypin, either i am not understanding you, or you are not understanding my use-case. Either way, here is some example code based on your suggestion of using Fract. It does not compile. https://play.crystal-lang.org/#/r/9iou
<oprypin>
postmodern:
<oprypin>
replace V4L2Fract with Fract everywhere
<oprypin>
and delete struct V4L2Fract
<yxhuvud>
oprypin: while I agree that namedtuples are to be avoided when possible, there was nothing in the question that suggested that the nametuple wasn't created by the actual intended usecase, ie some named argument list that was passed along.
<yxhuvud>
personally I feel the current solution is a bit weak in that unions and nested structs can't be defined inline. But perhaps I'm colored by trying to create bindings for a monster struct.
_whitelogger has joined #crystal-lang
<oprypin>
>> lib L; struct S; x : Int32; y : Int32; end; end; L::S.new(**{x: 5, y: 6})
<oprypin>
postmodern: uh yea.. you see what's happening.. i hoped it would just work but apparently the syntax is even more special than i expected
<oprypin>
i think a bugfix for this would be accepted easily. but it might entail a refractor to unify the implementations of these constructors with normal methods arg handling
<postmodern>
workaround, is there a Fractional like type in Crystal I could convert to this Fract struct behind the scenes?
<oprypin>
postmodern: no BigRational is way too heavy of a type
<postmodern>
was wondering if crystal had something like ruby's Rational() (but that didn't reduce the fraction to lcd)
<FromGitter>
<Blacksmoke16> ah nice, thanks @j8r
<FromGitter>
<Blacksmoke16> i guess the challenge will be handling it when the collection could be any indexable, of which i dont know the type
Dreamer3 has joined #crystal-lang
<oprypin>
Blacksmoke16, Set(typeof(indexable[0]))
<FromGitter>
<Blacksmoke16> oh, yea that would prob do it
oddp has joined #crystal-lang
<oprypin>
jesus the Docker CLI is so atrocious
<oprypin>
to find any info i basically end up finding ridig tutorials on the internet, and then im like "ok so i had done this step a bit differently" - how do i adapt then? no idea, nothing just works
<oprypin>
rigid*
<oprypin>
REPOSITORY TAG IMAGE ID CREATED SIZE
<oprypin>
<none> <none> b7d2f09bf9a6 12 hours ago 7GB
<oprypin>
the *only* thing i figured out how to do with it was `docker run -i -t b7d2f09bf9a6`
<oprypin>
ok got it. `sudo docker run -t -d b7d2f09bf9a6` opens the door to actually be able to refer to a running container in other commands
<FromGitter>
<Blacksmoke16> cool thanks again, all specs pass using that implementation
ua has quit [Ping timeout: 240 seconds]
ua has joined #crystal-lang
DTZUZU_ has joined #crystal-lang
DTZUZU has quit [Ping timeout: 240 seconds]
<FromGitter>
<Kusken> Hi everybody! Started to go through Crystal Syntax yesterday I was having a good time. ⏎ I'm now stuck at setting up an appropriate environment. ⏎ ⏎ I use vscode with the Crystal Language extension https://marketplace.visualstudio.com/items?itemName=faustinoaq.crystal-lang. ⏎ The issue I'm having is that at the top on every file I get this error, (red squiggly-lines) ...
<FromGitter>
<Kusken> Tried this both on a debian-vm and in the official docker container published at docker-hub (for crystal lang)
<FromGitter>
<Blacksmoke16> looks like the fix was merged but isnt released yet so prob not on the marketplace?
<FromGitter>
<Kusken> aah
<FromGitter>
<Kusken> okey
<FromGitter>
<Kusken> I see now :D Thanks for the direction
<FromGitter>
<Blacksmoke16> `0.4.0` is on marketplace, and `0.4.1` was released towards end of may and patch was merged in june, so needs another release/push
<FromGitter>
<Kusken> yee seems like it. But I guess I can't be the only one with the issue? The extension is pretty useless for me right now...
<FromGitter>
<Blacksmoke16> :shrug: i dont actually use it so not sure if there is a workaround or not
<FromGitter>
<Kusken> What do you use? Any alternative to vscode, except for vim?
<FromGitter>
<Blacksmoke16> i just use sublime
<FromGitter>
<Blacksmoke16> is a plugin that does highlighting and runs formatter on save, is enough for me
<FromGitter>
<Kusken> It probably is :)
<FromGitter>
<Kusken> I will try to install the extension manually from the github release :D
<FromGitter>
<Blacksmoke16> 👍
<FromGitter>
<Kusken> @Blacksmoke16 Thank you for the directions 👍
<FromGitter>
<Blacksmoke16> np
<FromGitter>
<Blacksmoke16> > *<postmodern>* does crystal have something like StandardError? Something to denote that the programmer made a mistake, instead of a call failing for some system issue ⏎ ⏎ https://crystal-lang.org/api/master/RuntimeError.html there is this
<FromGitter>
<Blacksmoke16> > *<postmodern>* also is there a macro for defining NotImplementedError methods? ⏎ ⏎ no, id just add the method and raise yourself
<postmodern>
Blacksmoke16, RuntimeError (and it's subclasses) are usually meant to desribe something that went wrong with the system (ex: diskspace exhausted) vs. something the programmer intentially did that wasn't allowed
<FromGitter>
<Blacksmoke16> i mean wouldnt the compiler catch those things?
Dreamer3 has quit [Quit: Leaving...]
<FromGitter>
<Blacksmoke16> only thing i can think of would be like dividing by zero or over(under)flows
Human_G33k has joined #crystal-lang
HumanG33k has quit [Ping timeout: 240 seconds]
<yxhuvud>
well, stuff like segfaults etc might also qualify. There was an issue around it, but I don't remember the resolution.
<postmodern>
Blacksmoke16, compiler cannot guard against specific values of enums or combinations of such values that violate some API constraint
<postmodern>
this especially applies to C structs and unions, where depending on what a certain type enum field is, you may or may not access certain fields of the union/struct
yxhuvud has quit [Remote host closed the connection]
<oprypin>
if im making bindings for a project called "foo-bar" and my repository name is "crystal-foo-bar", what should i have as shard.yml `name: ` and the `src/*.cr`?
<oprypin>
`name: foo-bar` and `src/foo-bar.cr` doesnt seem quite right, but neither do underscores
yxhuvud has joined #crystal-lang
<FromGitter>
<Blacksmoke16> style guide says to use `_`
<FromGitter>
<Blacksmoke16> > File paths match the namespace of their contents. Files are named after the class or namespace they define, with underscore-case.
<FromGitter>
<Blacksmoke16> fwiw
<oprypin>
Blacksmoke16, ok thats one part. does it follow that shard name also must be underscores?
<FromGitter>
<Blacksmoke16> for athena i use `-` to denote they're part of the `Athena` namespace, i.e. `athena-config`, but like `athena-dependency_injection`