From 50908bfbed3ced7b78469ee0313e80c0f6bc6c88 Mon Sep 17 00:00:00 2001 From: Lei WANG Date: Sat, 12 Jul 2014 19:18:03 +0800 Subject: [PATCH] 7.12 e35b83b18e --- conf.py | 15 ++++++++++----- manual/constructors.rst | 12 ++++++------ manual/control-flow.rst | 3 +++ manual/faq.rst | 36 ++++++++++++++++++++++++++++++++++++ manual/packages.rst | 4 ++-- manual/types.rst | 18 +++++++++++++++--- 6 files changed, 72 insertions(+), 16 deletions(-) diff --git a/conf.py b/conf.py index 8507dde..af7b838 100644 --- a/conf.py +++ b/conf.py @@ -11,7 +11,7 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import sys, os, re # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the @@ -56,10 +56,15 @@ # |version| and |release|, also used in various other places throughout the # built documents. # -# The short X.Y version. -version = '0.3' -# The full version, including alpha/beta/rc tags. -release = '0.3.0-dev' +try: + # The full version, including alpha/beta/rc tags. + with open("../VERSION") as f: + release = f.read().rstrip() + # The short X.Y version. + version = '.'.join(re.split('[.-]', release)[:2]) +except: + release = 'X.Y.Z-unknown' + version = 'X.Y' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/manual/constructors.rst b/manual/constructors.rst index 809cea4..3c4b65d 100644 --- a/manual/constructors.rst +++ b/manual/constructors.rst @@ -94,7 +94,7 @@ type T2 x::Int64 - T2(x::Int64) = new(x) + T2(x) = new(x) end julia> T1(1) @@ -104,10 +104,10 @@ T2(1) julia> T1(1.0) - no method T1(Float64,) + T1(1) julia> T2(1.0) - no method T2(Float64,) + T2(1) 内部构造方法能不写就不写。提供默认值之类的事儿,应该写成外部构造方法,由它们调用内部构造方法。 @@ -229,13 +229,13 @@ Point{Int64}(1,2) julia> Point{Int64}(1.0,2.5) - ERROR: no method Point{Int64}(Float64, Float64) + ERROR: InexactError() julia> Point{Float64}(1.0,2.5) Point{Float64}(1.0,2.5) julia> Point{Float64}(1,2) - ERROR: no method Point{Float64}(Int64, Int64) + Point{Float64}(1.0,2.0) 上面的参数化构造方法等价于下面的声明: :: @@ -243,7 +243,7 @@ x::T y::T - Point(x::T, y::T) = new(x,y) + Point(x,y) = new(x,y) end Point{T<:Real}(x::T, y::T) = Point{T}(x,y) diff --git a/manual/control-flow.rst b/manual/control-flow.rst index 3006b00..cbc623e 100644 --- a/manual/control-flow.rst +++ b/manual/control-flow.rst @@ -405,6 +405,9 @@ It will be evaluated and returned depending on the preceding conditionals: (2,3) (2,4) +A ``break`` statement inside such a loop exits the entire nest of loops, +not just the inner one. + .. _man-exception-handling: 异常处理 diff --git a/manual/faq.rst b/manual/faq.rst index bbb54d7..d6de383 100644 --- a/manual/faq.rst +++ b/manual/faq.rst @@ -87,6 +87,42 @@ But here is a thing you should pay attention to: suppose ``x`` is bound to an Ar Here we created a function ``change_array!()``, that assigns ``5`` to the first element of the Array. We passed ``x`` (which was previously bound to an Array) to the function. Notice that, after the function call, ``x`` is still bound to the same Array, but the content of that Array changed. + +Can I use ``using`` or ``import`` inside a function? +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +No, you are not allowed to have a ``using`` or ``import`` statement inside +a function. If you want to import a module but only use its symbols +inside a specific function or set of functions, you have two options: + +1. Use ``import``:: + + import Foo + function bar(...) + ... refer to Foo symbols via Foo.baz ... + end + + + This loads the module Foo and defines a variable ``Foo`` that refers + to the module, but does not import any of the other symbols from the + module into the current namespace. You refer to the ``Foo`` symbols by + their qualified names ``Foo.bar`` etc. + + +2. Wrap your function in a module:: + + module Bar + export bar + using Foo + function bar(...) + ... refer to Foo.baz as simply baz .... + end + end + using Bar + + This imports all the symbols from Foo, but only inside the module Bar. + + 类型,类型声明和构造方法 ------------------------ diff --git a/manual/packages.rst b/manual/packages.rst index 259e2ce..2ee6c34 100644 --- a/manual/packages.rst +++ b/manual/packages.rst @@ -333,8 +333,8 @@ This creates the directory ``~/.julia/v0.3/FooBar``, initializes it as a git rep src/FooBar.jl | 5 +++++ 4 files changed, 44 insertions(+) -At the moment, the package manager knows about the MIT "Expat" License, indicated by ``"MIT"``, and the Simplified BSD License, indicated by ``"BSD"``. -If you want to use a different license, you can ask us to add it to the package generator, or just pick one of these two and then modify the ``~/.julia/v0.3/PACKAGE/LICENSE.md`` file after it has been generated. +At the moment, the package manager knows about the MIT "Expat" License, indicated by ``"MIT"``, the Simplified BSD License, indicated by ``"BSD"``, and version 2.0 of the Apache Software License, indicated by ``"ASL"``. +If you want to use a different license, you can ask us to add it to the package generator, or just pick one of these three and then modify the ``~/.julia/v0.3/PACKAGE/LICENSE.md`` file after it has been generated. If you created a GitHub account and configured git to know about it, ``Pkg.generate`` will set an appropriate origin URL for you. It will also automatically generate a ``.travis.yml`` file for using the `Travis `_ automated testing service. diff --git a/manual/types.rst b/manual/types.rst index 1bed5c3..509dcf5 100644 --- a/manual/types.rst +++ b/manual/types.rst @@ -161,14 +161,22 @@ not a declaration. Foo("Hello, world.",23,1.5) julia> typeof(foo) - Foo (constructor with 1 method) + Foo (constructor with 2 methods) -由于没有约束 ``bar`` 的类型,它可以被赋任意值; ``baz`` 则必须是 ``Int`` , ``qux`` 必须是 ``Float64`` 。参数必须与构造类型签名 ``(Any,Int,Float64)`` 相匹配: +When a type is applied like a function it is called a *constructor*. +Two constructors are generated automatically (these are called *default +constructors*). One accepts any arguments and calls ``convert`` to convert +them to the types of the fields, and the other accepts arguments that +match the field types exactly. The reason both of these are generated is +that this makes it easier to add new definitions without inadvertently +replacing a default constructor. + +由于没有约束 ``bar`` 的类型,它可以被赋任意值;但是 ``baz`` 必须能被转换为 ``Int`` : .. doctest:: julia> Foo((), 23.5, 1) - ERROR: no method Foo((), Float64, Int64) + ERROR: InexactError() You may find a list of field names using the ``names`` function. @@ -456,6 +464,10 @@ Julia 的类型系统支持参数化:类型可以引入参数,这样类型 julia> Point{Float64}(1.0,2.0,3.0) ERROR: no method Point{Float64}(Float64, Float64, Float64) + +Only one default constructor is generated for parametric types, since +overriding it is not possible. This constructor accepts any arguments +and converts them to the field types. 大多数情况下不需要提供 ``Point`` 对象的类型,它可由参数类型来提供信息。因此,可以不提供 ``T`` 的值: