Skip to content

confused why compiler doesn't work #485

Description

@mugenosantyago

my contract is this:

contract UserProfile {
let owner: Address
let mut username: ByteVec
let mut bio: ByteVec
let mut profilePicHash: ByteVec
let mut followers: [Address]

// Constructor function
pub fn initialize(ownerAddress: Address) -> () {
    owner = ownerAddress
    username = ByteVec::empty()
    bio = ByteVec::empty()
    profilePicHash = ByteVec::empty()
    followers = [] // Initialize empty followers array
}

// Verify caller is profile owner
fn checkOwner() -> () {
    assert!(callerAddress!() == owner, 0)
}

// Event Definitions
event ProfileUpdated(newBio: ByteVec, newPic: ByteVec)
event NewFollower(follower: Address)

// Update Profile function
pub fn updateProfile(newBio: ByteVec, newPic: ByteVec) -> () {
    checkOwner()
    bio = newBio
    profilePicHash = newPic
    emit ProfileUpdated(newBio, newPic)
}

// Follow function
pub fn follow() -> () {
    let follower = callerAddress!()
    if followers.exists(f => f == follower) {
        // Handle case where follower is already in the list
        emit NewFollower(follower)
    } else {
        followers.push(follower)
        emit NewFollower(follower)
    }
}

// Get Profile function
pub fn getProfile() -> (ByteVec, ByteVec, ByteVec, U256) {
    return (username, bio, profilePicHash, followers.length())
}

}

I get this error every time:

✘ Failed to compile, error: user_profile.ral (1:1): Syntax error
1 |contract UserProfile {
|^^^^^^^^^^

Expected end-of-input
Trace log: Expected multiContract:1:1 / end-of-input:10:1, found "contract U"
otoyume@otoyumes-MacBook-Pro alephi v_4 %

Activity

  1. Lbqds commented on Feb 15, 2025

    @Lbqds
    Member

    Currently the compiler's error messages are not good enough. The first letter of contract should be capitalized: Contract UserProfile.

  2. mugenosantyago commented on Feb 15, 2025

    @mugenosantyago
    Author

    ok my updated code:

    Contract UserProfile() {
    let owner: Address
    let mut username: ByteVec
    let mut bio: ByteVec
    let mut profilePicHash: ByteVec
    let mut followers: Map<Address, U256>
    let mut followerCount: U256

    // Event declaration
    event NewFollower(Address indexed follower)
    
    pub fn __init__(ownerAddress: Address) -> () {
        assert!(owner == Address::zero(), "Already initialized")
        owner = ownerAddress
        username = ByteVec::empty()
        bio = ByteVec::empty()
        profilePicHash = ByteVec::empty()
        followers = Map<Address, U256>()
        followerCount = 0
    }
    
    // Check if the caller is the owner
    fn checkOwner(&self) -> () {
        assert!(callerAddress!() == self.owner, "Not the owner")
    }
    
    pub fn follow() -> () {
        let follower = callerAddress!()
        if !followers.contains_key(&follower) {
            followers.insert(follower, 1);
            followerCount += 1
            emit NewFollower(follower)
        } else {
            let currentCount = followers[&follower];
            followers.insert(follower, currentCount + 1);
            // Do NOT increment followerCount for existing followers
        }
    }
    
    pub fn setUsername(newUsername: ByteVec) -> () {
        checkOwner()
        username = newUsername
        // Emit event if needed, e.g., emit UsernameUpdated(newUsername)
    }
    

    }

    give an error of this:
    ✘ Failed to compile, error: user_profile.ral (2:5): Syntax error
    2 | let owner: Address
    | ^^^^^^^^^^

    Expected "}"
    Trace log: Expected multiContract:1:1 / globalDefinition:1:1 / rawContract:1:1 / "}":2:5, found "let owner:"
    otoyume@otoyumes-MacBook-Pro alephi v_4 %
  3. Lbqds commented on Feb 15, 2025

    @Lbqds
    Member

    You can refer to the doc here to check the contract field definition: https://docs.alephium.org/ralph/contracts#fields

    And the doc here for mapping: https://docs.alephium.org/ralph/types#map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions