Direct type declaration

I was wondering if it is possible in julia to assign a custom variable or an existing type without calling the constructor.

Something like direct class declaration in C ++.

Here is an example of what I intend to achieve:

type foo a end #custom type a = foo b = foo #julia type, force c to be of type Int c = Int aa = 5.5 ba = 4.5 c = 6 

Change To clarify my question:

In C ++ or Fortran, a common coding practice declares a variable at some point for later use.

I don’t remember the correct Fortran syntax, but in C ++ you would write something like:

 class foo; class bar; int a; class foo{ private: bar myBar; public: foo(int a){ myBar( a ); } } class bar{ public: bar(int a){ std::cout << a << std::endl; } } a = 3; foo( a ); 

The advantage of this code structure is that it allows you to define objects / types / variables before using them.

+7
source share
3 answers

You can declare a variable, but not in the global scope ; these are the constructs in Julia that introduce a new scope :

Some constructs in the language introduce scope blocks, which are areas of code that can be the scope of a set of variables. The scope of a variable cannot be an arbitrary set of source strings; instead, it will always match one of these blocks. Constructions representing such blocks:

  • functional bodies (any syntax)
  • while cycles
  • for loops
  • try the blocks
  • catch blocks
  • finally blocks
  • let the blocks
  • Type of blocks.

In this list, in particular, there are no starting blocks and if blocks that do not introduce new blocks of the scope.

You can optionally use type declarations :

 julia> x ERROR: UndefVarError: x not defined julia> x::Int ERROR: UndefVarError: x not defined julia> begin x end # still in global scope ERROR: UndefVarError: x not defined julia> begin x::Int end ERROR: UndefVarError: x not defined julia> let x end # local scope julia> let x; x end ERROR: UndefVarError: x not defined 

Note that Julia will try to convert the value to the specified type:

 julia> let x::Int # declare variables y::Float64 = 7 # converts (if possible) x = y # converts (if possible) x, y end (7, 7.0) julia> function foo(x, y) x::Int y::Float64 z # Any # there must be assignment for conversion to happen x, y = x, y z = 5im x, y, z end foo (generic function with 1 method) julia> foo(3.0, 7) (3,7.0,0 + 5im) 

Edited example:

Define types / immutable.

 julia> type Foo{T<:Number} x::T end julia> type Bar x end julia> immutable Baz a b c end 

Define conversion methods.

 julia> import Base.convert julia> convert{T<:Number}(::Type{Foo{T}}, x::Number) = Foo(T(x)) convert (generic function with 535 methods) julia> convert(::Type{Bar}, x) = Bar(x) convert (generic function with 536 methods) julia> convert(::Type{Baz}, xs::NTuple{3}) = Baz(xs...) convert (generic function with 537 methods) 

So you can do something like this:

 julia> let # decalre variables: a::Foo{Int} b::Foo{Float64} c::Bar d::Baz e::Int # assign values: e = 42 a = e b = e c = string(e) d = 'a', e, "test" [a, b, c, d] end 4-element Array{Any,1}: Foo{Int64}(42) Foo{Float64}(42.0) Bar("42") Baz('a',42,"test") julia> 
+7
source

It seems to me that you want to instantiate the type without specifying the content, and then populate it later. This can be done by creating a constructor that leaves some fields uninitialized:

 type Foo a # inner constructor that leaves all fields uninitialized Foo() = new() end a = Foo() b = Foo() aa = 5.5 ba = 4.5 

By providing fewer new arguments in the internal constructor than fields with a type, the latter become uninitialized. Error reading uninitialized field before it is assigned a value.

Is that what you were?

+3
source

There is no need to pre-declare variables in Julia, as the type is simply determined by use.

If you want to restrict certain types, you can do this:

 type MyType a::Int end 

(Note that type names have conditional source capital.)

 b = MyType(1) c = MyType(2) d = MyType(3.5) # error 
+2
source

Source: https://habr.com/ru/post/1232682/


All Articles