SenasOzys has quit [Remote host closed the connection]
SenasOzys has joined #ponylang
SenasOzys has quit [Remote host closed the connection]
SenasOzys has joined #ponylang
nisanharamati has joined #ponylang
khan has joined #ponylang
SenasOzys has quit [Remote host closed the connection]
SenasOzys has joined #ponylang
aturley has joined #ponylang
khan has quit [Quit: khan]
khan has joined #ponylang
aturley has quit [Client Quit]
khan has quit [Quit: khan]
khan has joined #ponylang
khan has quit [Client Quit]
khan has joined #ponylang
SenasOzys has quit [Ping timeout: 240 seconds]
khan has quit [Client Quit]
khan has joined #ponylang
SenasOzys has joined #ponylang
inoas has quit [Ping timeout: 256 seconds]
brainpro1 has quit [Read error: Connection reset by peer]
brainpro1 has joined #ponylang
khan has quit [Read error: Connection reset by peer]
khan has joined #ponylang
khan has quit [Client Quit]
khan has joined #ponylang
khan has quit [Quit: khan]
khan has joined #ponylang
khan has quit [Client Quit]
khan has joined #ponylang
acarrico has quit [Ping timeout: 240 seconds]
_whitelogger has joined #ponylang
_andre has quit [Quit: leaving]
<vassilvk>
I am trying to wrap my mind around primitives and I have a question. If a primitive has no fields and there can only be one instance of a user-defined primitive, what is the purpose of a user-defined primitive constructor?
<slfritchie>
vassilvk: If I understand your question correctly (that you mean "constructor" to be the "apply()" function) is that Pony convention of calling `PrimitiveName(args)` is shorthand for `PrimitiveName.apply(args)`
<vassilvk>
slfritchie: Actually by "constructor" I meant a constructor defined with the "new" keyword - i.e. "new create() => None".
<slfritchie>
I'm trying to think of an instance of `PrimitiveName.create()` but failing ... do you have an example in mind?
<vassilvk>
See that's the thing, I am trying to figure out what is the applicability of such a constructor. I tried it out -- I can define a parameterless constructor on a user-defined primitive type. Not only that, but my tests show that the constructor is called every time a value is assigned to that primitive.
<vassilvk>
As for an example of a primitive constructor, see the definition of builtin Bool, which has some really interesting constructor that looks like this: "new create(from: Bool) => from". This looks like a trick to me and I don't think it is at all applicable to user-defined primitives, because I see no way to pass in a value of user-defined value of type MyPrimitive to the constructor of type MyPrimitive.
<vassilvk>
So bottom line is, I was wondering, why are user-defined primitives allowed to have constructors to begin with, if they cannot be "constructed", so to speak. And if the thinking is that although they behave like singletons they are still constructed once, then why isn't the constructor called only once? Plus, they already have a singleton constructor so to speak, in the form of the initialization function _init. Just
<vassilvk>
wondering if I'm missing something...
<slfritchie>
"[...] I see no way to pass [...] to the constructor of type MyPrimitive". But a primitive isn't an an object, so I'm puzzled by what you mean by "constructor" in that sentence, sorry. (for reference: https://tutorial.ponylang.org/types/primitives.html)
<vassilvk>
See first paragraph below the numbered list at the top of the document you linked to: "...if your code calls a constructor on a primitive type...".
<vassilvk>
Give me a sec -- I'll put together an example
SenasOzys has quit [Read error: Connection reset by peer]
<vassilvk>
Also, I'm not sure I understand what you mean by "primitives are not objects". No types are objects. Maybe you mean they are not classes? But they are "similar to classes" as the doc you sent to me points out. And they clearly have constructors, just like classes do.
<slfritchie>
Yes, sorry, I meant "not classes".
<vassilvk>
Right, but they are "similar to classes" (first sentence from that doc you sent to me).
<slfritchie>
Perhaps "similar" isn't a great choice of words.
<vassilvk>
Yes, thank you -- I understand all that.
<vassilvk>
My question is, what is the purpose of a constructor if there is no state to be initialized by that constructor.
<slfritchie>
There isn't any construction happening when `MyPrimitive.create()` is called because there's no object associated with it.
<vassilvk>
The language allows you to declare a constructor on a primitive using the "new" keyword.
<slfritchie>
I've written Erlang code for over 15 years and Pony for about 1/2 year, so my object oriented mind is not well developed.
<vassilvk>
Lol :)
<slfritchie>
You can try s/new create/fun create/ on that example, but the compiler won't like it
<vassilvk>
Exactly -- it complains that there is no constructor defined.
<slfritchie>
Hmmm, here's where my lack of OO vocabulary is a problem.
<slfritchie>
There's definitely type information associated with a primitive that has been created by `MyPrimitive.create()`, regardless of whether the `create` is explicit or implicit.
<slfritchie>
But the value of the type is a constant. x is equal to y is equal to w is equal to z.
<slfritchie>
Kind of like a tag refcap: test for equality is about all you can do with a tag
<slfritchie>
Usually it's the type info that your code will care about, for use as an enumeration type
<slfritchie>
The value info ... who cares? (I think)
<slfritchie>
Hrm. Not sure if that's helpful or not.
<vassilvk>
Yes, the "value" of a primitive is a constant (or rather an immutable instance) -- all the "is" tests prove it. But that still doesn't help me understand what do I need a primitive constructor for (but the language provides for defining one and even demands for one to be there when performing primitive assignments sich as the one in my gist). And most of all, why is the constructor called every time I acquire that same
<vassilvk>
instance.
jbk_ has joined #ponylang
jbk has quit [Ping timeout: 264 seconds]
<slfritchie>
Perhaps you want a side-effect (e.g. C FFI call) when "creating" a new primitive instance?
<vassilvk>
But you are not creating a new primitive instance -- there is only one.
<vassilvk>
Of course - you absolutely don't need one. Most primitives I've seen so far have no constructors.
<slfritchie>
Perhaps a notion of preserving symmetry between object instantiation and primitive looks-like-instantiation is the reason? And/or it makes some compiler task easier? I don't know.
<vassilvk>
Umm, I think that's not it -- I mean, the designers of the language did not attempt to preseve symmetry between classes and actors, why do it for primitives and classes.
<vassilvk>
I have another theory...
<vassilvk>
The builtin primitives - they are the reason to have constructors. They actually need constructors as evident by the Bool primitive.
atk has quit [Quit: Well this is unexpected.]
<vassilvk>
They need constructors, because they have state.
<vassilvk>
So, I think the designers of the language *had* to maintain the presence of constructors in user-defined primitives, because they had to have constructors for the builtin primitives.
<vassilvk>
Just a theory, which I'm trying to prove wrong by finding a good use case where constructors make sense for user-defined primitives.
<slfritchie>
Perhaps if you really desired to keep stateless primitives and stateful "machine word" thingies as similar as possible? Could be.
<vassilvk>
Or maybe they are based on the same underlying fundamental structures? Could be..
<vassilvk>
Anyways, thanks for trying to hash this out with me, @slfritchie
<slfritchie>
Thanks for suffering through my questions. ^_^