in reply to Re: Idea: Moose Mutal Coercion
in thread Idea: Moose Mutual Coercion
OK, so perhaps first and last names wasn't the best way to describe my idea. I agree with all these points about the difficulties of storing names but there's no need to lament about users who don't exist! It was just an example.
The idea is: A formal way of describing the relationship between attributes that can constructed from each other. Lets start again. After thinking about the problem some more I'm going to rename this from "mutal coercions" to "invertible builders".
Here's a different example that is hopefully less confusing:package Equation_Problem; use Moose; use MooseX::Invertible_Build; has 'a' => ( isa => Num); has 'b' => ( isa => Num); has 'c' => ( isa => Num, invertible_build => { #declare which attrs are part of #our invertible builder group #and the invertible function to base #their builders off of A => 'a', B => 'b', via => 'sum(A, B)', }, ); package main; $math_problem = new Equation_Problem( a => 4, b => 5); print $math_problem->c # prints 9 $math_problem = new Equation_Problem(c => 7, b => 4); print $math_problem->a #prints 3
So what is MooseX::Invertible_Build doing?
It defines this function sum() which can be used in a string as part of the 'via' value in the invertible_build option. sum adds two numbers together and returns the result, so in this case it would add the 'a' and 'b' attributes together and return the result.
but it also creates a builder for 'a' and 'b' which for 'a' would look like:default => sub{$_[0]->a + $_[0]->b}
Now that my idea is more clear lets return to the original example which involves strings and change it a little (and ignore the complexities behind generalizing human names).default => sub{$_[0]->c - $_[0]->b}
has 'full_name' => ( isa => 'full_name', coerce => 1, lazy => 1, invertible_build => ( A => 'first_name', B => 'last_name', via => 'join(' ', A, B)', }, );
now our 'via' statement contains the 'join'function....and Once again it has an inverse! the inverse of join(A,B) with respect to A is (split(' ', C))[0] (with C being full_name). Not only do we know what the builder for full_name is but we also know it for first_name and last_name. MooseX::Invertible_Build goes ahead and defines the default methods for first_name and last name with their respective split statements.
So right now concrete idea for MooseX::Invertible_Build is:Thanks for taking the time to read all this monks. I'll try and get something working this weekend. Any comments/ideas/advice on where to start welcome. Also a description of the mathematical concepts involved would be cool. I used the word 'inverse' a lot which I think is correct, but I forgot all the mathemical jargon to do with sets, functions and mappings etc.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Idea: Moose Mutal Coercion
by tobyink (Canon) on Feb 16, 2012 at 08:36 UTC | |
Re^3: Idea: Moose Mutal Coercion
by BrowserUk (Patriarch) on Feb 15, 2012 at 11:03 UTC | |
by educated_foo (Vicar) on Feb 15, 2012 at 13:58 UTC | |
by BrowserUk (Patriarch) on Feb 15, 2012 at 14:31 UTC | |
by choroba (Cardinal) on Feb 15, 2012 at 16:27 UTC | |
by BrowserUk (Patriarch) on Feb 15, 2012 at 16:54 UTC |