# Can this be more Juliesque?

**URL:** <https://discourse.julialang.org/t/can-this-be-more-juliesque/70684>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 31, 2021, 12:18pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684 "2021-10-31T12:18:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [October 31, 2021, 12:18pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/1 "2021-10-31T12:18:48Z")

</div>

Dear all,

I am a relative newbie in Julia, and what I am about to ask might sound ludicrously simple to you. So, here is the situation. I would like to introduce physical properties in a simulation program I am developing in Julia and they may be defined as constant, or as functions of other properties like temperature, time, or what have you. So, I would say I need some sort of abstraction going in the code. I came up with this:

```julia
abstract type AbstractProperty end

mutable struct ConstantProperty <: AbstractProperty
  constant :: Float64 # holds constant value of a property
  function ConstantProperty(a :: Float64) new(a) end
end

mutable struct EquationProperty <: AbstractProperty
  equation :: String # holds an equation describing the property
  function EquationProperty(s :: String) new(s) end
end

function evaluate(a :: ConstantProperty)
  return a.constant
end

function evaluate(a :: ConstantProperty, e :: String)
  return a.constant
end

function evaluate(a :: EquationProperty, e :: String)
  return eval(Meta.parse(e*"; "*a.equation))
end

```

which behavior as I wanted. To be more precise, I can do:

```julia
julia> a = ConstantProperty(13.0)
ConstantProperty(13.0)

julia> b = EquationProperty("x^2")
EquationProperty("x^2")

julia> evaluate(a)
13.0

julia> evaluate(a, "x=2")
13.0

julia> evaluate(b, "x=2")
4

```

But, I am wondering, is there a better way to do it? I mean something which would be closer to Julia style, something more sophisticated or more robust? Can you already see some performance pitfalls that I am unaware of at this stage? It also seems to me I do not need the `AbstractProperty` at all ☹

_Cheers_

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [October 31, 2021, 12:27pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/2 "2021-10-31T12:27:47Z")

</div>

I will not give advice on the programming style (I am still not that experienced a Julia programmer), but if I interpret your requirements/goals correctly, it seems to me that you are essentially trying to mimick what is already implemented elsewhere.

In particular, have a look at [Modia.jl](https://github.com/ModiaSim/Modia.jl). They also distinguish between parameters, variables, equations, models, pretty much in the Modelica’s Object Oriented Modeling (OOM) style.

Similar (and perhaps even more Julian style) reference can be made to [ModelingToolkit.jl](https://github.com/SciML/ModelingToolkit.jl).

If you are implementing these things just for the fun of learning, it is perfectly fine. If you are looking for an existing tool, have a look the two packages.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 31, 2021, 2:29pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/3 "2021-10-31T14:29:36Z")

</div>

> [@Niceno](#):
>
> ```julia
> julia> evaluate(b, "x=2")
> 
> ```

You can simplify things if you are willing to accept Julia anonymous functions as input parameters. For example, a minimal example is:

```julia
evaluate(f, a) = f(a) 

a = 13.0
evaluate(x -> x^2, a)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 31, 2021, 2:54pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/4 "2021-10-31T14:54:13Z")

</div>

Using code stored in strings and using `eval` to call it is pretty horrible 😉 ☠🎃 (in the spirit of Halloween here). It’s both slow, awkward and unsafe. But don’t worry, it’s a typical beginner’s mistake 🙂

Definitely use functions here, anonymous most likely. Also, I think that constant properties should probably be held in immutable structs, not mutable ones.

(Sorry for using strong emphasis here, I just wanted to make clear that using `eval` with code strings is a very bad idea, instead of just saying that there might be better ways.)

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [October 31, 2021, 3:23pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/5 "2021-10-31T15:23:06Z")

</div>

> [@Niceno](#):
>
> `function EquationProperty(s :: String) new(s) end`

For one liner functions, you can the other function definition syntax:

```julia
EquationProperty(s::String) = new(s)

```

Also, in this case, you can drop this constructor completely because Julia will automatically define a constructor for structs. For example:

```julia
julia> struct B
       x::Int
       end

julia> B(1)
B(1)

```

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [October 31, 2021, 3:23pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/6 "2021-10-31T15:23:56Z")

</div>

Thanks for the advice. It is not really the code I am storing in strings, but certain physical properties depending on user input.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 31, 2021, 3:26pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/7 "2021-10-31T15:26:15Z")

</div>

You’re storing small pieces of code as strings, right? That’s what I mean. Using anonymous functions is both powerful and fast, and is what I’d recommend.

With code strings, they must be parsed and compiled before being run, which is slow in itself, also it’s fundamentally type instable.

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [October 31, 2021, 3:29pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/8 "2021-10-31T15:29:23Z")

</div>

Well, if you want to call them small pieces of code, that is fine with me. But these are actually physical properties I read from external files when simulation starts.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 31, 2021, 3:30pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/9 "2021-10-31T15:30:50Z")

</div>

That complicates things a bit. You have no way of knowing what they are beforehand?

---

<div class="post-metadata">

**Author:** ![Niceno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/niceno/32/29648_2.png) [@Niceno](https://discourse.julialang.org/u/Niceno)\
**Post date:** [October 31, 2021, 3:32pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/10 "2021-10-31T15:32:20Z")

</div>

Not really.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [October 31, 2021, 3:57pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/11 "2021-10-31T15:57:26Z")

</div>

I’ve never been in that situation, but at least I suggest you parse and evaluate the strings into functions before passing them to your type constructors, so parsing and evaluation only happens once.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [October 31, 2021, 4:43pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/12 "2021-10-31T16:43:07Z")

</div>

Running arbitrary code from user input is real dangerous.

---

<div class="post-metadata">

**Author:** ![bvrb](https://avatars.discourse-cdn.com/v4/letter/b/76d3ee/32.png) [@bvrb](https://discourse.julialang.org/u/bvrb)\
**Post date:** [October 31, 2021, 7:16pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/13 "2021-10-31T19:16:39Z")

</div>

Alternatively, if there is a somewhat limited amount of functions the user could possibly want to invoke, you could predefine them in the main code and let the user specify the name of the function (that should be used for a given property) in a config file, along with constant parameters for the simulation (I like the [TOML](https://docs.julialang.org/en/v1/stdlib/TOML/) format, which is part of the Julia standard library). If you really need arbitrary function evaluation, this will of course not work.

[This post on stackoverflow](https://stackoverflow.com/questions/34016768/julia-invoke-a-function-by-a-given-string/34023458#34023458) gives quite detailed instructions on how to invoke a particular function if you have its name given as a string, without resorting to `eval`.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [November 5, 2021, 8:07pm UTC](https://discourse.julialang.org/t/can-this-be-more-juliesque/70684/14 "2021-11-05T20:07:54Z")

</div>

> [@Niceno](#):
>
> But these are actually physical properties

Sounds like an application for [GitHub - PainterQubits/Unitful.jl: Physical quantities with arbitrary units](https://github.com/PainterQubits/Unitful.jl)
