groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From OC <...@ocs.cz>
Subject Re: changing dynamically the name of classes in a source code
Date Mon, 28 Mar 2016 13:22:49 GMT
> one of my main message in my course is to have  variables with a type!
> so "def x" is not encouraged (not even taught)!

Well, that's arguable at best.

Whilst declaring variables is highly _practical_ for it helps to prevent typos and allows
both the IDE and compiler to assist the programmer, _conceptually_ it is very wrong. The concept
of object programming is that an object is an object is an object; all objects are conceptually
equal, and each object can be sent any message (well, the Java howler of “calling methods”
instead of the proper paradigm of “sending messages” does not help either :( ).

All in all, given you don't teach Groovy but programming, I would strongly recommend to use
some variant of Smalltalk instead -- it is less practical for everyday purposes, but allows
the students to grok the principles of OOP _much_ better than anything Java-based ever might.

> now if you have a factory that returns the proper class and you assign it to
> a variable typed with a subclass it won't work.

===
26 /tmp> <w.groovy
class Localizeděščřžýáíí extends LinkedList { }
class Factory { static LinkedList list() { return new LinkedList() } }

Localizeděščřžýáíí ěěě=Factory.list()
ěěě<<"Actually, it does work, though in Java-based language it's a small miracle"
println "${ěěě.class.simpleName} contains $ěěě"
27 /tmp> groovy w 
Localizeděščřžýáíí contains [Actually, it does work, though in Java-based language
it's a small miracle]
28 /tmp> 
===

?

All the best,
OC


Mime
View raw message