fbpx

Mixing functions and methods

  • Otto Christiaanse
  • Otto Christiaanse's Avatar Topic Author
  • Offline
More
2 years 2 months ago #1 by Otto Christiaanse
Mixing functions and methods was created by Otto Christiaanse
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 this\self?

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?

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago #2 by Robert van der Hulst
Replied by Robert van der Hulst on topic Mixing functions and methods
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

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago #3 by Chris Pyrgas
Replied by Chris Pyrgas on topic Mixing functions and methods
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

XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago #4 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Mixing functions and methods
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

Please Log in or Create an account to join the conversation.

  • Otto Christiaanse
  • Otto Christiaanse's Avatar Topic Author
  • Offline
More
2 years 2 months ago #5 by Otto Christiaanse
Replied by Otto Christiaanse on topic Mixing functions and methods

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...

Please Log in or Create an account to join the conversation.

  • Otto Christiaanse
  • Otto Christiaanse's Avatar Topic Author
  • Offline
More
2 years 2 months ago #6 by Otto Christiaanse
Replied by Otto Christiaanse on topic Mixing functions and methods

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#.

Please Log in or Create an account to join the conversation.

More
2 years 2 months ago - 2 years 2 months ago #7 by Robert van der Hulst
Replied by Robert van der Hulst on topic Mixing functions and methods
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

XSharp Development Team
The Netherlands
This email address is being protected from spambots. You need JavaScript enabled to view it.
Last edit: 2 years 2 months ago by Robert van der Hulst.

Please Log in or Create an account to join the conversation.

  • Otto Christiaanse
  • Otto Christiaanse's Avatar Topic Author
  • Offline
More
2 years 2 months ago - 2 years 2 months ago #8 by Otto Christiaanse
Replied by Otto Christiaanse on topic Mixing functions and methods
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).
Last edit: 2 years 2 months ago by Otto Christiaanse.

Please Log in or Create an account to join the conversation.