Allowing the object.method(args...) syntax as an alias for method(object, args ...)

gianmariomanca
Sat, 29 May 2021 17:09:54 +0000
Internals & Design

I started exploring Julia very recently and I like it. I’ve been chatting about it with colleagues.

Many people coming from OOP languages (Python…) like the syntax object.method(a,b,c…).
Since you can get a preview of the available methods acting on object with tab press.
Would it be possible to have that in Julia (I’m assuming it’s not already there) to improve usability?

Let say I define a IIR filter struct

mutable struct IIRfilter
	state;
	alpha;
	IIRfilter(state,alpha) = 0<=alpha<=1 ?  new(state,alpha) : error("alpha must between 0 and 1")
	IIRfilter(alpha) = 0<=alpha<=1 ?  new(NaN,alpha) : error("alpha must be between 0 and 1")
end

and a method

function filter(filter::IIRfilter, x)
	if isnan(filter.state)
		filter.state = x
	else
		filter.state = filter.alpha*filter.state + (1-filter.alpha)*x;
	end
	return filter.state
end

I can now use my filter

begin
	N = 3000
	f = IIRfilter(0.9)
	Random.seed!(1)
	xv = randn(N,1);
	xf = zeros(N,1);
end;

begin
	for i = collect(1:N)
		xf[i] = filter(f,xv[i])
	end
end

It would be nice to be able to start typing

f.

the press a tab and get

f.state
f.alpha
f.filter(...)

as options instead of just

f.state
f.alpha

basically get a preview of all available methods for the IIRfilter type (where IIRfilter is the first argument of the method if we are thinking about standard OOP, or maybe a more general solution that is closer in spirit with multiple dispatch?)

Is there a reason why we might not want this or we would actually want to actively discourage it?

217 posts - 41 participants