Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 13:27
    Blacksmoke16 labeled #12065
  • 13:27
    Blacksmoke16 labeled #12065
  • 09:18
    I3oris opened #12065
  • May 21 17:59
    HertzDevil labeled #12064
  • May 21 17:59
    HertzDevil labeled #12064
  • May 21 17:59
    HertzDevil labeled #12064
  • May 21 17:59
    HertzDevil opened #12064
  • May 20 20:01
    beta-ziliani milestoned #12033
  • May 20 19:13
    beta-ziliani milestoned #12040
  • May 20 18:57
    beta-ziliani milestoned #11984
  • May 20 18:50
    beta-ziliani review_requested #11972
  • May 18 13:16
    hugopl closed #12062
  • May 17 20:20
    Blacksmoke16 synchronize #12044
  • May 17 18:19
    straight-shoota edited #11947
  • May 17 18:19
    straight-shoota milestoned #11947
  • May 17 18:18
    straight-shoota closed #11971
  • May 17 18:18
    straight-shoota closed #11970
  • May 17 17:21
    HertzDevil labeled #12063
  • May 17 17:21
    HertzDevil labeled #12063
  • May 17 17:21
    HertzDevil labeled #12063
Quinton Miller
@HertzDevil
i don't think there is ever a use for those formal types for runtime reflection
From IRC (bridge bot)
@FromIRC
<frojnd> Can someone explains to me why kemal when param is empty adds favicon.ico to the param? https://carc.in/#/r/bs23
y8
@y8
Most likely it's your browser trying to fetch favicon for 127.0.0.1:3000. Try curl http://127.0.0.1:3000/
From IRC (bridge bot)
@FromIRC
<frojnd> Nah, I see "Kemal doesn't know this way" message it prints html
<frojnd> From crystal console I see just: 2021-08-18 11:07:57 UTC 404 GET / 66.87µs
<frojnd> But if I enter with param like this: curl http://0.0.0.0:3000/bla I see date: bla" and 2021-08-18 11:08:58 UTC 200 GET /bla 56.54µs
y8
@y8
To handle GET /, you need separate route get "/"
George Dietrich
@Blacksmoke16
stellarpower
@stellarpower
Are there any specific docs on how crystal manages memory? Am wondering if custom allocators would be possible.
Ary Borenszweig
@asterite
Memory is allocated by the GC, we use boehm. You can do LibC.malloc and LibC.free if you want, but there's no support for custom allocators (Crystal is not a low-level language)
From IRC (bridge bot)
@FromIRC
<yxhuvud> I mean, it is also definitely possible to monkeypatch allocate, but it is not a well threaden path and deallocation would be a problem as it would have to be manual
<yxhuvud> monkeypatching + being self hosted allows so many abilities to do horrible things :⁠D
Ary Borenszweig
@asterite
It's true! Also, I think you could redefine allocate on particular classes, and I think that would allow a custom allocator... except that this language isn't like Rust or C++ where memory is automatically deallocated when things go out of scope, so the custom memory allocator will have to be a GC
From IRC (bridge bot)
@FromIRC
<yxhuvud> Yes, I've played around with custom allocate on classes before. It works but it is a bit of work to get something that is useful. I could see building a slab allocator as well, where the whole slab gets deallocated at once, but that would probably be easiest by having an external allocation method rather than using the default machinery with new and initialize.
Javi Fontan
@jfontan
I've been implementing a toy parallel find like command in crystal and I'm really happy with the language. Using 4 threads (8 workers) it runs slightly faster than another naive implementation I made in rust.
Do you know if it's possible to change the number of workers from code? That is, without setting the env var
From IRC (bridge bot)
@FromIRC
<yxhuvud> It is not, currently.
Javi Fontan
@jfontan
ok, thanks
From IRC (bridge bot)
@FromIRC
<yxhuvud> though possible is a very broad word, considering the language allows monkeypatching of basically everything. It is possible, but very intrusive and not recommended to redefine Crystal::Scheduler#worker_count to return something else than 4 by default.
<yxhuvud> personally I'd use https://github.com/yxhuvud/nested_scheduler instead, but I cannot really promise any api stability (or other kind of stability for that matter), at this point.
Javi Fontan
@jfontan
ah, interesting
From IRC (bridge bot)
@FromIRC
<yxhuvud> or for that matter, that the shard continue to work when the next version comes out. it monkeypatches all over the internals :⁠)
Javi Fontan
@jfontan
it works perfect, just a little bit slower than with normal threads
standard:
3.14user 2.03system 0:01.23elapsed 418%CPU (0avgtext+0avgdata 42320maxresident)k
1986inputs+0outputs (0major+10215minor)pagefaults 0swaps

nested_scheduler:
3.26user 2.06system 0:01.28elapsed 415%CPU (0avgtext+0avgdata 40480maxresident)k
1790inputs+0outputs (0major+9672minor)pagefaults 0swaps
could be the load of my machine anyway. Is a small test
From IRC (bridge bot)
@FromIRC
<yxhuvud> could also be the overhead of the additional threads. Just be aware that it is quite likely that every version of crystal that gets released for the foreseeable future will break the shard until I fix it again :⁠)
Javi Fontan
@jfontan
right, lowering the number of threads to 5 (the same number of fibers) improves the time
ewalden
@ewalden:matrix.org
[m]

Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.

For example, the original Ruby it looks like this:

def initialize(opts = {})
        @x = opts[:has_x] ? true : false
    @y = opts[:y]
end

The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.

I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.

I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.

I am now considering using a structs with default values, like so:

struct UserOpts 
    property x
    property y
    def initialize(@x = true , @y = "default")
    end
end

This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)

Is there a better way to handle this?

I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.

George Dietrich
@Blacksmoke16
using **kwargs would allow you to do like Obj.new has_x: true, y: 10
are these opts used in multiple types or just one?
i guess i never got the benefit of using a hash/arbitrary args for this. Like the first example only uses 2, so why not just have def initialize(@has_x : Bool, @y : Int32)
Kaia Leahy
@tenebrousedge
having multiple overloads probably is a way to avoid that sort of thing, too
ewalden
@ewalden:matrix.org
[m]

My issue with **kwargs was that it uses a NameTuple, which require the types to the same. It's actually quite a bit of data of different types.

I considered def initialize(@has_x : Bool, @y : Int32) approach, but that would change the method signature by a lot, add a lot of paramters, and require a lot more rewriting. But now that I think it over, this may be simplest in the long term.

Kaia Leahy
@tenebrousedge
structs could be a thing, too
George Dietrich
@Blacksmoke16
yes, just bite the bullet and make your initializers how they should be, you may have to un-learn some stuff from Ruby :wink:
Kaia Leahy
@tenebrousedge
going from Crystal to Ruby, I really miss being able to assign instance vars in the signature
ewalden
@ewalden:matrix.org
[m]
Yeah, I think that's the lesson here. Time to let go the unlearn the Ruby way. So my take away: for lots of params: Using structs and/or listing out all the params are reasonable approaches. Thanks ya'll.
George Dietrich
@Blacksmoke16
if a type has a lot of params, is it possible to break it up?
ewalden
@ewalden:matrix.org
[m]
Which type? You mean method?
George Dietrich
@Blacksmoke16
like instead of having 1 type with a bunch of instance variables, could you break it up into other types, kinda like what you were doing with the struct just as a way to better group related ivars
granted i know 0 of your context so maybe that doesn't make sense :shrug:
1 reply
Quinton Miller
@HertzDevil
your ruby code can also be
def initialize(has_x:, y:)
  @x = has_x ? true : false
  @y = y
end
ewalden
@ewalden:matrix.org
[m]

:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.

For example, the original Ruby it looks like this:

def initialize(opts = {})
        @x = opts[:has_x] ? true : false
    @y = opts[:has_y]
end

The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.

I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.

I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.

I am now considering using a structs with default values, like so:

struct UserOpts 
    property x
    property y
    def initialize(@x = true , @y = "default")
    end
end

This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)

Is there a better way to handle this?

I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.

:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.

For example, the original Ruby it looks like this:

def initialize(opts = {})
        @x = opts[:has_x] ? true : false
    @y = opts[:y]
end

The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.

I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.

I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.

I am now considering using a structs with default values, like so:

struct UserOpts 
    property x
    property y
    def initialize(@x = true , @y = "default")
    end
end

This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)

Is there a better way to handle this?

I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.

:point_up: Edit: Hello everyone, I am fairly experienced with Ruby and just getting started with Crystal. I love the experience! But I have a question about the approach. I am porting an existing Ruby app to Crystal, and in the original app, we use hashes to pass an arbitrary number of params.

For example, the original Ruby it looks like this:

def initialize(opts = {})
        @x = opts[:has_x] ? true : false
    @y = opts[:y]
end

The problem is that there are lots of options. So I am trying to figure out the most "Crystal-eque" way to do something like this.

I've considered using splats/NameTuples, but the problem is that each option has a different type, so that doesn't work.

I've considered a Hash, but the problem is that hash is complicated, and I would have to pass many options every time I call the method.

I am now considering using a structs with default values, like so:

struct UserOpts 
    property x
    property y
    def initialize(@has_x = true , @y = "default")
    end
end

This way, I can pass the method a struct with default values, like so: initialize(opts : UserOpts)

Is there a better way to handle this?

I suspect this is a common issue from people from Ruby, so I wanted to see if there is a recommended best practice for this situation in Crystal.

George Dietrich
@Blacksmoke16
oof, i like how editing it re-pastes the whole message? :thinking:
From IRC (bridge bot)
@FromIRC
<riza> probably because matrix/gitter/irc/one-bridge-to-rule-them-all
ewalden
@ewalden:matrix.org
[m]
Are you referring to my edits? Did they trigger re-posts? 😬
From IRC (bridge bot)
@FromIRC
<riza> yeah :⁠[