charly-lang / charly

🐈 The Charly Programming Language | Written by @KCreate
https://charly-lang.github.io/charly/
MIT License
198 stars 10 forks source link

Namespaces for native extensions #115

Open KCreate opened 7 years ago

KCreate commented 7 years ago

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.

ghost commented 7 years ago

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

KCreate commented 7 years ago

What would you propose?

ghost commented 7 years ago

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

KCreate commented 7 years ago

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?

ghost commented 7 years ago

I think that would work great.