xsharp.eu • Mixing functions and methods
Page 1 of 1

Mixing functions and methods

Posted: Tue Nov 08, 2016 12:23 pm
by Otto
We removed the reference to the Win32 library from our assembly, and after some refactoring it all compiled again.
However, after some inspection we encountered a strange method:
METHOD DeleteFile(filename AS STRING)
RETURN DeleteFile(path + filename)
It compiled, but that was it.
Instead of calling the win32 function it called itself recursively...

So, who decided in the .NET world that you don't need to prefix a call to a method with thisself?

I know that this sounds like a "issue" in our situation, but I don't agree with that.

I think that it is more fundamental: in a language that supports "functions" besides method calls with use of automatic selection of the 'context', the compiler should throw an error or warning that says that there are multiple options, and that the writer should supply the right context.

Like when the compiler gives an error if there are more than one namespace that contain a class and that the compiler needs directions which should be used here.

Or is this such a niche situation that we shouldn't bother?

Mixing functions and methods

Posted: Tue Nov 08, 2016 10:24 pm
by robert
Otto,

How can the compiler "know" that you are not calling yourself recursively on purpose ?

And w.r.t. writing code with or without self/this, that is a matter of style.
I know many companies that prefer not to use this/SELF when not necessary.

Robert

Mixing functions and methods

Posted: Wed Nov 09, 2016 12:38 am
by Chris
Hi Otto,

Personally, I'd much prefer it in all languages (VO, x#, c#, vulcan etc) that the "SELF" or "this" keyword is always mandatory when accessing instances member, as it makes code much better to read for my own taste and less error prone. But, as usual, this is a personal preference and as Robert said many others (I am sure including many people in the forums here!) prefer it otherwise, so we need to support both ways.

But, in any case, it's in our todo list to make your sample behave as you'd expect at runtime. In VO/Vulcan, the DeleteFile() call in the RETURN statement in your code

METHOD DeleteFile(filename AS STRING)
RETURN DeleteFile(path + filename)

resolves to the DeleteFile() function (if there exists one such function), so for VO/Vulcan compatibility, we will adjust also the x# compiler to search for a same named function first, before resolving the call to the same-named method. That will happen for the vulcan dialect, not sure if it should be done for the core dialect as well.

Chris

Mixing functions and methods

Posted: Wed Nov 09, 2016 7:02 am
by wriedmann
Hi Chris, hi Robert,

personally I would prefer that a self or this prefix would be required. In C# this is not neccessary as there are no functions, but for X# it would be better to read.

(IMHO X# code is much more readable than C# code).

Wolfgang

Mixing functions and methods

Posted: Wed Nov 09, 2016 8:12 am
by Otto
Robert wrote:Otto,

How can the compiler "know" that you are not calling yourself recursively on purpose ?
By making it explicit when the programmer meant to call the method and when the function.

The discussion is, I think, 'fundamental' about mixture of functions and methods and the change from VO (where 'SELF:' is mandatory) to .NET (where it isn't and even Resharper gives you a warning(!) when you use 'this.'). We have two cultures here that are mixing.
I see a few options which could have 'prevented' this (unittests of course :) )
1. when there is a function and a method with the same name, the compiler could raise a warning that there are two possible solutions, 'if you don't specify which you want, the compiler will take the function'.
2. Vulcan.net or any other language that support 'functions' MUST use the SELF: prefix when the programmer wants to call a method.
And w.r.t. writing code with or without self/this, that is a matter of style.
I know many companies that prefer not to use this/SELF when not necessary.
In c# I also follow the preferred standard. We even prefer 'Resharper green'.
For Vulcan this situation bothers me. One can even hijack a method by creating a function with the same name in the same namespace, and one will not know it at compiletime...

Mixing functions and methods

Posted: Wed Nov 09, 2016 8:24 am
by Otto
wriedmann wrote:Hi Chris, hi Robert,

personally I would prefer that a self or this prefix would be required. In C# this is not neccessary as there are no functions, but for X# it would be better to read.
that's what I meant. it took you a lot less words than me :)

In the core language or other dialects, when there are no functions, it could be optional, like in c#.

Mixing functions and methods

Posted: Wed Nov 09, 2016 8:51 am
by robert
Otto,

Like Chris said: we are considering to add a warning here. Not for the next build but it will come.
Otto wrote: 1. when there is a function and a method with the same name, the compiler could raise a warning that there are two possible solutions, 'if you don't specify which you want, the compiler will take the function'.
2. Vulcan.net or any other language that support 'functions' MUST use the SELF: prefix when the programmer wants to call a method.
But in this case, since you have deleted the DeleteFile() function, there is no ambiguity: DeleteFile() is only a method. What should the compiler do? Allow the code to compile without SELF: ?


Robert

Mixing functions and methods

Posted: Wed Nov 09, 2016 9:21 am
by Otto
Before the deletion of the (library reference containing the) function the compiler could have raised a warning because of the ambiguity.

Now (after the deletion) it should raise a warning because I don't use the SELF keyword here (because functions are part of the language dialect).