2013-10-14 5 views
1

제한된 형식을 생성자 함수를 통해 반환하려고합니다. 제한된 유형이기 때문에 유형을 복사 할 수 없지만 최선의 방법은 아닌지 잘 알고 있습니다. 확장 된 반환 문을 사용하여 작업하고 있지만 제한된 형식을 반환 할 수 있어야한다고 들었습니다.제한된 유형의 Ada 반환

thing_protected.ads :

package Thing_Protected is 

    type Thing is protected interface; 
    procedure Verb_It (Object : in out Thing; Key : String) is abstract; 

    function Create return Thing'Class; 

private 

    protected type Thing_Impl is new Thing with 
     overriding procedure Verb_It (Key : String); 
    private 
     Data : Integer; 
    end Thing_Impl; 

end Thing_Protected; 

thing_protected.adb :

package body Thing_Protected is 

    function Create return Thing'Class is 
    begin 

     -- Not sure how to make this work: 
     -- return Thing_Impl'(Data=><>, others=><>); 
     -- thing_protected.adb:6:35: expected type "Thing_Impl" defined at thing_protected.ads:10 
     -- thing_protected.adb:6:35: found a composite type 

     -- extended return: 
     -- return X : Thing_Impl do 
     --  null; 
     -- end return; 

     -- shortened version: 
     return X : Thing_Impl; 
    end; 

    protected body Thing_Impl is 
     overriding procedure Verb_It (Key : String) is 
     begin 
     null; 
     end; 
    end Thing_Impl; 

end Thing_Protected; 

main.adb :

with Thing_Protected; 

procedure Main is 
    Thing_Instance : Thing_Protected.Thing'Class := Thing_Protected.Create; 
begin 
    null; 
end; 
+3

보호 또는 작업 유형에 대한 집계 구문 ('return Thing_Impl '(...)')이 없습니다. 그래서 나는 당신이 연장 된 수익을 사용해야한다고 생각합니다. (덧붙여서, 컴파일러가 잘못 받아들이더라도'Verb_It '의 본체에 대한'오버 라이딩'은 불법입니다.) – ajb

+0

[* 4.5 제한된 타입과 return 문 *] (http://www.adaic.org /resources/add_content/standards/05rat/html/Rat-4-5.html)을 참조하십시오. – trashgod

+0

ajb : 나는 오버 라이딩이 완벽하게 합법적이라고 생각합니다. [ARM] (http://www.adaic.org/resources/add_content/standards/05rm/html/RM-9-4.html#S0196)에 따르면,'protected_operation_declaration'은'subprogram_declaration' 일 수 있습니다. 'overriding' 지시자를 가질 수 있습니다. – flyx

답변

1

흠, 그래서 당신은 데이터를 초기화하려면? 당신은 generics/packages를 사용하여 그렇게 할 수 있습니다 ... 길고 길며 아마도 약간 뒤얽 힙니다.

package Thing_Protected is 

type Thing is protected interface; 

procedure Verb_It (Object : in out Thing; Key : String) is abstract; 

function Create return Thing'Class; 

private 

generic 
    Default : in Integer; 
package Implementation is 
    protected type Thing_Impl is new Thing with 
    procedure Verb_It (Key : String); 
    private 
    Data : Integer:= Default; 
    end Thing_Impl; 

    Function Create return Thing'Class; 
end Implementation; 

end Thing_Protected; 

package body Thing_Protected is 

package body Implementation is 
    protected body Thing_Impl is 
    overriding procedure Verb_It (Key : String) is 
    begin 
     null; 
    end; 
    end Thing_Impl; 

    function Create return Thing'class is 
    begin 
    return Result : Thing_Impl do 
     null; 
    end return; 
    end Create; 
end Implementation; 


function K(Data_Val : Integer := 10) return Thing'Class is 
    Package I is new Implementation(Default => Data_Val); 
begin 
    return X : Thing'Class := I.Create do 
    null; 
    end return; 
end K; 


function Create return Thing'Class is (K); 

end Thing_Protected;