Extensible mathematical generic word #2423
Replies: 4 comments 4 replies
-
I think this would be a great improvement. Not impossible! In our current mathematical tree, we have "coercer" quotations that convert to make sure the arguments are both a specific type. Perhaps in the "else" branch which right now throws, we allow overriding using multi-methods on both argument types? |
Beta Was this translation helpful? Give feedback.
-
I wonder if providing means of adding coercer and types to the tree would be sufficient. Or provide the dispatch when the same type in the tree and error otherwise. That could work with the current method.
… On Jan 18, 2021, at 6:38 PM, kusumotonorio ***@***.***> wrote:
I will try to think of ways to make this happen. I hope you will give it a try too.
Perhaps it could be extended without multi-method support?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Beta Was this translation helpful? Give feedback.
-
Apparently, it is working as expected. At least in the following example. IN: scratchpad
USE: mm9
MGENERIC: my-plus ( a b -- c ) mathematical
USING: math.private ;
MM: my-plus ( a:fixnum b: fixnum -- c ) fixnum+ ;
MM: my-plus ( a: float b: float -- c ) float+ ;
MM: my-plus ( a: string b: string -- c ) append ;
1 2.0 my-plus
"1 + 2 = " "3.0" my-plus
Loading resource:work/mm9/mm9.factor
Loading resource:basis/shuffle/shuffle.factor
--- Data stack:
3.0
"1 + 2 = 3.0"
IN: scratchpad |
Beta Was this translation helpful? Give feedback.
-
I've added a few more. IN: scratchpad
USE: mm9
MGENERIC: my-plus ( a b -- c ) mathematical
USING: math.private ;
MM: my-plus ( a:fixnum b: fixnum -- c ) fixnum+ ;
MM: my-plus ( a: float b: float -- c ) float+ ;
MM: my-plus ( a: string b: string -- c ) append ;
USE: unicode
MM: my-plus ( a: character b: string -- c ) swap 1array >string prepend ;
1 2.0 my-plus
"1 + 2 = " "3.0" my-plus
CHAR: A "BC" my-plus
Loading resource:work/mm9/mm9.factor
Loading resource:basis/shuffle/shuffle.factor
--- Data stack:
3.0
"1 + 2 = 3.0"
"ABC"
IN: scratchpad |
Beta Was this translation helpful? Give feedback.
-
This is a brainstorming session. :-)
One of the reasons for the desire to implement multiple methods in Factor is to achieve operator overloading. For example, the first post here (#2364) , which I am now writing frequently, and I can see that being a request here (#2261) as well.
In a multi-method implementation such as the prototype I just presented, it is theoretically possible to write words such as
+
to be open to string concatenation, multi-dimensional array addition, etc., but if such operators were created as mathematical generic words, non-numeric parameters would not be available, and thus the implementation would not be able to use them, so it would not be usable for string concatenation in reality. In other words, the situation would be the same as it is now.But...
If a mathematical generic word with multi-method support works by switching the dispatch strategy to a normal multi-method one, instead of raising an exception when given a non-numeric parameter load, then we can keep the advantages of the mathematical generic word and make it extensible Is that right? I don't think it's impossible.
Beta Was this translation helpful? Give feedback.
All reactions