Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 21:11
    straight-shoota closed #12355
  • 18:26
    straight-shoota closed #12351
  • 18:26
    straight-shoota closed #12374
  • 17:19
    Blacksmoke16 labeled #12377
  • 17:19
    Blacksmoke16 labeled #12377
  • 17:15
    dscottboggs opened #12377
  • 16:26
    Blacksmoke16 closed #8093
  • 16:26
    Blacksmoke16 labeled #8093
  • 15:48
    Blacksmoke16 labeled #12376
  • 15:48
    Blacksmoke16 labeled #12376
  • 15:20
    straight-shoota milestoned #12374
  • 15:20
    wontruefree opened #12376
  • 15:20
    wontruefree labeled #12376
  • 14:53
    asterite labeled #12375
  • 14:53
    asterite labeled #12375
  • 14:53
    asterite opened #12375
  • 13:04
    asterite synchronize #12374
  • 12:47
    straight-shoota milestoned #12355
  • 12:20
    asterite labeled #12374
  • 12:20
    asterite labeled #12374
didactic-drunk
@didactic-drunk
How do you get a String Class::Name.method_name from a macro for the current method?
George Dietrich
@Blacksmoke16
{{"#{@type.name.id}.#{@def.name.id}".id}} would prob do it
didactic-drunk
@didactic-drunk
@what for the caller?
George Dietrich
@Blacksmoke16
is Class::Name the same as the class the method is in?
didactic-drunk
@didactic-drunk
First example didn't quiet work
class Foo
  macro foo
    p "{{"#{@type.name.id}.#{@def.name.id}".id}}"
  end
end

class Bar
  def bar
    Foo.foo
  end
end

Bar.new.bar => "Foo.bar"
# Should print "Bar.bar"
George Dietrich
@Blacksmoke16
didactic-drunk
@didactic-drunk
My more complex code shows Foo.bar
George Dietrich
@Blacksmoke16
got a link?
didactic-drunk
@didactic-drunk
Not published, I'll work on it
George Dietrich
@Blacksmoke16
the \{{ is impt
otherwise the macro expression is evaluated within the macro def
Ramakrishnan Vasantha Gopal
@vgramkris
hey, new here, posting some general questions out here.
Any reason why String.to_i32! is not implemented?
a : Int64 = (Int32::MAX).to_i64 + 1
b : Int32 | Nil = a.to_i32!
c : String = a.to_s
d : Int32 | Nil = c.to_i32!  # Error: undefined method 'to_i32!' for String
George Dietrich
@Blacksmoke16
what would to_i32! to diff than .to_i32?
do diff*
Ramakrishnan Vasantha Gopal
@vgramkris
if you see above, in line 2, assignment to be works fine
but line 4 is an issue
George Dietrich
@Blacksmoke16
right, can't you just call c.to_i32?
Ramakrishnan Vasantha Gopal
@vgramkris
so if the String cannot be typecasted to i32 then i expect nil instead of a exception
George Dietrich
@Blacksmoke16
it'll raise on over/under flow iirc
Ramakrishnan Vasantha Gopal
@vgramkris
thats right, that is what i am trying to overcome with the '!'
George Dietrich
@Blacksmoke16
you're getting a compile time error not an over/underflow exception
if you want it to wrap, try .to_i32?
Ramakrishnan Vasantha Gopal
@vgramkris
yep thats right
George Dietrich
@Blacksmoke16
so im not sure i see the problem?
going from a number to number has diff semantics than a string to a number, which is probably why they have slightly diff conversion methods
Ramakrishnan Vasantha Gopal
@vgramkris
yeah exactly, so num to num with ! we can do both checking & assignment in same line
but from string to num i need to check with .to_i32? and then assign
that is what i was trying to understand if there is any reason why for string they did not have .to_i32! while they do have .to_i32 for string
George Dietrich
@Blacksmoke16
still not sure i follow, you deff can do like d : Int32 | Nil = c.to_i32?
because to_i32! represents a wrapping operation on the value if it over/underflows. In the case of a string its to_i32? because you cant wrap a string, and needs to handle the case of the string not being a number. So it'll return nil if either of those happen
Ramakrishnan Vasantha Gopal
@vgramkris
okay this makes sense.. thanks!
George Dietrich
@Blacksmoke16
there is a block version of to_i32 that you can use. Which will use the blocks value in case of under/overflow or its not a valid number
e.g. e : Int32 = c.to_i { 0 }
habit, to_i is the same as to_i32
Ramakrishnan Vasantha Gopal
@vgramkris
sure, this would work as well! thanks @Blacksmoke16
George Dietrich
@Blacksmoke16
np, ofc another solution is to just not go from number to string to number :)
but i assume that was just an example
Ramakrishnan Vasantha Gopal
@vgramkris
:)
baiorett
@baiorett
hi, quick question: how do i split crystal classes to multiple files
George Dietrich
@Blacksmoke16
for what reason?
Daniel Worrall
@Daniel-Worrall
Just define them in multiple files.
You can reopen classes
George Dietrich
@Blacksmoke16
but to answer your question just declare the class and add more stuff to ^
re-declare
Daniel Worrall
@Daniel-Worrall
If you're overwriting methods in the same class, require order matters
baiorett
@baiorett
alright thank you
George Dietrich
@Blacksmoke16
i'd suggest thinking if you really want to do that tho. could just make things more confusing
baiorett
@baiorett
im sorta used to splitting classes because i came to crystal from java
George Dietrich
@Blacksmoke16
Other people might not be. Especially if it's a small type