Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^2: my Dog $spot;

by Zaxo (Archbishop)
on Jun 07, 2006 at 07:19 UTC ( [id://553975]=note: print w/replies, xml ) Need Help??


in reply to Re: my Dog $spot;
in thread my Dog $spot;

Objection 1: Well, don't do that ;-) Even though my Dog $spot; looks like strong typing, it isn't. Following with my $dog = $spot; accomplishes exactly the same thing without the constructor call, which is . . .

Objection 2: I agree that the price is too high if allowing the import call for an empty attribute list hurts the performance of vanilla my calls. There appears to be a place in the lexer for my Dog $spot;, and with the death of pseudohashes that ecological niche is empty. Since attributes.pm has already absorbed part of it, it seems natural to extend the rest that way. If the pseudohashes' use for that kind of statement didn't interfere with vanilla my, then it seems likely that this wouldn't either. That remains to be seen, though.

As for the rest, thanks for the props. I agree that that kind of generic object would be easy to generate this way. I'm not a big fan of that sort of thing, though. Without any methods but getters and setters, those classes just look like hash data to me.

Let me expand a little about dynamic strong typing. That's what's behind my interest in this little hack.

The declaration my Dog $spot; doesn't really implement any kind of strong typing. It just looks that way to C++ and Java slingers. A strong dynamic type system for perl needs a little more than that.

Dynamic strong types require a great deal of expensive monitoring and testing at runtime. They can't rely on static checking, like C++. It should be optional in Perl, and you shouldn't pay for it if you don't use it. You shouldn't use it if you don't need it.

I'm working on a follow-on to Tie::Constrained, one which will be a base class for dynamic strongly-typed classes of all kinds. Instead of my Foo $foo; making $foo a Foo, it can tie $foo in a way that makes sure it subsequently remain a Foo. Attempts to assign non-Foo's to $foo will meet sudden death (or something).

That's why I'm interested in this syntactic sugar. The tie interface is ugly and obscure. A constructor-like interface makes people expect a regular weakly-typed object. For a strongly typed Dog class, my Dog $spot; looks exactly like what it does -- makes an uninitialized but indisputable Dog.

That's what I'm really trying to do. An optional, per-variable, pay-as-you-go strong type system for Perl.

It can detaint, too.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^3: my Dog $spot;
by TedYoung (Deacon) on Jun 07, 2006 at 16:24 UTC

    Let's not forget that while native phash syntax is going/gone, phashes still exist in the form of use fields. IIRC, you still need to use the my Dog $spot syntax if you want compile-time checking of field based classes.

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)

        My Tie::Constrained follow-on is meant to do exactly what the Value Types spec describes: to restrict the types which can be assigned to a variable. I don't imagine Perl 6's internals for my Dog $spot; will be much like Perl 5's attribute- and phash-centered code, but the effect would be the same. In fact, it would be trivial to modify T::C to get enforcement of a seperate returns property.

        The restrictions T::C can impose are quite flexible. By constructing what the pod of the current version calls &validator from &UNIVERSAL::isa or &UNIVERSAL::can, we can leash poor $spot either by type or by capability. Constraints on allowable values are just as easy to set up, and detainting before assignment is available as a bonus.

        I'm the first to admit that Perl 6 doesn't loom large for me, but I have been aware of it.

        Added: I've posted example code for a strong typing/value type Dog at Re: my Dog $spot;. It's as experimental as ever.

        After Compline,
        Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://553975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 19:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found