explicit requires?

68 views
Skip to first unread message

Roger Pack

unread,
Sep 28, 2016, 5:38:33 PM9/28/16
to Crystal
Had the thought (I know this isn't ruby-like, but here goes):
In this code:


it includes this:

require "./mess/*"
require "kemal"
require "json"
require "mysql"
require "pool/connection"


class User
  DB.mapping({
    id: Int32,
    first_name:   {type: String, nilable: true},
  ...



It is super non intuitive as to where "DB.mapping" came in.  Did it come from mysql? (I actually don't know, and can't tell).

Perhaps something like

require "pool/connection", ["DB", "ConnectionPool"] # only import those symbols, Python style

or something would be useful?

Cheers!

Brian J. Cardiff

unread,
Sep 28, 2016, 5:49:59 PM9/28/16
to Crystal
But the DB could be defined across multiple source files from different requires.
I think explicit import only works if the class can't be split.

--
You received this message because you are subscribed to the Google Groups "Crystal" group.
To unsubscribe from this group and stop receiving emails from it, send an email to crystal-lang...@googlegroups.com.
To post to this group, send email to crysta...@googlegroups.com.
Visit this group at https://groups.google.com/group/crystal-lang.
To view this discussion on the web visit https://groups.google.com/d/msgid/crystal-lang/50420925-961b-4e96-9d6d-5a87c2354554%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Brian J. Cardiff - Manas Technology Solutions
[ar.phone] +54 (11) 4796-0232
[us.phone] +1 (760) 626-2701
[email] bcar...@manas.com.ar
[web] https://manas.tech/blog/by/bcardiff/

Ary Borenszweig

unread,
Sep 28, 2016, 6:13:04 PM9/28/16
to Crystal

Roger Pack

unread,
Sep 29, 2016, 2:11:58 PM9/29/16
to Crystal


On Wednesday, September 28, 2016 at 3:49:59 PM UTC-6, bcardiff wrote:
But the DB could be defined across multiple source files from different requires.
I think explicit import only works if the class can't be split.

As far as I know this should be possible still...Python handles it by everything "declares itself" in a namespace local to the file.
However, those classes can be shared (or monkey patched) before you include them, as long as you include from "somewhere" as it were...
Thanks.
-roger-

Ary Borenszweig

unread,
Sep 29, 2016, 2:16:24 PM9/29/16
to Crystal
It's not about it being possible or impossible. Pretty much everything is possible :-)

The thing is if we want every file to begin like this:

~~~
import foo from bla
import lala, lele from bleh
import some_more
import hmm_this_is_getting_a_bit_tedious_to_write
import oh_and_ill_need_to_do_this_in_many_more_files

puts "hello" # finally some code that does something!
~~~

vs this:

~~~
puts "hello"
~~~

In Ruby you see very few requires, probably once in a project to use everywhere, because everything is globally namespaced. This can bring clashes and conflicts, maybe some time ago that might have been a problem but now that we are all connected and pretty much know what namespaces are occurpied it's less of a problem. Ruby worked very well in this regards and the community managed to evolve without problems. Refinements were introduced to "solve" this problem, but nobody seems to use them.

So again, it's not that we don't know how to do it or that it can't be done, it's just that it's simpler to do it with "require" (less typing, less boilerplate, less cluttering, despite some possible conflicts)

Roger Pack

unread,
Sep 29, 2016, 2:22:12 PM9/29/16
to crysta...@googlegroups.com
On Thu, Sep 29, 2016 at 12:16 PM, Ary Borenszweig
<aboren...@manas.com.ar> wrote:
> It's not about it being possible or impossible. Pretty much everything is
> possible :-)
>
> The thing is if we want every file to begin like this:
>
> ~~~
> import foo from bla
> import lala, lele from bleh
> import some_more
> import hmm_this_is_getting_a_bit_tedious_to_write
> import oh_and_ill_need_to_do_this_in_many_more_files
>
> puts "hello" # finally some code that does something!

perhaps a "*" option would be useful, or a default that is "everything"

> ~~~
>
> vs this:
>
> ~~~
> puts "hello"
> ~~~
>
> In Ruby you see very few requires, probably once in a project to use
> everywhere, because everything is globally namespaced. This can bring
> clashes and conflicts, maybe some time ago that might have been a problem
> but now that we are all connected and pretty much know what namespaces are
> occurpied it's less of a problem. Ruby worked very well in this regards and
> the community managed to evolve without problems. Refinements were
> introduced to "solve" this problem, but nobody seems to use them.
>
> So again, it's not that we don't know how to do it or that it can't be done,
> it's just that it's simpler to do it with "require" (less typing, less
> boilerplate, less cluttering, despite some possible conflicts)

OK good to know it's theoretically possible :)
In terms of philosophy...I'd say that it (can be) more clear where
classes originate, so you can know in which codebase to look if you
want to track down a class (less "magic" as it were).
As well as avoiding namespace conflicts, as you mentioned ("DB" comes
to mind here, though it was coincidence it was included in my
question).
Cheers!
-roger-
> You received this message because you are subscribed to a topic in the
> Google Groups "Crystal" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/crystal-lang/Yid3gQnlKyI/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> crystal-lang...@googlegroups.com.
> To post to this group, send email to crysta...@googlegroups.com.
> Visit this group at https://groups.google.com/group/crystal-lang.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/crystal-lang/CA%2BdJ3wT3XS2uXKFTcdnegsf8xhT_M-jvnT3oC06iZAM8ubRaNA%40mail.gmail.com.

Oleksii Fedorov

unread,
Sep 29, 2016, 2:26:49 PM9/29/16
to crysta...@googlegroups.com
Alternative solution to the problem of locating where certain symbol (class, variable, constant, whatever) is coming from would be to expose compiler tool that allows you to point at certain place in code and ask compiler to tell you what is it and where it came from. Finally it can be integrated with your favorite code editor or IDE.

What do you think?

Roger Pack

unread,
Sep 30, 2016, 2:03:42 PM9/30/16
to crysta...@googlegroups.com
On Thu, Sep 29, 2016 at 12:26 PM, Oleksii Fedorov
<waterl...@gmail.com> wrote:
> Alternative solution to the problem of locating where certain symbol (class,
> variable, constant, whatever) is coming from would be to expose compiler
> tool that allows you to point at certain place in code and ask compiler to
> tell you what is it and where it came from. Finally it can be integrated
> with your favorite code editor or IDE.
>
> What do you think?

Good idea, since Crystal is all compiled you'd think an IDE would be
able to tell me where symbols came from (and where to find the
source/docu for methods invoked)...which might be enough.
> https://groups.google.com/d/msgid/crystal-lang/CAFCn8x0iQ3x_%3DCOkuLezU3u6Wn1pcwnPDAYk%3DrbxnJq%3Ds8iTLw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages