Mathematica: MTools: Argument Pattern strong type matching does not intrinsically respect inheritance (makes implementing design-by-contract and some Design Patterns less convenient). But you can use PatternTests with Webel MAll extensions.

This page identifies a possible issue, concern, error, limitation, or bug!
Webel IT Australia promotes the amazing Mathematica tool and the powerful Wolfram Language and offers professional Mathematica services for computational computing and data analysis. Our Mathematica tips, issue tracking, and wishlist is offered here most constructively to help improve the tool and language and support the Mathematica user community.
DISCLAIMER: Wolfram Research does not officially endorse analysis by Webel IT Australia.
Icon class
icon_class
far fa-sticky-note
icon_class_computed
far fa-sticky-note
Note kind
Policy level
Keywords
Whilst argument pattern matching works against a single MTools class name, there is no intrinsic consideration of inheritance. Consider this setup with MClassB extends MClassA:


BeginPackage["Demo`", {"MTools`"}]

$ContextAliases["M`"] = "MTools`Core`MPlusPlus`";

MClassA = M`NewClass[{"Fields"->{"name"}}]

MClassB = M`NewClass[{"Parents"->{MClassA}}]

newB::usage = "newRefR32[name]";

Begin["`Private`"]  

newB[name_] := M`New[MClassB]["name"->name];

End[] 

EndPackage[]

This works as expected (matches against a specific class):


i: b1 = newB["b1"];

i: fTestB[b_MClassB] := M`o.name;

i: fTestB[b1]

o: b1 

But matching against the parent (base) class does not work:


i: fTestA[b_MClassA] := M`o.name;

i: fTestA[b1]

o: fTestA[MClassB[object$123435]]

The Webel MAll MTools extension and All` package provide additional support for more convenient contract tests for use with PatternTest (? queries), as shown in this slide: But it's not so elegant (it would be nicer to have contract matching built-in via a fully vendor supported OO mechanism). Visit also:
NEW: The Webel recipe for pseudo classes with adapted Abstract Data Types (ADTs) handles design-by-contract fine:
Relates to
Related notes
Related notes (backlinks)
Related snippets (extracts)
Visit also
Visit also (backlinks)