Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • Dec 02 22:36
    straight-shoota synchronize #12809
  • Dec 02 22:35
    straight-shoota synchronize #12809
  • Dec 02 22:24
    straight-shoota milestoned #12779
  • Dec 02 22:24
    straight-shoota milestoned #12779
  • Dec 02 17:07
    straight-shoota milestoned #12800
  • Dec 02 17:07
    straight-shoota milestoned #12800
  • Dec 02 17:07
    straight-shoota milestoned #12773
  • Dec 02 17:07
    straight-shoota milestoned #12773
  • Dec 02 16:56
    HertzDevil labeled #12800
  • Dec 02 16:56
    HertzDevil unlabeled #12800
  • Dec 02 14:17
    HertzDevil labeled #12817
  • Dec 02 14:15
    imsofi opened #12817
  • Dec 02 14:15
    imsofi labeled #12817
  • Dec 02 13:51
    straight-shoota milestoned #12784
  • Dec 02 13:51
    straight-shoota milestoned #12784
  • Dec 02 13:50
    straight-shoota milestoned #12808
  • Dec 02 13:50
    straight-shoota milestoned #12808
  • Dec 02 13:40
    HertzDevil synchronize #12784
  • Dec 02 13:39
    HertzDevil ready_for_review #12784
  • Dec 02 13:26
    straight-shoota milestoned #12816
MB
@marceloboeira
given "hello" I want to create MyGenericClass(String).new("hello")
From IRC (bridge bot)
@FromIRC
<oprypin> @marceloboeira, macros deal only with literals
MB
@marceloboeira
that's what I thought :/
From IRC (bridge bot)
@FromIRC
<oprypin> that would hopefully be MyGenericClass.new({{value}})
<Papierkorb> @marceloboeira, that syntax should work fine without for this case: The generic type can be auto-deduced from the arguments given to #initialize
<oprypin> MyGenericClass(typeof({{value}})).new({{value}}) might work but is redundant
MB
@marceloboeira
hmm
the typeof works!!1
thanks :)
From IRC (bridge bot)
@FromIRC
<oprypin> @marceloboeira, yeah but pls dont ignore the part about not needing it
MB
@marceloboeira
yes, I actually am experimenting the limits of the macros
it is nothing serious
From IRC (bridge bot)
@FromIRC
<Papierkorb> Macros are quite capable in general, up to the point that you can call external programs (!), or as long you adhere to the Crystal language syntax, implement your own little 'script' language with it.
<Papierkorb> The latter because you have pretty good access to the AST if passed as argument
MB
@marceloboeira
interesting that, given a default name of a class (upper camelcase), I can't define a macro with a similar structure. e.g.:
class Foo; end

macro FooBar(value)
...
end

FooBar("foo")
... expecting token 'CONST', not 'nil'
even if I don't have a class with the same name, which would be understandable...
Ghost
@ghost~55586c4815522ed4b3e08557
a macro name, like methods should start with a lowercase letter
a word starting by an upper case letter is recognized as a CONST (a type, a constant, ..)
MB
@marceloboeira
:+1:
but is that because of the order of evaluation of the macro?
Ghost
@ghost~55586c4815522ed4b3e08557
no it has nothing to do with the order of execution, but with the parser: FooBar(...) is recognized as a generic type (like Array(String))
and it mess up something when trying to parse it
MB
@marceloboeira
I thought maybe the macro expansion happened before the rest of the evaluation/parsing. But it makes sense to happen after considering that you need to check the types
From IRC (bridge bot)
@FromIRC
<oprypin> that's the magical part
<oprypin> macros need to be evaluated before everything but also after everything :⁠D
<oprypin> in simple cases macros are evaluated before everything, but for example if you refer to the current type in the macro, that delays the expansion until the type's semantics are figured out
MB
@marceloboeira
macros need to be evaluated before everything but also after everything :⁠D
http://www.reactiongifs.com/wp-content/uploads/2013/10/tim-and-eric-mind-blown.gif
From IRC (bridge bot)
@FromIRC
<Papierkorb> Why
MB
@marceloboeira
just experimenting, I wanted to do it with macros, but then I've realised that it does not make any sense considering that you might have dynamic results
From IRC (bridge bot)
@FromIRC
<Papierkorb> For real code, you'd just rely on Nil
<Papierkorb> In your example, it'd simply be a Unsafe.new.io || "Empty..."
Cris Ward
@crisward
Just need 4,027 more stars to overtake ruby... https://github.com/showcases/programming-languages :wink:
MB
@marceloboeira
But considering everything can be nil,
Unsafe.new.io.foo.bar
you would have to
(((Unsafe.new.io || "default").foo) || "default_2").bar
From IRC (bridge bot)
@FromIRC
<Papierkorb> No
<Papierkorb> Everything can't be nil
MB
@marceloboeira
not everything, but the calls to unsafe things
IO related
From IRC (bridge bot)
@FromIRC
<Papierkorb> That's the whole point. Nil is a type on its own in Crystal. Try this: def foo(a : String); a; end; foo(nil) - Won't work
<Papierkorb> ..huh
<Papierkorb> IO is a type. If you expect an IO, or pledge to return an IO, you can only return that, no nil
<Papierkorb> Impossible. If you do find a way, it's a serious compiler bug
<RX14> if you do find a way you'll get segfaults lol
<Papierkorb> The only way to 'escape' having to return something matching the method prototype is raising an Exception. Another common theme is something like this: def read_input : String?; ...; end. It may return a String, or nil. In this case, the compiler will force you to work with the result in a type-safe manner
MB
@marceloboeira

e.g.:

User.where(foo: "bar").first.name

will never be nil?

From IRC (bridge bot)
@FromIRC
<Papierkorb> If #where, #first and #name are not nil-able, never.
<RX14> @marceloboeira have a pay with nil, you'll find out what works and what doesn't.
MB
@marceloboeira
you mean because of the nilable String?, ok, fair enough
From IRC (bridge bot)
@FromIRC
<Papierkorb> The type system of Crystal alone safes you from having to clutter your code with Just(x) etc.