0

I want to define a struct:

struct unit_SI_gen
    x::Float32
    const c = 2.99792458e8
    speed(x)=c*x
end

However, it raise an error :

syntax: "c = 2.99792e+08" inside type definition is reserved

I know I cant use struct as class in python, but I can not find how to solve this problem.

How to define a constant in struct ?

Mr.CG
  • 69
  • 2
  • 5
  • 2
    There's a lot of implicit assumptions in your question, all of which are false. In the first place, a `const` in julia isn't what you think it is, and a `struct` in julia isn't what you think it is. It may be worth reading the relevant parts in the docs first, these should answer most of your question. – Tasos Papastylianou Sep 04 '18 at 12:05

2 Answers2

5

Given I agree with what was said above about normal usage of struct in Julia, it is actually possible to define what was requested in the question using an inner constructor:

struct unit_SI_gen{F} # need a parametric type to make it fast
    x::Float32
    c::Float64 # it is a constant across all unit_SI_gen instances
    speed::F # it is a function

    function unit_SI_gen(x)
        c = 2.99792458e8
        si(x) = c*x
        new{typeof(si)}(x, c, si)
    end
end
Bogumił Kamiński
  • 66,844
  • 3
  • 80
  • 107
  • Purely for the sake of pedantry and challenging you further, this assumes x is also 'constant' / 'private', so it's not quite what is being asked :) (assuming what is being asked is clear, heheh) – Tasos Papastylianou Sep 04 '18 at 15:27
  • 2
    The difference is (as I am sure you know :)) that `x` may change across instances and `c` may not. And at least this is what I thought that is being asked for - that something like `final static` in Java is sought for. Of course this is true only if `c` is bits type (but this is what was asked for). If `c` were mutable then its pointer would differ between instances unless it were allocated at compile time using a macro (and now we are getting into even more muddy waters). – Bogumił Kamiński Sep 04 '18 at 15:52
2

I second @Tasos's comment, you should probably make yourself familiar with Julia's structs first. The relevant part of the documentation is probably here.

Since you declared your struct as struct (in contrast to mutable struct) it is immutable and hence all (immutable) fields of the struct are constants in the sense that they can't be changed.

julia> struct A
       x::Int
       end

julia> a = A(3)
A(3)

julia> a.x = 4
ERROR: type A is immutable
Stacktrace:
 [1] setproperty!(::A, ::Symbol, ::Int64) at .\sysimg.jl:19
 [2] top-level scope at none:0

Note, that they get their unchangeable value in the construction process and not in the struct definition.

Also, methods should generally live outside of the struct definition.

carstenbauer
  • 9,817
  • 1
  • 27
  • 40
  • 1
    just to avoid confusing OP even further, it's less that they generally live outside, and more that one cannot define them as normal fields in the first place (short of hacking your way to it [as per my favourite post here](https://stackoverflow.com/q/39133424/4183191) :p ) – Tasos Papastylianou Sep 04 '18 at 12:35
  • 1
    Nice post indeed :) – carstenbauer Sep 04 '18 at 13:42