fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Mixing functions and methods

Mixing functions and methods 2 years 7 months ago #578

  Otto's Avatar Topic Author Otto Offline Posts: 141
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.

Mixing functions and methods 2 years 7 months ago #579

  robert's Avatar robert Offline Posts: 969
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.

Mixing functions and methods 2 years 7 months ago #581

  Chris's Avatar Chris Offline Posts: 1137
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.

Mixing functions and methods 2 years 7 months ago #583

  wriedmann's Avatar wriedmann Offline Posts: 1545
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Mixing functions and methods 2 years 7 months ago #585

  Otto's Avatar Topic Author Otto Offline Posts: 141

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.

Mixing functions and methods 2 years 7 months ago #587

  Otto's Avatar Topic Author Otto Offline Posts: 141

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.

Mixing functions and methods 2 years 7 months ago #593

  robert's Avatar robert Offline Posts: 969
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.

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

Last edit: by robert.

Mixing functions and methods 2 years 7 months ago #598

  Otto's Avatar Topic Author Otto Offline Posts: 141
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).

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

Last edit: by Otto.
  • Page:
  • 1