charly icon indicating copy to clipboard operation
charly copied to clipboard

Namespaces for native extensions

Open KCreate opened this issue 8 years ago • 5 comments
trafficstars

You should be able to put methods added via native extensions into a namespace.

charly_namespace "mynamespace" do
  charly_api "mymethod" do
    TString.new "Hello world"
  end
end

Access inside charly:

const mynamespace = __internal__namespace("mynamespace")
mynamespace.mymethod() # => "Hello world"

The interpreter will automatically create bindings to all these methods.

KCreate avatar Dec 12 '16 13:12 KCreate

@KCreate I would like to make them easier to access. An extra variable for that is to much. Maybe a special shortcut for that?

ghost avatar Apr 22 '17 07:04 ghost

What would you propose?

KCreate avatar Apr 22 '17 10:04 KCreate

Maybe that a Namespace has the same syntax as accessing an class or module in Crystal?

ghost avatar Apr 22 '17 10:04 ghost

So charly_namespace should automatically inject itself into the program? That would cause a namespace conflict pretty fast. Having the ability to put each namespace and method into a user-defined variable makes more sense imho.

What is possible however is, that __internal__namespace could return a class instead of an object, allowing multiple instances of internals namespaces.

charly_namespace "mynamespace" do
  charly_api "constructor" do
    puts "initializing"
  end

  charly_api "greet" do
    TString.new "Hello world"
  end

  charly_api "goodbye" do
    TString.new "Bye world"
  end
end
const MyNamespace = __internal__namespace("mynamespace")
const MyObject = MyNamespace() // prints "initializing"
MyObject.greet() // => "Hello world"
MyObject.goodbye() // => "Bye world"

But then we'd need to add some methods which can operate on the classes instance and static variables.

What are your thoughts on this?

KCreate avatar Apr 22 '17 10:04 KCreate

I think that would work great.

ghost avatar Apr 22 '17 11:04 ghost