Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 11:47
    HertzDevil synchronize #11496
  • 11:47
    HertzDevil synchronize #11495
  • 10:48
    HertzDevil labeled #11502
  • 10:48
    HertzDevil labeled #11502
  • 10:48
    HertzDevil labeled #11502
  • 10:48
    HertzDevil opened #11502
  • 06:50
    marekkwiatkowski edited #11501
  • 06:48
    marekkwiatkowski opened #11501
  • 06:48
    marekkwiatkowski labeled #11501
  • Nov 26 22:55
    straight-shoota closed #11493
  • Nov 26 22:32
    straight-shoota synchronize #11500
  • Nov 26 22:20
    straight-shoota synchronize #11500
  • Nov 26 21:59
    straight-shoota labeled #11500
  • Nov 26 21:59
    straight-shoota opened #11500
  • Nov 26 21:51
    straight-shoota synchronize #11475
  • Nov 26 21:51
    straight-shoota synchronize #11475
  • Nov 26 21:42
    oprypin review_requested #11395
  • Nov 26 20:59
    oprypin review_requested #11485
  • Nov 26 20:57
    beta-ziliani synchronize #11485
  • Nov 26 19:38
    hugopl synchronize #11395
From IRC (bridge bot)
@FromIRC
<jhass[m]> I never ever needed that feature in Ruby either and always found any usages of it I found strange and hard to follow
<mfiano> How can I define a generic struct with all of its fields being of type T (homogeneous), where T is restricted to Float32 or Float64?
From IRC (bridge bot)
@FromIRC
<jhass[m]> We don't support restricting type variables yet. A hack is to check the type in macro land using https://carc.in/#/r/bk8d
<mfiano> :⁠( Thanks
From IRC (bridge bot)
@FromIRC
<xyhuvud> shouldn't it be possible by setting type restrictions on the constructors though?
George Dietrich
@Blacksmoke16
you could, but then you end up with a union of the two
manveru
@manveru:matrix.org
[m]
is there some trick to using @type.instance_vars in a module to get the variables of a class that it was included into?
i have this code, but both annoations and instance vars are empty:
module Test
  annotation Flag
  end

  module Configuration
    macro included
      {% puts @type %}
      {% puts @type.annotations(Flag) %}
      {% puts @type.instance_vars %}
    end
  end

  struct Config
    include Configuration

    @[Flag(name: "a", short: "a", type: String?)]
    @a : String?
  end
end
George Dietrich
@Blacksmoke16
it has to be called in the context of a method
manveru
@manveru:matrix.org
[m]
ah, i guess the problem is with the macro included...
  module Configuration
    def initialize
      {% puts @type %}
      {% puts @type.annotations(Flag) %}
      {% puts @type.instance_vars %}
    end
  end
this now gives me instance vars, but not annotations
George Dietrich
@Blacksmoke16
the problem is @type.instance_vars has to be used in the context of a method, which it is now
and annotation is empty because the macro is being expanded within the module i'd guess
manveru
@manveru:matrix.org
[m]
ah, the annotation is on the instance var
thanks :)
From IRC (bridge bot)
@FromIRC
<mfiano> I'm trying to affect how an instance of a struct prints with puts, but defining a custom to_s in the struct scope doesn't seem to do anything. What is the correct way?
George Dietrich
@Blacksmoke16
def to_s(io : IO) : Nil
then write the content to that io
From IRC (bridge bot)
@FromIRC
<mfiano> Perfect, thanks.
George Dietrich
@Blacksmoke16
there's also def inspect(io : IO) : Nil which is similar but diff
From IRC (bridge bot)
@FromIRC
* mfiano reads
<mfiano> I see.
<mfiano> Crystal is just amazing me the more I learn it. I'm pretty excited to be getting started. It's so nice to be able to just write what I mean with minimal syntax/short symbols like "to_s".
George Dietrich
@Blacksmoke16
fun fact, to_s also gets invoked if you add a var into a string. I.e. puts "Hello #{user}" is the same as puts "Hello #{user.to_s}"
From IRC (bridge bot)
@FromIRC
<mfiano> I remember reading the performance gotchas about that
From IRC (bridge bot)
@FromIRC
<mfiano> I'm struggling with multiple struct constructors. I have a struct, Foo, with @a and @b variables. I'd like to invoke it as Foo.new(1.2, 3.4), or Foo.new(1.2), in which case, @a and @b would be set to 1.2.
George Dietrich
@Blacksmoke16
is the 2nd value just the first one doupeld?
doubled
From IRC (bridge bot)
@FromIRC
<mfiano> Given one argument, both variables should be set to the same value.
George Dietrich
@Blacksmoke16
def self.new(value : Float64) : self
  new value, value
end

def initialize(@a : Float64, @b : Float64); end
should do it
From IRC (bridge bot)
@FromIRC
<mfiano> Ah so you can only have 1 initialize method?
<mfiano> I was trying to overload that multiple times :⁠/
George Dietrich
@Blacksmoke16
no, but
tl;dr i always just mke initialize represent the actual state of the instance, but then have overloaded .new to transform the arguments into something the initializer constructs
expects*
From IRC (bridge bot)
@FromIRC
<mfiano> That post was very well explained, thanks.
George Dietrich
@Blacksmoke16
From IRC (bridge bot)
@FromIRC
<mfiano> Is there a way to alias a module at the file level?
<mfiano> I don't want to remove the full qualification, but for a long/deep module that is used all over a single file, I would like to alias it to a single character or something
George Dietrich
@Blacksmoke16
technically no, but you shouldn't need to provide the FQN if its in the same namespace

or if its in a diff namespace you can do like

```

private alias Foo = MyApp::Some::Very::Long::Namespace::Foo
private top level aliases are scoped to the file they're defined in
From IRC (bridge bot)
@FromIRC
<mfiano> Oh! I wasn't aware that program-level private stuff was file-local