Author Topic: Struct Functions - part 2  (Read 12751 times)

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Struct Functions - part 2
« on: November 28, 2016, 09:08:33 AM »
I've been implementing some code using struct functions and must say I'm completely happy with the calling side. On the receiving side (the function itself), a patterns is appearing that might be avoided. Currently struct functions look like this:

Code: [Select]
func void myStruct_init(MyStruct* this) {
   this.a = 1;
   this.b = 2;
   // ..
}

This is just a small piece of code, but the thing that starts to itch in bigger functions is the frequent appearance of the this.
To make it feel more like (for example) a C++ class, we could add the struct members in the function namespace. Then all the
this keywords could go (unless ambiguous). This would have some consequences:

  • name duplicates between module and struct members is allowed (since can distinguish with module.a and this.a)
  • name duplication between struct members and function arguments is not allowed (cannot distinguish somehow)

I don't think since is a problem, since it's not a problem in object-oriented languages as well. Any feedback is welcome,

Cheers,
Bas

jerry

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Struct Functions - part 2
« Reply #1 on: December 02, 2016, 10:34:01 AM »
In my opinion this is not a problem. Some other syntactical quirck which looks redundant is the myStruct_<func> naming. I'm not saying we could do it the same way as Golang does but it removes the redundancy. Like this (in golang)

Code: [Select]
type MyStruct struct {
  a int
  b int
}

func (v *MyStruct) init() {
 v.a = 1
 v.b = 2
}

This is written here: https://golang.org/ref/spec#Selectors

Its just an idea to minimize the amount of typing.

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Struct Functions - part 2
« Reply #2 on: December 09, 2016, 08:47:07 AM »
One of the basic design choices in C2 is not to do symbol mangling. So for a function, it should be quite
clear what the exact symbol will look like. In C2 this is currently 'module_symbol', which is not real mangling,
but just concat-ing module name and symbol name.

I agree that the syntax of struct functions is probably not the best it can be, but I'm still puzzling to find
a better one.

One idea that came up during usage is to add the struct members to the 'namespace' of the function. So
you don't have to do 'this.member' but can simply do 'member'. This leads to the question whether a user
should have to type the first argument and whether it's always 'this'. I always prefer simplicity when in doubt
and I don't think that saving some keystrokes should be a goal. A simple language with a well readable syntax
is a higher goal imho.

Another idea would be to make the fact that a function is a struct function more explicit. In that sense your
syntax proposal could be used...




kyle

  • Newbie
  • *
  • Posts: 48
    • View Profile
Re: Struct Functions - part 2
« Reply #3 on: December 22, 2016, 02:32:41 AM »
Is there a "Struct Functions - part 1"?

bas

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Struct Functions - part 2
« Reply #4 on: January 02, 2017, 08:19:35 PM »
In C2 'this' is not a keyword. It's just the name of the first argument used in struct functions. You are allowed
to use any name you want. The question was whether to let developers skip the 'this.member' and just type 'member'
inside struct functions. The compiler would then convert 'member' to '<name_of_arg0>.member'.
This way struct functions are a lot more similar to C++ class functions (where you are also allowed to use 'this->member'),
but can also just use 'member'.