Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties #18

Open
mauro3 opened this issue Sep 19, 2016 · 14 comments
Open

Properties #18

mauro3 opened this issue Sep 19, 2016 · 14 comments

Comments

@mauro3
Copy link
Owner

mauro3 commented Sep 19, 2016

It would be cool to have fields which are always calculated:

@with_kw type P
    a=1
    b=2
    @calc c=a+3b
end

would translate into something like

type P
    a
    b
    c
    P(a,b) = (c=a+3b; new(a,b,c))
    ...
end
...

(i.e. there would be no constructor which allows setting c)

@carstenbauer
Copy link

carstenbauer commented Mar 10, 2018

I was going to ask for the same. I want some user to be able to set a and b but not c - which is just a consequence of a and b.

Can't promise anything unfortunately but I'll try to have a look and see if I can make a PR.

@carstenbauer
Copy link

carstenbauer commented Mar 10, 2018

Just to be a little bit more precise, one can currently do

@with_kw type P
    a=1
    b=2
    c=a+3b
end

but a user will be able to overwrite the c=a+3b default. So I probably wouldn't call the macro @calc but maybe @fix or @nokw.

@with_kw type P
    a=1
    b=2
    @nokw c=a+3b
end

@mauro3
Copy link
Owner Author

mauro3 commented Mar 10, 2018

Tnx for your interest. With Julia 0.7 this could be done via properties. So, maybe it should be a 0.7 only feature?

@carstenbauer
Copy link

I'm not sure I understand, could you elaborate?

@mauro3
Copy link
Owner Author

mauro3 commented Mar 12, 2018

See JuliaLang/julia#25311

@carstenbauer
Copy link

I see that this could be used to hide the field c from the user. But wouldn't the @with_kw macro still need to know that it should exclude a field from being an allowed keyword in the constructor?

@mauro3
Copy link
Owner Author

mauro3 commented Mar 12, 2018

Yes, you'd need to mark a "field" with @calc or maybe now rather @prop.

@mauro3 mauro3 changed the title Calculated fields Properties May 16, 2018
@mauro3
Copy link
Owner Author

mauro3 commented May 16, 2018

This would be an ok syntax:

@with_kw struct A
    a
    b <= sin(a) + 5
end

i.e. the b is a property which is set via sin(a) + 5.

Also, properties would be read-only (also for mutable types) as it gets too complicated otherwise.

@mauro3
Copy link
Owner Author

mauro3 commented Aug 2, 2018

Although, even with properties, it might be nice to have fields which are calculated for performance reasons:

@with_kw struct A
    a
    @set b = really_expensive_function(a) + 5
end

@carstenbauer
Copy link

carstenbauer commented Aug 26, 2018

AFAIU we want two features:

  1. Be able to specify a property (not a real field) which when called by A.b will (on-the-fly) calculate it's value based on the current field values.
  2. A "calculated field", indicated by @set, @calc, or @nokw, which will be calculated when an instance is constructed based on the initial field values.

Correct?
I was asking for the latter type of field above.

@BeastyBlacksmith
Copy link

BeastyBlacksmith commented Feb 25, 2019

Case 1. would be doable by having

@with_kw struct A {F<:Function}
  a
  b
  c::F = (a::A)->(a.a+2a.b)
end
d = A(1,2)

and then Parameters.jl would need to extend getproperty such that d.c calls d.c(d)

@mauro3
Copy link
Owner Author

mauro3 commented Feb 25, 2019

But how would you know when one wants to store a function in a field vs when it should be used as a property-function? I think it would still need to be marked somehow, no?

@BeastyBlacksmith
Copy link

That depends on how convenient the syntax should be, the code above works right now as it is.
The user than has to declare that a Function is stored and add the type Parameter to the field, which IMO is not a bad thing, since it could be troublesome if Parameters.jl handles that for me, but then I have to account for the additional type parameter(s) for example when I define outer Constructors.
There is potential to make the construction of the anonymous function more readable like

@with_kw struct A {F<:Function}
  a
  b
  c::F = a+2b
end

The information that c should be a function could then be extracted from the type parameters and annotations.

@mauro3
Copy link
Owner Author

mauro3 commented Feb 26, 2019

But if I have, say a ODE problem which defaults to exponential growth:

@with_kw struct MyODE{F}
  tspan::Tuple{Float64,Float64} = (0.0, 1.0)
  objfn::F = x -> x
end

Now, objfn is not a property. Thus when doing myode.f I want the function to be returned and not myode.myode. Thus a field which is supposed to be a property needs to be marked somehow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants