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

Allow external functions and methods for classes #7175

Closed
vicuna opened this issue Mar 11, 2016 · 3 comments
Closed

Allow external functions and methods for classes #7175

vicuna opened this issue Mar 11, 2016 · 3 comments

Comments

@vicuna
Copy link

vicuna commented Mar 11, 2016

Original bug ID: 7175
Reporter: goswin
Status: acknowledged (set by @damiendoligez on 2017-02-27T15:50:52Z)
Resolution: open
Priority: normal
Severity: feature
Category: language features

Bug description

I have C stubs that take a class as argument and the class needs to call the C stubs. So they are both dependent on the other and one has to use recursive modules. That gets rather long winded and hard to read for something that should be trivial.

I want to write:

external stub : #c -> unit = "stub"
class c = object
initializer stub self
end

I need to write:

module rec E : sig
val stub : #C.c -> unit
end = struct
external stub : #C.c -> unit = "stub"
end and C : sig
class c : object end
end = struct
class c = object(self)
initializer E.stub self
end
end

It might be a good idea to allow both static functions and class methods as external. Maybe this syntax:

class foo bar = object
val external stub : int -> foo -> unit = "stub"
method external method_stub : unit = "method_stub"
end

In this case method_stub would take class foo as first argument implicitly while stub can have it at any place (or not at all).

@vicuna
Copy link
Author

vicuna commented Mar 11, 2016

Comment author: @lpw25

I don't think that:

class foo bar = object
val external stub : int -> foo -> unit = "stub"
end

makes sense, as an instance variable should be a value that can be overwritten in another copy of the object. I think a more sensible way to write what you want would be:

class foo bar = object
let module M = struct
external stub: int -> foo -> unit = "stub"
end in object
..
end

which is unfortunately not currently supported. In the future, I could imagine this becoming:

class foo bar = object
let external stub: int -> foo -> unit = "stub" in object
..
end

which is probably the most accurate way to write what you mean.

It is worth noting that, since class types are structural you can workaround your issue by just writing an explicit class type:

class type foo_t = object .. end

external stub : foo_t -> ...

class foo : foo_t = object ... end

@vicuna
Copy link
Author

vicuna commented Feb 28, 2017

Comment author: goswin

Writing out class types is tedious, more so with inheritance, even more so with recursive types. Writing out the types for Qt5 classes is nearly impossible.

@github-actions
Copy link

github-actions bot commented May 9, 2020

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

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

No branches or pull requests

1 participant