On 6 July 2011 16:37, Mark Proctor <span dir="ltr">&lt;<a href="mailto:mproctor@codehaus.org">mproctor@codehaus.org</a>&gt;</span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<a href="http://blog.athico.com/2011/07/traits-duck-typing-and-dynamic-semantic.html" target="_blank">http://blog.athico.com/2011/07/traits-duck-typing-and-dynamic-semantic.html</a><br>
</blockquote><div><br>The text in this blog has been amended and corrected in several places after<br>it was sent in this email. In my comments I&#39;ve been trying to consider these<br>changes.<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
-----<br>
Duck typing is the ability to say that something implements an<br>
interface. In this article I&#39;ll focus on triples (sets of key value<br>
pairs) such as Maps in Java or Dictionaries in other languages.</blockquote><div><br>An amendment cites the definition of &quot;triples&quot; from the document set<br>around the Resource Description Framework: RDF triples are<br>
subject-predicate-object expressions. There is no way a (single)<br>Map is capable of representing RDF triples for more than one<br>subject and more than one relation. An unrestricted set of<br>RDF triples requires a Set of Map objects.<br>
 <br>   <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The terms map and triple set will be used interchangeably.<br>
<br></blockquote><div>Without some clarification such as that a single subject is implied<br>by a single map this statement is confusing to the reader.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

I&#39;ll use MVEL like syntax to demonstrate:<br>
<br>
bean = [ name = &quot;Zod&quot;, age = 150 ]<br>
bean dons Human<br>
assertEquals( &quot;Zod&quot;, <a href="http://bean.name/" target="_blank">bean.name</a> )<br>
assertEquals( 150, bean.age)<br>
<br>
Without the don&#39;s keyword to duck type the map to the interface this<br>
would not compile as the compiler</blockquote><div><br>Which &quot;compiler&quot; would accept the above without the &quot;dons&quot; expression?<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
 would report a static typing error of<br>
unknown fields for “name” and “age”.<br><br></blockquote><div> <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
rule Human when<br>
     $tp : Map( this contains [ &quot;name&quot; : String, &quot;age&quot; : int ] )<br>
then<br>
     $tp dons Human// that $tp instance is now recognised by all other rules that match on Human<br>
end<br></blockquote><div><br>Is
this meant to be correct in an existing system? AFAIK, a bracketed
expressions is an MVEL inline Map, but instead of &quot;String&quot; and &quot;int&quot; we
would need expressions. I would have thought that<br>    $tp.containsKey( &quot;name&quot; ) &amp;&amp; $tp.containsKey( &quot;age&quot; )<br>would be a good test for determining that the Map object is capable of donning Human.<br>
 </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
rule HelloWorld when<br>
     $s : Human() // this will actually match against the Map &quot;donned&quot; to Human<br>
then<br>
     println( &quot;hello &quot; + $<a href="http://s.name/" target="_blank">s.name</a> );<br>
end<br>
<br>
We can see the rule that applies the trait can probably have a first<br>
class representation for it&#39;s use case.<br></blockquote><div> </div><div>I do not understand what the previous sentence is trying to express.<br>
What is a &quot;first class representation for it&#39;s use case&quot;? <br>
<br> </div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
For instance if someone is Human and is also 18 years of age or younger<br>
we can apply a further abstraction and say the are not just Human but <br></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
also a Student. We use the &quot;dons&quot; keyword after the arguments to say the<br></blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

existing traits the Map must already wear, i.e. abstractions we already<br></blockquote><div><br>It may be necessary to be able to specify more than one existing trait in order<br>to establish another one?<br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

know about the thing.<br>
<br>
trait Student( String name, int age ) dons Human  when<br>
     age(&lt;  18; )<br>
end<br>
<br></blockquote><div>The syntax is ugly, but also confusing, since we now also have field names<br>acting as pattern &quot;type&quot;. Moreover, the hitherto existing type is Human<br>and I would think that the restriction for donning Student must be based<br>
on a Human&#39;s property, hence<br><br>   trait Human dons Student when<br>
        Human( age &lt;  18 )<br>   
end<br><br> </div><div> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
&quot;name&quot; and &quot;age&quot; both have to be represented as objects the system is<br>
going to bloat fast.</blockquote><div><br>But the String object for &quot;name&quot; and the Integer object <br>(let&#39;s ignore simple types) for &quot;age&quot; must be around in any case?  <br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
The relations are what we refer to for<br>
each of the key/value pairs in the triple set, i.e. a property (bean<br>
getter and setter pair, normally on a member field) is a relation on a<br>
class.</blockquote><div><br>A bean property with its getter and setter pair implements a relation<br>between the bean&#39;s class and the property type. <br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

which is generated as an actual class from which beans can be initiated,<br>
“name”, “age” and “gender” are static relations. Young, Boy and<br>
FussyEater are all interfaces.</blockquote><div><br>
The example seems to insinuate that only marker interfaces can be added dynamically.<br>
Is this so?<br>
<br>
If not, how would one &quot;don&quot; a trait with properties? <br>
<br>
 </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> Human extends TripleSet so that we know<br>
that further dynamic relations can be added and traits applied. We<br>
detect the bean instance is “&lt; 18” and thus the trait Young is applied<br>
and that if the gender is M the trait Boy is applied. Further if a<br>
property exists, either static or dynamic (the two are seamless in the<br>
syntax) called “dislikes” with a value of “carrots” we apply the<br>
FussyEater trait.<br>
<br>
declare Human extends TripleSet<br>
     String name;<br>
     int age;<br>
     Gender gender; // M/F enum<br>
end&lt;<br>
<br></blockquote><div>s/&lt;//<br>
<br>
Deus ex machina! Here class TripleSet is thrown at us, like a bone to a dog. Don&#39;t we deserve a wee bit of explanation?<br>
<br>
 </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
trait Young(int age) dons Human when<br>
     age(&lt;  18; )<br>
en<br>
<br>
trait Boy(Gender gender) dons Young when<br>
     Gender( Gender.M; )<br>
end<br>
<br>
trait FussyEater(String dislikes) dons Boy when<br>
     dislikes( “carrots”; )<br>
end<br>
<br></blockquote><div><br>
Here suddenly the property &quot;dislikes&quot; is pulled out of the hat. Is it a property of Boy?<br>
I realise that &quot;dislikes&quot; is added to Human as a dynamic property, but is it recognized<br>
in all sorts of Boy as well? If yes, then this implies that I have access to any property<br>
of the bearer of a donned interface <i>via that interface.</i><br>
<br><br></div><div>
<br>
</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Human human = new Human( “Zod”, 16 )<br>
human.add(  [dislikes : “carrots”]  )<br></blockquote><div><br>
Method &quot;add&quot; is coming from TripleSet? Semantics?<br>
The argument&#39;s Java type is what?<br>
<br>
 </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
insert ( human )<br>
insert( new GiveIceCream( human ) );<br>
<br>
We can now have a single rule that disallows fussy eaters from getting<br>
ice cream. How cool is that :)<br>
<br>
rule “Don&#39;t give icecream to boys who are fussy eaters”<br>
      $f : FussyEater()<br>
      $d : GiveIceCream( $f; )<br>
then<br>
    retract ( $d )<br>
end<br>
<br>
</blockquote></div>