Author Topic: Why the module restriction on struct functions?  (Read 5775 times)

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Why the module restriction on struct functions?
« on: June 22, 2018, 10:35:57 PM »
From the docs:

"struct-functions are defined in the same module as the struct (not necessary the same file!)"

Why this restriction?

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Why the module restriction on struct functions?
« Reply #1 on: June 23, 2018, 07:14:40 AM »
When a struct (Type) is defined in some module, it makes sense to also place the operations on
that type in the same module. This way, they are automatically shared between all users of that module.
Also it prevents ambiguity, that there can only be one function named x for that type. So there can
never be a A.x() in one module and B.x() in another.

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Re: Why the module restriction on struct functions?
« Reply #2 on: June 23, 2018, 10:26:23 PM »
There is a good reason why one would like to allow it though: it allows something similar to class extensions.

For example, let's day one module defines a "string" struct, which then is a char pointer + length.

The code has a String.find(char *) function, but is missing String.beginsWith and String.endsWith – which are really useful for some string handling.

Obviously you can define string_begins_with and string_ends_with, but it will be less nice to have:

Code: [Select]
if (string_begins_with(string, "foo")) {
  return string.toUpper();
}

When we could have:
Code: [Select]
if (string.beginsWith("foo")) {
  return string.toUpper();
}

At least of me it's common to build up a complementary library of additional functions on various types. In many languages we are forced to create stand-alone functions, because the class definition is closed. In languages like Objective-C – or in any language with UFCS, we have the option to let ourselves extend this definition (regardless wether we are struct-based or use OO).


bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Why the module restriction on struct functions?
« Reply #3 on: June 25, 2018, 07:46:55 AM »
I agree with your extension idea. When compiling C2 code, the compiler has a global view
of the code (ie not per-file, but the whole lib/binary). Thus there can never be multiple functions
with the same name. This also goes for struct functions. While it would be possible to let other
modules add functions to a struct; these functions would have to be globally unique.

This does not have to be a problem, but the error message might be an indication for the developer
to refactor some code..

lerno

  • Full Member
  • ***
  • Posts: 247
    • View Profile
Re: Why the module restriction on struct functions?
« Reply #4 on: June 25, 2018, 11:20:51 AM »
Not allowing duplicate names is a natural restriction. Allowing function overrides (basically a form of monkey patching) is interesting but not required.