Factor Language: Tuple examples




An example:
TUPLE: employee name position salary ;

This defines a class word named employee, a predicate employee?, and the following slot accessors:


이것은 정의한다 하나의 클래스 워드 이름 붙여진 것은 employee, 술어는 employee?, 그리고 그 따르는 slot 접근자들:
Reader Writer Setter Changer
name>> (>>name) >>name change-name
salary>> (>>salary) >>salary change-salary
position>> (>>position) >>position change-position

We can define a constructor which makes an empty employee:


우리는 할 수 있다 정의하다 하나의 생성자 그것은 만든다 하나의 빈 employee:
: <employee> ( -- employee ) employee new ;

Or we may wish the default constructor to always give employees a starting salary:


또는 우리는 일 것이다 바라다 그 기본 생성자 나아가서 하는 것은 항상 준다 employee들 어떤 시작 급여:
: <employee> ( -- employee ) employee new 40000 >>salary ;

We can define more refined constructors:


우리는 할 수 있다 정의하다 좀 더 개선된 생성자들:
: <manager> ( -- manager ) <employee> "project manager" >>position ;

An alternative strategy is to define the most general BOA constructor first:


어떤 또다른 전략 이다 나아가서 하는 것은 정의하다 가장 일반적인 BOA 생성자 최초로:
: <employee> ( name position -- employee ) 40000 employee boa ;

Now we can define more specific constructors:


지금 우리는 할 수 있다 정의하다 좀 더 구체적인 생성자들:
: <manager> ( name -- employee ) "manager" <employee> ;

An example using reader words:

한 예 사용하는 읽기 워드들:

TUPLE: check to amount number ; SYMBOL: checks : <check> ( to amount -- check ) checks counter check boa ; : biweekly-paycheck ( employee -- check ) [ name>> ] [ salary>> 26 / ] bi <check> ;

An example of using a changer:


한 예 관련된 것은 사용하는 어떤 changer:
: positions ( -- seq ) { "junior programmer" "senior programmer" "project manager" "department manager" "executive" "CTO" "CEO" "enterprise Java world dictator" } ; : next-position ( role -- newrole ) positions [ index 1 + ] keep nth ; : promote ( employee -- employee ) [ 1.2 * ] change-salary [ next-position ] change-position ;

An example using subclassing can be found in Tuple subclassing example.

댓글

이 블로그의 인기 게시물

Publish to my blog (weekly)

Publish to my blog (weekly)