質問編集履歴
9
リンクを見やすくした。
title
CHANGED
File without changes
|
body
CHANGED
@@ -302,5 +302,6 @@
|
|
302
302
|
|
303
303
|
**追記3**:
|
304
304
|
|
305
|
-
文字数上限に達してしまったので別の質問をたてて、続きを書きま
|
305
|
+
文字数上限に達してしまったので別の質問をたてて、続きを書きます。リンクはこちらです。
|
306
|
+
|
306
|
-
https://teratail.com/questions/248408
|
307
|
+
* [C++ で多重ディスパッチをする方法と Julia 2](https://teratail.com/questions/248408)
|
8
追記3。
title
CHANGED
File without changes
|
body
CHANGED
@@ -296,4 +296,11 @@
|
|
296
296
|
Animal cat = Cat();
|
297
297
|
```
|
298
298
|
|
299
|
-
がどういう意味か分かりません。 `Animal ≠ Dog`, `Animal ≠ Cat` ではないということでしょうか?
|
299
|
+
がどういう意味か分かりません。 `Animal ≠ Dog`, `Animal ≠ Cat` ではないということでしょうか?
|
300
|
+
|
301
|
+
---
|
302
|
+
|
303
|
+
**追記3**:
|
304
|
+
|
305
|
+
文字数上限に達してしまったので別の質問をたてて、続きを書きました。
|
306
|
+
https://teratail.com/questions/248408
|
7
追記2。
title
CHANGED
File without changes
|
body
CHANGED
@@ -296,4 +296,4 @@
|
|
296
296
|
Animal cat = Cat();
|
297
297
|
```
|
298
298
|
|
299
|
-
がどういう意味か分かりません。
|
299
|
+
がどういう意味か分かりません。 `Animal ≠ Dog`, `Animal ≠ Cat` ではないということでしょうか?
|
6
cat を追加。
title
CHANGED
File without changes
|
body
CHANGED
@@ -293,6 +293,7 @@
|
|
293
293
|
|
294
294
|
```cpp
|
295
295
|
Animal dog = Dog();
|
296
|
+
Animal cat = Cat();
|
296
297
|
```
|
297
298
|
|
298
299
|
がどういう意味か分かりません。
|
5
日本語として修正。
title
CHANGED
File without changes
|
body
CHANGED
@@ -289,7 +289,7 @@
|
|
289
289
|
|
290
290
|
**追記2**:
|
291
291
|
|
292
|
-
どこが分かってないのか分かってきた気がします。Julia では `abstract type` はインスタンス化できないので、`Animal` 型のオブジェクトは存在し
|
292
|
+
どこが分かってないのか分かってきた気がします。Julia では `abstract type` はインスタンス化できないので、`Animal` 型のオブジェクトは存在しません。私が Julia 脳になっているので C++ での
|
293
293
|
|
294
294
|
```cpp
|
295
295
|
Animal dog = Dog();
|
4
追記2。
title
CHANGED
File without changes
|
body
CHANGED
@@ -283,4 +283,16 @@
|
|
283
283
|
}
|
284
284
|
```
|
285
285
|
|
286
|
-
にすべきでした。
|
286
|
+
にすべきでした。
|
287
|
+
|
288
|
+
---
|
289
|
+
|
290
|
+
**追記2**:
|
291
|
+
|
292
|
+
どこが分かってないのか分かってきた気がします。Julia では `abstract type` はインスタンス化できないので、`Animal` 型のオブジェクトは存在しないので、私が Julia 脳になっているので C++ での
|
293
|
+
|
294
|
+
```cpp
|
295
|
+
Animal dog = Dog();
|
296
|
+
```
|
297
|
+
|
298
|
+
がどういう意味か分かりません。
|
3
追記1。
title
CHANGED
File without changes
|
body
CHANGED
@@ -252,4 +252,35 @@
|
|
252
252
|
This box has a cat.
|
253
253
|
```
|
254
254
|
|
255
|
-
私の理解のうち何が間違っているのでしょうか?
|
255
|
+
私の理解のうち何が間違っているのでしょうか?
|
256
|
+
|
257
|
+
---
|
258
|
+
|
259
|
+
**追記1**:
|
260
|
+
|
261
|
+
```julia
|
262
|
+
function content(::Box{<:Animal})
|
263
|
+
println("This box has an animal.")
|
264
|
+
end
|
265
|
+
```
|
266
|
+
|
267
|
+
```cpp
|
268
|
+
template <typename T>
|
269
|
+
auto content(Box<T> box) {
|
270
|
+
cout << "This box has an animal." << endl;
|
271
|
+
}
|
272
|
+
```
|
273
|
+
|
274
|
+
の部分の Julia の `<:Animal` の部分が C++ だと表現できてないので C++20 の機能を使って
|
275
|
+
|
276
|
+
```cpp
|
277
|
+
#include <concepts>
|
278
|
+
|
279
|
+
template <typename T>
|
280
|
+
requires std::derived_from<T, Animal>
|
281
|
+
auto content(Box<T> box) {
|
282
|
+
cout << "This box has an animal." << endl;
|
283
|
+
}
|
284
|
+
```
|
285
|
+
|
286
|
+
にすべきでした。
|
2
再調整
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,26 +1,8 @@
|
|
1
1
|
Julia では(例えば[ここ](https://docs.julialang.org/en/v1/manual/methods/)で) multiple dispatch / 多重ディスパッチできるよ!と言われ、C++ ではできないよ!と言われていますが、
|
2
2
|
|
3
3
|
>
|
4
|
-
The choice of which method to execute when a function is applied is called *dispatch*. Julia allows
|
5
|
-
|
6
|
-
given, and on the types of all of the function's arguments. This is different than traditional
|
7
|
-
object-oriented languages, where dispatch occurs based only on the first argument, which often
|
8
|
-
has a special argument syntax, and is sometimes implied rather than explicitly written as an argument.
|
9
|
-
[1] Using all of a function's arguments to choose which method should be invoked, rather than
|
10
|
-
just the first, is known as [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch).
|
11
|
-
Multiple dispatch is particularly useful for mathematical code, where it makes little sense to
|
12
|
-
artificially deem the operations to "belong" to one argument more than any of the others: does
|
13
|
-
the addition operation in `x + y` belong to `x` any more than it does to `y`? The implementation
|
14
|
-
of a mathematical operator generally depends on the types of all of its arguments. Even beyond
|
15
|
-
mathematical operations, however, multiple dispatch ends up being a powerful and convenient paradigm
|
16
|
-
for structuring and organizing programs.
|
17
|
-
>>
|
18
|
-
[1]:
|
19
|
-
In C++ or Java, for example, in a method call like `obj.meth(arg1,arg2)`, the object obj "receives"
|
20
|
-
the method call and is implicitly passed to the method via the `this` keyword, rather than as
|
21
|
-
an explicit method argument. When the current `this` object is the receiver of a method call,
|
22
|
-
it can be omitted altogether, writing just `meth(arg1,arg2)`, with `this` implied as the receiving
|
23
|
-
object.
|
4
|
+
The choice of which method to execute when a function is applied is called *dispatch*. Julia allows the dispatch process to choose which of a function's methods to call based on the number of arguments given, and on the types of all of the function's arguments. This is different than traditional object-oriented languages, where dispatch occurs based only on the first argument, which often has a special argument syntax, and is sometimes implied rather than explicitly written as an argument. [1] Using all of a function's arguments to choose which method should be invoked, rather than just the first, is known as [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch). Multiple dispatch is particularly useful for mathematical code, where it makes little sense to artificially deem the operations to "belong" to one argument more than any of the others: does the addition operation in `x + y` belong to `x` any more than it does to `y`? The implementation of a mathematical operator generally depends on the types of all of its arguments. Even beyond mathematical operations, however, multiple dispatch ends up being a powerful and convenient paradigm for structuring and organizing programs.
|
5
|
+
>> [1]: In C++ or Java, for example, in a method call like `obj.meth(arg1,arg2)`, the object obj "receives" the method call and is implicitly passed to the method via the `this` keyword, rather than as an explicit method argument. When the current `this` object is the receiver of a method call, it can be omitted altogether, writing just `meth(arg1,arg2)`, with `this` implied as the receiving object.
|
24
6
|
|
25
7
|
|
26
8
|
自分が思っていた多重ディスパッチの定義だと C++ でも動くようなので、何かの理解を間違えていると思うのですがどこを間違えているのか分かりません。私は3つの polymorphism / ポリモルフィズム(ad hoc / アドホック, subtype / 部分型, parametric / パラメトリック)を Julia と C++ でそれぞれ書くならば、以下のことが行えることと理解しています。
|
1
引用部分を見やすく変更した。
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,10 +1,26 @@
|
|
1
1
|
Julia では(例えば[ここ](https://docs.julialang.org/en/v1/manual/methods/)で) multiple dispatch / 多重ディスパッチできるよ!と言われ、C++ ではできないよ!と言われていますが、
|
2
2
|
|
3
3
|
>
|
4
|
-
The choice of which method to execute when a function is applied is called dispatch. Julia allows
|
4
|
+
The choice of which method to execute when a function is applied is called *dispatch*. Julia allows
|
5
|
+
the dispatch process to choose which of a function's methods to call based on the number of arguments
|
6
|
+
given, and on the types of all of the function's arguments. This is different than traditional
|
7
|
+
object-oriented languages, where dispatch occurs based only on the first argument, which often
|
8
|
+
has a special argument syntax, and is sometimes implied rather than explicitly written as an argument.
|
9
|
+
[1] Using all of a function's arguments to choose which method should be invoked, rather than
|
10
|
+
just the first, is known as [multiple dispatch](https://en.wikipedia.org/wiki/Multiple_dispatch).
|
11
|
+
Multiple dispatch is particularly useful for mathematical code, where it makes little sense to
|
12
|
+
artificially deem the operations to "belong" to one argument more than any of the others: does
|
13
|
+
the addition operation in `x + y` belong to `x` any more than it does to `y`? The implementation
|
14
|
+
of a mathematical operator generally depends on the types of all of its arguments. Even beyond
|
15
|
+
mathematical operations, however, multiple dispatch ends up being a powerful and convenient paradigm
|
16
|
+
for structuring and organizing programs.
|
5
17
|
>>
|
6
|
-
[1]
|
7
|
-
In C++ or Java, for example, in a method call like obj.meth(arg1,arg2), the object obj "receives"
|
18
|
+
[1]:
|
19
|
+
In C++ or Java, for example, in a method call like `obj.meth(arg1,arg2)`, the object obj "receives"
|
20
|
+
the method call and is implicitly passed to the method via the `this` keyword, rather than as
|
21
|
+
an explicit method argument. When the current `this` object is the receiver of a method call,
|
22
|
+
it can be omitted altogether, writing just `meth(arg1,arg2)`, with `this` implied as the receiving
|
23
|
+
object.
|
8
24
|
|
9
25
|
|
10
26
|
自分が思っていた多重ディスパッチの定義だと C++ でも動くようなので、何かの理解を間違えていると思うのですがどこを間違えているのか分かりません。私は3つの polymorphism / ポリモルフィズム(ad hoc / アドホック, subtype / 部分型, parametric / パラメトリック)を Julia と C++ でそれぞれ書くならば、以下のことが行えることと理解しています。
|