Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 12:19
    straight-shoota closed #13010
  • 12:19
    straight-shoota closed #13050
  • 12:18
    straight-shoota closed #13048
  • Feb 07 18:12
    HertzDevil labeled #13052
  • Feb 07 18:12
    HertzDevil labeled #13052
  • Feb 07 18:12
    HertzDevil labeled #13052
  • Feb 07 18:12
    HertzDevil opened #13052
  • Feb 07 17:49
    straight-shoota closed #13051
  • Feb 07 17:46
    beta-ziliani milestoned #13051
  • Feb 07 17:46
    beta-ziliani milestoned #13051
  • Feb 07 14:03
    beta-ziliani review_requested #13050
  • Feb 07 13:59
    straight-shoota labeled #13051
  • Feb 07 13:59
    straight-shoota assigned #13051
  • Feb 07 13:59
    straight-shoota opened #13051
  • Feb 07 13:50
    straight-shoota milestoned #13050
  • Feb 07 13:50
    straight-shoota milestoned #13050
  • Feb 07 13:47
    Blacksmoke16 labeled #13050
  • Feb 07 13:47
    Blacksmoke16 labeled #13050
  • Feb 07 13:47
    Blacksmoke16 labeled #13050
  • Feb 07 13:47
    Blacksmoke16 labeled #13050
George Dietrich
@Blacksmoke16
iirc they're also slightly different in regards to typing
like if you include MyModule into Foo, Foo.new.is_a? MyModule # => true, but if you extend the module it affects the type of Foo.class not the actual instance type
mfiano
@mjfiano:matrix.org
[m]
and this behaves like composition over inheritance, which is what I suppose they docs mean by partial types, right?
Like mixins in some other languages/paradigms
George Dietrich
@Blacksmoke16
yes exactly
mfiano
@mjfiano:matrix.org
[m]
Great thanks
George Dietrich
@Blacksmoke16
they're also great as a means of having interfaces
as you can define abstract methods on the module that the including type would be required to implement them
Ashish Bista
@ashishbista

you can run a spec file like you would a normal file

I didn't know this. It would solve my issue. Thank you!

George Dietrich
@Blacksmoke16
crystal spec spec/path/to/file
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I've been trying to think how to structure my module hierarchy correctly for a couple hours. I'm not used to this way of programming apparently.
George Dietrich
@Blacksmoke16
oh?
mfiano
@mjfiano:matrix.org
[m]
I have a few different structs, each in their own file under src/myproject/, and in a MyProject module. In src/myproject.cr I basically just do require "./src/myproject/*. Now I would like to add a "common.cr" file with common functionality between all the structs, implementing mostly class methods, and also available to users of the library. I can't seem to figure out how to get that to work.
George Dietrich
@Blacksmoke16
is the logic common to all the structs or only some of them?
mfiano
@mjfiano:matrix.org
[m]
Well, one such common method would be that nearly_equal?(x, y, rel_tol, abs_tol) method I wrote yesterday. This would be used in the implementation of several of each of the structs' instance methods.
George Dietrich
@Blacksmoke16
put it in a parent abstract struct?
mfiano
@mjfiano:matrix.org
[m]
I could look into that. I also want some of these methods to be public facing.
If that will work
George Dietrich
@Blacksmoke16
a public method declared in a parent type is available in all its children
a module/mixin is mainly ideal for like extra code that could be useful for any type. E.g. like the stdlib's Comparable, or Enumerable modules
but if something is common to multiple things, just define that once and use inheritance
mfiano
@mjfiano:matrix.org
[m]
Right, I just have to look up abstract types in Crystal again. I'm used to all of their methods also being abstract.
mfiano
@mjfiano:matrix.org
[m]
Thanks. I'll re-read that again. Your suggestion above also really clicked for me.
George Dietrich
@Blacksmoke16
and im assuming you actually want structs. I.e. are they immutable?
one gotcha is you cant inherit a non abstract struct, which might require some redesign if you wanted to have a base struct that was also usable on its own
mfiano
@mjfiano:matrix.org
[m]
That is something I'm still deciding on. I'm not sure if I want to allocate new stack memory or just use heap-allocated objects/mutability yet.
I will have to see how it is to be used and benchmark some I suppose. For now, they are structs though. I expect the change to class to be easy later if need be.
George Dietrich
@Blacksmoke16
:thumbsup:
mfiano
@mjfiano:matrix.org
[m]
I'm having a ton of fun learning, albeit slow, thanks for your help :)
George Dietrich
@Blacksmoke16
np
George Dietrich
@Blacksmoke16

is there a better way to create read addition bytes into a larger Bytes slice other than doing like:

    bytes = Bytes.new 4
    io.pos -= 3
    io.read_fully bytes

where the original bytes slice was 3

mfiano
@mjfiano:matrix.org
[m]
Do the property/getter/setter macros inline the generated method defs?
George Dietrich
@Blacksmoke16
im not sure, would be up to LLVM i'd think
could you even inline that? like since its internal state
mjfiano
@mjfiano:matrix.org
[m]
I guess it depends how they are implemented. In Lisp, struct members are regular functions that access the internal state, and are inlined by default, but can be told not to be.
I was just wondering if that was the case here, and if the AlwaysInline annotation was implied, or a way to opt in/out
George Dietrich
@Blacksmoke16
@HertzDevil might know
manveru
@manveru:matrix.org
[m]
undefined macro method 'Path#instance_vars' confused me way longer than i wanna admit... it refers to Crystal::Macros::Path and not Path :P
George Dietrich
@Blacksmoke16
yea...all that stuff is macro level types
manveru
@manveru:matrix.org
[m]
i was using Path around that area, so got quite confused :)
George Dietrich
@Blacksmoke16
that would do it
manveru
@manveru:matrix.org
[m]
i'm still fighting with macro evaluation order i think
George Dietrich
@Blacksmoke16
oh/
?
manveru
@manveru:matrix.org
[m]
basically... i want some kind of inheritance i guess
      macro derive_from(owner, *names)
        \{% for name in names %}
          \{% var = owner.resolve.instance_vars.find{ |v| v == name } %}
          property \{{name}} : \{{var.type}}
        \{% end %}

        def initialize( \{{ names.map{ |n| "@#{n}".id }.join("").id }} )
        end
        \{% debug %}
      end
the problem is that at this point, instance_vars is empty once more