0

I am having trouble finding out how to create a function that creates and instance of an object where the name of the variable that holds the function is specified in the parameter of the function. I am writing an assignment where I do not know how many instances of the object that is to be created. I know how to call the constructor in a function, but I do not know how to give each instance a unique name?

let test() = class
    let variable = 1
end

let createInstance (name : string) =
    let name = new Test()

This is just a test example. This is what I have tried, but the function does not parse the string "name" to be the name of the object instance variable.

Thanks!

Emil
  • 31
  • 3
  • I don't think this is possible. – Foole Jan 12 '16 at 09:32
  • 5
    It isn't possible. The question is, what problem are you trying to solve that you think would actually benefit from doing this? If you describe what behaviour you're trying to achieve in more detail, maybe we can suggest an alternative that does work. – TheInnerLight Jan 12 '16 at 09:40
  • I think you are doing this the wrong way, but rmaybe reflection can do this? – John Palmer Jan 12 '16 at 10:10
  • Is it the name that has to be unique? Or can an INTERNAL property of the class be unique? Or can there be an EXTERNAL dictionary holding the unique name as the key and the instance as the value? – Guy Coder Jan 12 '16 at 12:33
  • 2
    Most likely, this question wasn't intended to mean what it seems to mean. In the first bit, are you trying to write a class or a function? The above syntax is an invalid mix of both. Even if it were possible to operate on variable names, it sounds like a bad idea. Those names are nothing but identifiers local to the scope they're used in; nobody would expect them to be processed by the program. – Vandroiy Jan 12 '16 at 12:41
  • why would you do this? you could use a Map (persistent hashtable) to map the string to the instance of an object. This is a little workaround but not that big. if you are finde with the name being an integer, then you could use an array of instance and simple just returning the index of that instance. The first solution would you need to make a carry instance of that Map, for multiple classtypes use Map as map type. then simple just add an item and delete it as you go along. – kam Jul 03 '19 at 19:19

0 Answers0