matthijscox / Blog

MIT License
0 stars 0 forks source link

pages/2023-01-31-julia-class/ #19

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Functional Noise

https://www.functionalnoise.com/pages/2023-01-31-julia-class/

CMoebus commented 1 year ago

I like this post very much. One get's some overview or metaknowledge. Next I read your trait.jl recommendation. Best, Claus

CMoebus commented 1 year ago

Your code is rather tricky :) If I understand it correctly, then 'myInt::Int' is an instance or object variable. Do you have an idea how to a class variable should be coded ? Best, Claus

CMoebus commented 1 year ago

Hi Mathijs, now another question concerning with 'Functions as custom properties'. I explored your code and generated an object with 'obj789 = MyModule2.MyClass(5)'. Then I looked at the properties of 'obj789' with 'propertynames(obj789)'. I was astonished that only one property appeared: ':myInt'. But in the code of your script all function names appeared as properties ':' in the 'if-elseif-end'. That's like magic. Best, Claus

matthijscox commented 1 year ago

Hi Claus, in the case of the 'Functions as custom properties', it will indeed only show the myInt property. You'll have to manually define the propertynames function if you want to show something else:

function Base.propertynames(obj::MyClass)
    return (:myInt, :print_int, :set_int!)
end

This should also enable tab-completion to work for those names.

likev commented 6 months ago

You are one of few who really know how to OOP in Julia

CMoebus commented 6 months ago

:), but usually I prefer FP-style with dispatch. C.

matthijscox commented 6 months ago

glad to know that this post remains informative :)