Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support usage of function types without aliasing #97

Open
Dandigit opened this issue Jan 14, 2019 · 4 comments
Open

Support usage of function types without aliasing #97

Dandigit opened this issue Jan 14, 2019 · 4 comments

Comments

@Dandigit
Copy link
Contributor

C2 currently supports function pointer types through aliasing:

type HandlerFunc func bool(char*);

func void passHello(HandlerFunc handler) {
    handler("hello");
}

This is great, and it's a big improvement over C. However, currently, it's not possible to do the following:

func void passHello(func bool(char*) handler) {
    handler("hello");
}

Just using the function type without aliasing is not currently supported. I believe that it should be added for consistency's sake.

@lerno
Copy link
Collaborator

lerno commented Jan 14, 2019

Yeah, I think that makes sense too although might be difficult to read as a return type?

@Dandigit
Copy link
Contributor Author

Agreed. It'd be considered best practice to alias the type for better readability, however if you are just using it trivially, you have the freedom not to.

@lerno
Copy link
Collaborator

lerno commented Jan 14, 2019

If it's to be in the lang then it needs to parse correctly, so I wonder if that might be an issue. Consider:

func func bool(char*)* return_a_hello_function()

It's a bit better with mandatory ():

func (func bool(char*))* return_a_hello_function()

Still... I'm on the fence about this. Not using an alias does reduce readability quite a bit. It might be good to actually "force" the alias.

@bvdberg
Copy link
Member

bvdberg commented Jan 16, 2019

There are a few cases where function pointer declarations are needed:

  • function argument (your example)
  • variable/struct member
  • return type

What I don't like in C is that you have to change the prototype in several places if you
want to change the prototype of the function. This is especially nasty in the case of
function argument and return type. For the case of the struct member, you often
only specify the prototype in 1 place, like: (C-code)

typedef struct Mystruct {
   int (*handler)(char*);
}

In this case, the C2 solution requires more typing because it becomes:

type Handler func i32(char*);
type Mystruct struct {
    Handler handler;
}

This get worse if your code uses a lot of different function pointers in a struct.

What I also don't like about C is the syntax for function pointers. The name is hidden in the
middle instead of type name.

So I understand your argument and agree, but I think the alternative is worse in my opinion..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants