Skip to content

Commit

Permalink
make "using Base" implicit, and add baremodule. closes #1522
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Nov 29, 2012
1 parent 3708e1c commit deb6ee8
Show file tree
Hide file tree
Showing 26 changed files with 20 additions and 34 deletions.
1 change: 0 additions & 1 deletion base/blas.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
typealias LapackType Union(Float64,Float32,Complex128,Complex64)

module BLAS
using Base

export copy!,
scal!,
Expand Down
1 change: 0 additions & 1 deletion base/dsp.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module DSP

using Base
using Base.FFTW

export FFTW, filt, deconv, conv, conv2, xcorr, fftshift, ifftshift,
Expand Down
2 changes: 0 additions & 2 deletions base/fftw.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module FFTW

using Base

export bfft, bfftn, brfft, brfftn, fft, fft2, fft3, fftn,
ifft, ifft2, ifft3, ifftn, irfft, irfftn, rfft, rfftn

Expand Down
2 changes: 0 additions & 2 deletions base/git.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module Git
# some utility functions for working with git repos
#

using Base

dir() = readchomp(`git rev-parse --git-dir`)
modules(args::Cmd) = readchomp(`git config -f .gitmodules $args`)
different(verA::String, verB::String, path::String) =
Expand Down
1 change: 0 additions & 1 deletion base/grisu.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Grisu
using Base
export print_shortest
export @grisu_ccall, NEG, DIGITS, BUFLEN, LEN, POINT

Expand Down
1 change: 0 additions & 1 deletion base/lapack.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
## The LAPACK module of interfaces to LAPACK subroutines
module LAPACK
using Base

liblapack = Base.liblapack_name

Expand Down
2 changes: 0 additions & 2 deletions base/librandom.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module LibRandom

using Base

export DSFMT_state, dsfmt_get_min_array_size, dsfmt_get_idstring,
dsfmt_init_gen_rand, dsfmt_gv_init_gen_rand,
dsfmt_init_by_array, dsfmt_gv_init_by_array,
Expand Down
2 changes: 0 additions & 2 deletions base/math.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Math

using Base

export sin, cos, tan, sinh, cosh, tanh, asin, acos, atan,
asinh, acosh, atanh, sec, csc, cot, asec, acsc, acot,
sech, csch, coth, asech, acsch, acoth, sinc, cosc,
Expand Down
1 change: 0 additions & 1 deletion base/pcre.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## low-level pcre interface ##

module PCRE
using Base

include("pcre_h.jl")

Expand Down
1 change: 0 additions & 1 deletion base/pkg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module Pkg
# Julia's git-based declarative package manager
#

using Base
using Metadata

import Git
Expand Down
1 change: 0 additions & 1 deletion base/pkgmetadata.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ require("linprog")

module Metadata

using Base
using LinProgGLPK

import Git
Expand Down
1 change: 0 additions & 1 deletion base/printf.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Printf
using Base
using Base.Grisu
export @printf, @sprintf

Expand Down
1 change: 0 additions & 1 deletion base/rng.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module RNG

using Base
using Base.LibRandom

export librandom_init, srand,
Expand Down
4 changes: 2 additions & 2 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Base
baremodule Base

include("export.jl")

Expand Down Expand Up @@ -270,7 +270,7 @@ begin
typeinf_ext(minf[1][3], atypes, (), minf[1][3])
end

end # module Base
end # baremodule Base

using Base

Expand Down
3 changes: 2 additions & 1 deletion contrib/julia-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@
'("if" "else" "elseif" "while" "for" "begin" "end" "quote"
"try" "catch" "return" "local" "abstract" "function" "macro" "ccall"
"finally" "typealias" "break" "continue" "type" "global" "@\\w+"
"module" "using" "import" "export" "const" "let" "bitstype" "do")
"module" "using" "import" "export" "const" "let" "bitstype" "do"
"baremodule" "importall")
"\\|") "\\)\\>")
'font-lock-keyword-face)
'("\\<\\(true\\|false\\|C_NULL\\|Inf\\|NaN\\|Inf32\\|NaN32\\|nothing\\)\\>" . font-lock-constant-face)
Expand Down
2 changes: 2 additions & 0 deletions contrib/julia.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
<item> export </item>
<item> global </item>
<item> import </item>
<item> importall </item>
<item> in </item>
<item> local </item>
<item> macro </item>
<item> module </item>
<item> baremodule </item>
<item> return </item>
<item> typealias </item>
</list>
Expand Down
1 change: 0 additions & 1 deletion examples/bigfib.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require("extras/bigint")

module BigFib
using Base
export bigfib
const BigInt = Main.BigInt

Expand Down
1 change: 0 additions & 1 deletion extras/arpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ require("linalg_sparse")
## SparseMatrixCSC is not defined even after the require("sparse")

#module ARPACK
#using Base
#export eigs, svds

_jl_libarpack = dlopen("libarpack")
Expand Down
1 change: 0 additions & 1 deletion extras/glpk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require("sparse")

module GLPK
using Base

export
# Types
Expand Down
1 change: 0 additions & 1 deletion extras/gzip.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## gzip file io ##

module GZip
using Base

import Base.show, Base.fd, Base.close, Base.flush, Base.truncate, Base.seek
import Base.skip, Base.position, Base.eof, Base.read, Base.readall
Expand Down
1 change: 0 additions & 1 deletion extras/linprog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require("glpk")

module LinProgGLPK
using Base

import GLPK

Expand Down
1 change: 0 additions & 1 deletion extras/profile.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Profile
using Base

PROFILE_LINES = 1
PROFILE_DESCEND = 2
Expand Down
1 change: 0 additions & 1 deletion extras/suitesparse.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require("linalg_sparse")

#module Suitesparse
#using Base
#import Base.SparseMatrixCSC

import Base.size, Base.nnz, Base.eltype, Base.show
Expand Down
1 change: 0 additions & 1 deletion extras/zlib.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Zlib
using Base

export
# Compression routines
Expand Down
8 changes: 4 additions & 4 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@

(define reserved-words '(begin while if for try return break continue
function macro quote let local global const
abstract typealias type bitstype
module using import export ccall do))
abstract typealias type bitstype ccall do
module baremodule using import export importall))

(define (syntactic-op? op) (memq op syntactic-operators))
(define (syntactic-unary-op? op) (memq op syntactic-unary-operators))
Expand Down Expand Up @@ -943,9 +943,9 @@
(eq? (car assgn) 'local))))
(error "expected assignment after const")
`(const ,assgn))))
((module)
((module baremodule)
(let ((name (parse-atom s)))
(begin0 (list word name (parse-block s))
(begin0 (list 'module (eq? word 'module) name (parse-block s))
(expect-end s))))
((export)
(let ((es (map macrocall-to-atsym
Expand Down
12 changes: 10 additions & 2 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
{
assert(ex->head == module_sym);
jl_module_t *last_module = jl_current_module;
jl_sym_t *name = (jl_sym_t*)jl_exprarg(ex, 0);
int std_imports = (jl_exprarg(ex,0)==jl_true);
jl_sym_t *name = (jl_sym_t*)jl_exprarg(ex, 1);
if (!jl_is_symbol(name)) {
jl_type_error("module", (jl_value_t*)jl_sym_type, (jl_value_t*)name);
}
Expand All @@ -52,10 +53,17 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
// export all modules from Main
if (parent_module == jl_main_module)
jl_module_export(jl_main_module, name);

// add standard imports unless baremodule
if (std_imports) {
if (jl_base_module != NULL)
jl_module_using(newm, jl_base_module); // using Base
}

JL_GC_PUSH(&last_module);
jl_current_module = newm;

jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 1))->args;
jl_array_t *exprs = ((jl_expr_t*)jl_exprarg(ex, 2))->args;
JL_TRY {
for(int i=0; i < exprs->length; i++) {
// process toplevel form
Expand Down

12 comments on commit deb6ee8

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent.

@staticfloat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I really like this, and baremodule is a neat solution.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also likely to happen: non-bare modules will import all operators by default, which seems to match people's intuitive expectation that operators are global whereas names can be local.

@johnmyleswhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. We seem to still need an explicit using Base.Intrinsics in DataFrames. Is that likely to change?

And any thoughts about having Base.(push)(df::DataFrame, c::DataVec) = foo?

@johnmyleswhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question: how much are we looking down upon the as-yet unimplemented importall?

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to still need an explicit using Base.Intrinsics in DataFrames. Is that likely to change?

No since the idea is that intrinsics should not be generally available for users to stumble upon casually – they are not, after all, normal functions and are quite tricky to work with. Accordingly, they are intentionally not exported from Base.

And any thoughts about having Base.(push)(df::DataFrame, c::DataVec) = foo?

Yes, but without the parens :-)

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question: how much are we looking down upon the as-yet unimplemented importall?

What do you mean by "looking down upon"? As in its a bad thing? The use-case for importall in my thinking is when you're doing something like implementing a new collection type and you know you want to add methods to all of the functions that collections generally have. Then you can put something like importall Collections or import Collections.* depending on what syntax we settle on, and just add methods to push without having to explicitly prefix it with Base. If that's the usage then it's fine. If you do importall Base and stomp all over everything, then that's probably not a great idea, but hey, we're all grownups here.

@johnmyleswhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, importall seems treacherous if you're not exactly sure when you're overwriting something in Base as opposed to merely complementing it. Today I discovered that gradient is in Base now. That's exactly the sort of thing I'm likely to overwrite by accident.

It would be useful to have warnings when I destroy something in Base rather than extend it.

@JeffBezanson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have thought it might be good to warn if a method is overwritten from another module.

@StefanKarpinski
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But importing something is how you indicate that you intend to add methods, no? Otherwise how can you do it without warnings?

@JeffBezanson
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By "overwrite" I mean fully clobber an existing definition, not add a new definition, like

import Base.sin
sin(x::Float64) = 0

@johnmyleswhite
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, that sort of thing is exactly my fear with doing an importall without some sort of warning about clobbering Base.

Please sign in to comment.